I need more practical Ninject examples

In the past, I used swiftsuspenders, which is the IOC controller for ActionScript 3. Basically, the first version of switchfsuspender had something similar to the Ninject kernel, which was called an injector.

If I wanted to create an application injector (taking into account the most relevant mappings that will be used throughout the application), I would have to inject the injector into the application classes.

Now I'm wondering what is the practice of using kernel.get <> among several classes in an application. Do I have to enter the kernel myself?

Personally, I would prefer to use kernel.inject, but if I can do kernel.inject, I can really nest the dependencies manually, which is probably better (kiss).

The tests are good, but they are far from real practical questions, so I hope you can help me clarify this point. Thanks.

Edit: I noticed that some people are talking about the “root container”. It seems like this is the concept I'm looking for. How to configure the root container and let other application classes know this?

Edit2 Sample code (please forgive the errors, this is just for the sake of):

class SomeClass { public SomeClass() { Command cmd = new Command(); cmd.execute(); } } class SomeOtherClass:ISomeOtherClass { public void allright() { //right } } class Command { ISomeOtherClass dependency; void execute() { dependency.allright(); } } Program.Main() { IKernel kernel = new StandardKernel(); kernel.Bind<SomeClass>().ToSelf().InSingletonScope(); kernel.Bind<ISomeOtherClass>().To<SomeOtherClass>(); SomeClass sc = kernel.Get<SomeClass>(); } 

I have not tested this yet because I'm still afraid of some initialization problems, but my question is, how can the team class learn about SomeOtherClass? My current hypothesis is to inject the kernel into SomeClass and use the Inject method.

+8
inversion-of-control ninject ninject-2
source share
2 answers

Looking at your example, it is clear that SomeClass not built with control inversion in mind; that it has a dependency on Command , but control over this dependency is maintained inside SomeClass . ( Command cmd = new Command(); )

To invert control over this dependency, you will need to specify which dependency in SomeClass. As Remo Gloor pointed out, the standard way to do this with Ninject is through the constructor.

To do this, you can change SomeClass to something like this:

 class SomeClass { private ICommand _command; public SomeClass(ICommand injectedCommand) { _command = injectedCommand; _command.execute(); } } 

You will also need your Command to advertise your addiction:

 class Command { private ISomeOtherClass _dependency; public Command(ISomeOtherClass injectedSomeOtherClass) { _dependency = injectedSomeOtherClass; { void execute() { _dependency.allright(); } } 

Then you register the Command binding in the kernel, perhaps for example:

 Program.Main() { IKernel kernel = new StandardKernel(); kernel.Bind<SomeClass>().ToSelf().InSingletonScope(); kernel.Bind<ICommand>().To<Command>(); kernel.Bind<ISomeOtherClass>().To<SomeOtherClass>(); SomeClass sc = kernel.Get<SomeClass>(); } 

Then the kernel will be able to track the chain of dependencies and enter them all.

+11
source share

Use constructor injection, where possible, and factories whenever there is good reason not to create instances with an object that depends on them.

kernel.Inject should only be used when you are not kernel.Inject creation of an object. For example. Webform

kernel.Get should be used exactly once in the root of your composition (for example, Program.Main or MVC3.DependencyResolver) to create your application.

+5
source share

All Articles