简体   繁体   中英

Can MEF on asp.net be used for Dependency Injection?

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;

If it can, my follow up question is;

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.

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 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 (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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM