Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Kernel.Inject(instance); actually do?

Tags:

ninject

I am learning to use dependency injection with ninject. Most of the properties and methods are fairly intuitive, one that has me though is Kernel.Inject(instance);

What does the Inject method actually do as it doesn't return anything. I've looked around but search for a method called inject on a dependency injection container is a nightmare, I can't find any references to the method specifically.

like image 608
deanvmc Avatar asked Apr 02 '12 19:04

deanvmc


1 Answers

Kernel.Inject(instance) will inject dependencies into an already existing object.

That's why it returns void because it takes the parameter object instance and starts to investigate it's methods and property setters looking for the [Inject] attribute. Then it will call them with the resolved instances of their parameter types. (this is called Method or Property injection)

So when contructor injection is not enoughpossible you can Kernel.Inject to fill in your dependencies for a given instance.

You can read more about this here: Ninject Injection Patterns

like image 137
nemesv Avatar answered Jan 10 '23 18:01

nemesv