开发者

What is a kernel (with regards to dependency injection)?

I see the t开发者_开发问答erm kernel used a lot but I am not sure what it means. Can you give an example.


The kernel is the container itself. It's called "kernel" in Windsor (actually MicroKernel) and Ninject because it only provides the core injection functionality, relying on wrappers (in the case of MicroKernel, it's WindsorContainer) or modules/extension methods (in the case of Ninject) to provide convenience features (for example, WindsorContainer provides XML configuration parsing)


The kernel in Ninject and other dependency injectors is the core of the application. It's a container for the other modules.

A module represents an independent section of your application. You're free to organise them as you see fit within the structure of your codebase. You then load these modules into the kernel through the constructor. See this page for an example of that.

The kernel object is also the object responsible for resolving dependencies and creating new objects.

For example in Ninject in C#/.NET you can use the kernel to bind an interface to its implememtation:

IKernel ninjectKernel = new StandardKernel();
ninjectKernel.Bind<IMyFoo>().To<MyFoo>();

Then when you create an object as follows...

IMyFoo myFoo = ninjectKernel.Get<IMyFoo>;

... the kernel will automatically return an instance of type MyFoo because of the binding you just specified.

This page gives more of an overview of how kernels and modules fit together, for further reading. This page from Ninject may also help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜