什么是内核(关于依赖注入)?

发布于 2024-08-30 07:19:20 字数 40 浏览 1 评论 0原文

我看到“内核”一词被经常使用,但我不确定它的含义。你能举个例子吗?

I see the term kernel used a lot but I am not sure what it means. Can you give an example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私藏温柔 2024-09-06 07:19:20

内核就是容器本身。它在 Windsor 中被称为“内核”(实际上 MicroKernel)和 Ninject,因为它只提供核心注入功能,依赖于包装器(对于 MicroKernel,它是 WindsorContainer)或模块/扩展方法(在 Ninject 的情况下)提供便利的功能(例如,WindsorContainer 提供 XML配置解析)

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)

败给现实 2024-09-06 07:19:20

Ninject 和其他依赖注入器中的内核是应用程序的核心。它是其他模块的容器。

模块代表应用程序的一个独立部分。您可以根据自己的代码库结构自由地组织它们。然后,您可以通过构造函数将这些模块加载到内核中。 请参阅此页面以获取相关示例。

内核对象也是负责解决依赖关系和创建新对象的对象。

例如,在 C#/.NET 中的 Ninject 中,您可以使用内核将接口绑定到其实现:

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

然后,当您按如下方式创建对象时...

IMyFoo myFoo = ninjectKernel.Get<IMyFoo>;

...内核将自动返回 MyFoo< 类型的实例/code> 因为您刚刚指定的绑定。

此页面提供了有关内核和模块如何配合的更多概述一起,以供进一步阅读。 Ninject 的此页面也可能有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文