Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 605
quakkels Avatar asked Feb 25 '11 23:02

quakkels


People also ask

Is MEF dependency injection?

MEF is more than just dependency injection techniques. It is used wherein we need a plugin-based architecture for our application, but at the same time MEF uses an IoC-based approach for dependency injection.

Does ASP NET support dependency injection?

ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies.

Is MEF still supported?

Managed Extensibility Framework (MEF) - . NET Framework | Microsoft Learn. This browser is no longer supported. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Is MEF supported in .NET Core?

You can use MEF in your client applications, whether they use Windows Forms, WPF, or any other technology, or in server applications that use ASP.NET. MEF has been ported as Microsoft. Composition to . NET Core as well but partially.


2 Answers

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.

like image 162
Mark Seemann Avatar answered Oct 19 '22 09:10

Mark Seemann


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)

like image 22
RichTea Avatar answered Oct 19 '22 11:10

RichTea