ASP.NET 上的 MEF 可以用于依赖注入吗?
我理论上知道依赖注入是什么,但我还没有在我的任何项目中实际使用过依赖注入。所以我可以认为我是一个 DI 菜鸟。
最简单的问题是; MEF可以用于依赖注入吗?
如果可以,我的后续问题是; 使用 MEF 进行依赖注入是个好主意吗?
我知道我的后续问题可能被视为主观问题。但是,我正在寻找最佳实践以及支持和反对的理由。所以,我希望我的后续问题不会引起太多人的注意。
所有这一切的背景是我在试图弄清楚如何为 ASP.NET MVC 制作一个插件框架时感到有点迷失。
I know what Dependency Injection is in theory, but I haven't ever actually used Dependency Injection in any of my projects yet. So consider me to be a DI noob.
The straightforward question is; Can MEF be used for Dependency Injection?
If it can, my follow up question is; Is it a good idea to use MEF for dependency Injection?
I understand that my follow up question may be viewed as being subjective. But, I am looking for best practices and reasons for and against. So, I hope that my follow up question doesn't rustle too many feathers.
The context of all this is I feel a little lost trying to figure out how to make a plugin framework for asp.net mvc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如我在 我的书 中解释的那样 MEF 可以用作 DI 容器,但就目前的版本而言,它并不是特别适合这项任务。
MEF 旨在解决可扩展性场景,虽然它有很多重叠的功能,但在配置和生命周期管理方面却相当有限。
As I explain in my book MEF can be used as a DI Container, but in its current incarnation it's not particularly well-suited for the task.
MEF was designed to address extensibility scenarios, and while it has a lot of overlapping features, it's quite limited when it comes to configuration and lifetime management.
我相信 MEF 可以用于依赖注入;至少我目前在我自己的小型家庭 WPF 项目中使用它。我怀疑当您需要为应用程序的不同部署注入不同类型的接口时,如果您需要的话,它可能会变得混乱。将正确的类添加到您的目录中需要付出一些努力。
在我工作的地方,使用 ASP.NET MVC2,我们使用 Castle Windsor 进行依赖项注入。然后我们使用 XML 配置来初始化容器。这意味着我们可以为接口注入不同的类型,而无需重建。
我相信 .NET 为 MEF 提供了另一种选择,类似地称为 MAF。它应该更复杂,但提供更多的控制。但我对此一无所知。
(我经验不足(工作一年),所以如果有人不同意我的观点,他们可能更正确)
MEF can, I believe, be used for dependency injection; at least I use it in my own small home WPF project currently. I suspect it might get messy when you need to inject different types for an interface for different deployments of your application, if you require this. It would require going to some effort to add the right classes to your catalog.
Where I work, using ASP.NET MVC2, we use Castle Windsor for dependency injection. We make use then of the XML configuration to initialize the container. This means we can inject different types for an interface without having to rebuild.
I believe .NET offers another option to MEF, similarly called MAF. It's supposed to be more complex, but offer much more control. I don't however know anything more about it.
(I'm not very experienced (1 year employed), so if someone disagrees with my on something, they're probably more correct)
Glenn Block(MEF 前产品经理)在 博客文章。
他的文章中提到的 MEF 的大部分缺点已由 MEFContrib 解决:它包含额外的目录和导出提供程序实现添加对 POCO、开放泛型和拦截的支持。
更新:最近发布的MEF2 Preview3增加了对开放泛型的支持以及开箱即用的无属性注册。预览版本的 API 不是最终版本,但这很好地表明这些功能将出现在下一个 (>v4.0) .NET 版本中。
Glenn Block (former product manager of MEF) answered this FAQ in a blog post.
Most of the shortcomings of MEF mentioned in his post have been addressed by MEFContrib: it contains additional catalog and export provider implementations to add support for POCOs, open generics and interception.
update: the recently released MEF2 Preview3 adds support for open generics and attribute-less registration out of the box. The APIs of preview releases aren't final but this is a good indication that those features will be in the next (>v4.0) .NET release .