Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MEF as an IoC

Tags:

c#-4.0

mef

After reading some stuff such as this: http://mikehadlow.blogspot.com/2008/09/managed-extensibility-framework-why.html

I understand that MEF has some featcures that I will not find in an IoC, and that MEF has some IoC stuff that is probaboly not as advanced as some other IoC systems can offer.

I need the MEF stuff. Do I also need an IoC framework or would I be fine with what MEF has?

Asher

like image 327
Asher Avatar asked Jul 20 '10 09:07

Asher


People also ask

Is MEF an IoC container?

No, it is otherwise. IoC is a broad concept and DI is the design pattern to implement the core of IoC. MEF is some form of DI, but it has not all fundamental features of IoC. MEF uses composition to find out the dependencies it needs to resolve.

Is MEF dependency injection framework?

This article shows how to use the built-in dependency injection. The Managed Extensibility Framework (MEF) is a built-in set of elements that allows you to “export” and “import” objects across projects that allows you to not rely on hard dependencies.

Is dependency injection A IoC?

Dependency Injection is one of the subtypes of the IOC principle. Aspect-Oriented Programing is one way to implement Inversion of Control.

Is MEF part of .NET core?

For those who don't know, the Managed Extensibility Framework (MEF) is alive and well, and has been ported to . NET Core as System.


2 Answers

Depends on your requirements/existing code.

If you have an existing code infrastructure built on an IoC container, you can in fact combine these with MEF. Recently I've been building an ASP.NET MVC+MEF framework, and a couple of my readers have been asking how to integrate Unity with the MEF+MVC framework I have built. This turned out to be really easy, thanks to a project called the Common Services Locator.

The CSL project is designed to provide an abstraction over service location, so I can grab a CSL provider for Unity, wire it up with a custom ExportProvider and MEF automatically starts composing my IoC-driven parts.

This is one of the benefits of MEFs ExportProvider model, you can easily plug in any additional providers to start pulling exports from a variety of sources.

Last week I blogged about combining MEF+Unity (and also MEF+Autofac as another exmaple), and although my examples are geared up for ASP.NET MVC, the concept is the same for most other implementations.

If you have the option of building something fresh using MEF, you'll probably find that you won't need an IoC container, MEF can handle property injection, constructor injection, part lifetime management, and type resolution.

Let me know if you have any questions :)

like image 115
Matthew Abbott Avatar answered Sep 22 '22 03:09

Matthew Abbott


An IoC follows a purpose.

MEF specially is good designed, if you want to have some sort of plugins in your system. or code that you do not trust to run properly (dont forget to handle exceptions).

but, it comes therefore with a overhead.

if you just want to do IoC, as it is a good design pattern for extensible and testable software, then i would recommend AutoFac, as it is from the same guy. more or less :-)

and, if you need both intentions. then use Both. as Matthew pointed out, you can use the CSL to abstract both. if wanted.

for plugins -> MEF

for your IoC -> an simple IoC

like image 32
cRichter Avatar answered Sep 19 '22 03:09

cRichter