如何在 MEF 和 IoC 容器之间做出选择?

发布于 2024-10-06 10:52:18 字数 216 浏览 1 评论 0原文

什么场景下我们应该使用哪个?如何决定选择哪一个?什么情况下我们会选择两者一起使用?

我以前曾使用过 Unity 容器 ()。

In which scenario should we use which? How to decide on which one to choose? And under which circumstances would we choose to use both together?

I have previously worked with Unity Container ().

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

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

发布评论

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

评论(4

清君侧 2024-10-13 10:52:18

棘手的问题 - 因为两者确实在一定程度上重叠。

我想说的是:

  • 如果您主要关心依赖注入是为了解耦组件,例如为了能够注入模拟(用于测试),请使用任何有用的 IoC

  • 使用 MEF,特别是如果您更倾向于可扩展,例如能够“从该目录加载导出特定接口的所有程序集”,并且如果您需要可扩展并向第三方开放(例如 Visual Studio:提供公共 API,以便其他人可以为您的应用程序编写扩展) 。这就是 MEF 真正发挥作用的地方

对于 MEF 和 Unity,还有 MEF和 Unity 集成层,将两种工具的优势结合在一起。

我还建议您查看 Ayende 的 优秀博客文章 了解 MEF 与 IoC 的区别。

Tricky question - since the two do indeed overlap to a certain degree.

I would say this:

  • use any useful IoC if you're primarily concerned about dependency injection for the sake of decoupling your components, e.g. for the ability to be able to inject a mock instead (for testing)

  • use MEF especially if you're more into being extensible, e.g. be able to "load all assemblies from that directory that export a certain interface", and if you need to be extensible and open for third parties (like Visual Studio: offer a public API so others can write an extension for your app). This is where MEF really shines

For MEF and Unity, there's also the MEF and Unity Integration Layer to combine the strengths of both tools together.

I would also recommend you check out Ayende's excellent blog post on what differentiates MEF from an IoC.

救赎№ 2024-10-13 10:52:18

当您有第三方编写实现接口的插件并且您希望能够在不破坏无法重新编译的第三方插件的情况下对接口进行版本控制时,MEF 会大放异彩。相反,MEF 比原始 IoC 更复杂。

因此,如果所有内容都作为同一构建系统的一部分进行编译,我会说 IoC;如果您需要处理无法自行重新编译的加载项,我会说 MEF。

MEF shines when you have 3rd parties writing plug-ins, that implement interfaces and you wish to be able to version your interface without breaking the 3rd party plug in you can’t recompile. In exchange MEF is more complex than a raw IoC.

So I would say IoC if everything is compiled as part of the same build system, and MEF if you need to cope with add-ins you can’t recompile yourself.

爱的故事 2024-10-13 10:52:18

Glen Block(MEF 前产品经理)在他的博客上对此进行了很好的介绍:

Glen Block (former Product Manager of MEF) has covered this pretty well on his blog:

朱染 2024-10-13 10:52:18

我听到了对此的一个很好的解释(向作者致歉,我忘记了是谁):在非常高的水平上,当你想要给定接口的一个东西时,IoC 是好的,当你想要所有的东西时,MEF 是好的从给定的接口。

例如,在 IoC 中,您希望为接口返回一个特定的单个具体类:

For<ICarFactory>().Use<CarFactory>();

每当您想使用 ICanFactory 时,您都会得到一个 CarFactory

MEF 很擅长说给我所有的汽车工厂:

CheapCarFactory : ICarFactory
FamilyCarFactory : ICarFactory 
LuxuryCarFactory : ICarFactory

等等。

I heard a great explanation of this (apologies to the author, I've forgotten who it was): At a very high level, IoC is good when you want one thing for a given interface, MEF is good for when you want all things from a given interface.

For instance in IoC you want to return a specific single concrete class for an interface:

For<ICarFactory>().Use<CarFactory>();

Whenever you want to use a ICanFactory you will get a CarFactory.

MEF is good for saying give me all the car factories:

CheapCarFactory : ICarFactory
FamilyCarFactory : ICarFactory 
LuxuryCarFactory : ICarFactory

Etc.

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