This project has moved and is read-only. For the latest updates, please go here.

MEF Container + IoC Container = Me confused

Jan 9, 2009 at 4:05 PM
I'm looking for a little guidance on how to MEF fits into the picture when using DI/IoC.

Since MEF has containers and whatever IoC framework has containers, is there any guidance on how best to accomplish this? Where does Microsoft see MEF fitting into the picture from the grand scheme of things?

Where I'm left scratching my head is that there is a container for the IoC framework (Unity/Autofac/StructureMap/etc) and a seperate container for MEF that can be used for DI/IoC but isn't as functional (purposely from what I gather) as a full fledged IoC container.

So, how do you integrate MEF in with the IoC containers? Should MEF "live inside" something like Unity extended with behaviors/modules? Or should it be the other way around (in which case MEF would have to become much more capable)?

In the project I'm currently working on (A WCF app), I'm using Autofac with the WCF integration together with MEF. With Autofac, I'm using a module with an event handler on the "Activating" event to add the part to the MEF container and satisfy imports. But then I see posts where you mention using MEF to set up sub-modules for Unity and I'm wondering if I'm jumping through all sorts of hoops using 2 frameworks to accomplish the same basic thing.

I'm using MEF with to do the injection for my plug-ins (the recomposition is sooo nice), but using Autofac for the Dependency Injection (while leveraging MEF to allow dependencies to change at runtime which is super nifty).

Is this the wrong approach, or am I way off in left-field on this?
Jan 9, 2009 at 7:08 PM
Edited Jan 9, 2009 at 7:12 PM

We don't have a real position on this yet, as we are doing our own research to understand best how the two can integrate (or if that even makes sense). We have several approaches we are looking at including:

1. One is the apporach you mentioned where both live together with a separation of responsibilities. We're not crazy about this idea as it results in ambiguities though we know it is technically possible. This seems similar to the approach you are doing.
2. We're also considering other approaches which will not require two containers. Nick will have more on this soon.

Jan 9, 2009 at 9:24 PM
Edited Jan 9, 2009 at 9:28 PM

My thoughts, represented in the MEFContrib project, were to use MEF for what it does best - Module add-ins and to use the Prism/CompositeWPF's DI/IOC to do what it does best.   There was an issue of "chicken-before-the-egg" and does MEF or DI/IOC bootstrap the application?  It required some special footwork in the project's bootStrapper.  

My stab at it (see VIDEO clip) in the MEFContrib project was based on a laymans knowledge of MEF and Unity, and even with that it was very very powerful!  I'm excited about MEF, MAF and DI/IOC (Unity) coming out in a single solution - it will be a very powerful package.

Jan 9, 2009 at 9:56 PM
[Glenn] to understand best how the two can integrate (or if that even makes sense).
MAF is amazing because of the ability to reclaim resources - very important for enterprise level applications I have been involved with - the pipeline can take some time to wrap your brain around and the juice hasn't been worth the squeeze so we don't get to benefit from it.  MEF doesn't require much of a thought process so if you could MEF'ify MAF we (developers) could bring great value to those relying on us. 

So MEF makes great sense, but I also need Unity for my ASP.NET, desktop and now WCF applications.  Like the average joe programmer I can figure out a thing or two but the reality is only the Gurus, who truly understand the system like the back of their hand, can integrate them efficiently;  I took a shotgun theory approach and my mouth literally dropped when it just worked - a testimony to the power of MEF, the CompositeWPF and Unity more than my programming talents....

So I hope the gurus find that it makes sense...   
Jan 9, 2009 at 10:23 PM

Approach 1 has a code-smell that makes my stomach get all queasy. (Thus this topic)
Approach 2 seems much more preferable, I just don't see how it would fit without doing major work to either MEF or having to write a lot of adapter fluff for your IoC container.
I'm a big fan of Autofac (it is currently my favorite IoC container), so can't wait to see what Nick is cooking up.

Agreed, MEF is pretty cool when you first run something that is recomposable and the magic voodoo happens. I too am impatiently awaiting some sort of guidance on how MS plans to integrate MEF + MAF + DI/IoC. If you could have MEF leveraging MAF to do seperate AppDomains so assemblies can be unloaded + MEF integrating with your IoC container (thinking runtime recomposable IoC here) then you have a really nice framework for writing plug-ins/host apps with very little friction. Oh, and get the duck-typing back in MEF so folks writing extensions can do it "blind".

I'm curious to see what approach Microsoft takes on integration of all of them.
Jan 21, 2009 at 12:29 PM
Since scratching my head I though of the following:

Use Di/IOC (unity) for internal injection, (when you don't want third parties to be able to add/remove components) and for easy testing. You can also add you own builder strategy, so for example you could create a strategy auto-wire up an object to an event broker when the object is created - would be nice possibly to see a similar ability to add build strategies in MEF.

Use MEF when you want a component to be able to be changed by a third party (for example setting a logger to use event log instead of file) or when there are multiple components with the same contract to be loaded.

I've always thought the line between MEF and MAF is the bluriest, if MEF had the ability to set the Appdomain that the extension runs in, I can't really see that much difference.
Jan 21, 2009 at 5:00 PM

Expect to see more integration between MEF/MAF in the future. It's doubtful that we'll attempt to reinvent the wheel around isolation and versioning as MAF already does this very well.


Jan 21, 2009 at 10:00 PM
Edited Jan 21, 2009 at 10:04 PM
Hi Andy

As David mentioned we are looking at MEF/MAF integration. Currently I am doing some spiking / prototyping arond the integration which will most likely yield something out-of-band in the form a sample / library on Codeplex. From what I have seen so far, there appears to several meaningful approches to integration.

In the future (post V1) we will be considering support of those scenarios either through tighter platform integration or some other means. It's too early to tell.

Aug 1, 2009 at 9:21 PM
Edited Aug 1, 2009 at 9:25 PM

Alright...  I've been searching online for about an hour looking for some guidance/samples for Unity/MEF.  Since this thread is from Jan I would expect SOMETHING to be out there... so where might it be hiding? :) :) :)

EDIT: OK, I did find this:

But it's not "official" and it's a bit old... and MEFContrib doesn't have any mention of "Unity" in its codebase (despite the claim in the above post).  Is this the closest guidance there is?

Aug 4, 2009 at 9:33 PM
Edited Aug 4, 2009 at 9:33 PM

Here's the blog post for the above code sample link:

Aug 4, 2009 at 11:46 PM

HI Bill

I understand the confusion.

The approach that Piotr is suggesting is for folks that want to integrate MEF and Unity. MEF catalogs and our attributed model are great for third-party extensibility scenarios where the app needs to discover on the fly the available implementations. Unity however (and other IoC containers) really shine for cases where there a specific set of components like loggers, repositories etc. Unity also provides capabilities not available in MEF out of the box, like AOP interception, and explicit wire up.  

One place I would see this approach being valuable (and the one I believe Piotr was targeting) is if you have an existing application built on Unity, such as one using Prism or Enterprise Library and you want to add third-party extensibility to it. For that, this seems to be a reasonable solution.


From: BillL []
Sent: Tuesday, August 04, 2009 1:33 PM
To: Glenn Block
Subject: Re: MEF Container + IoC Container = Me confused [MEF:43823]

From: BillL

Read the full discussion online.

To add a post to this discussion, reply to this email (

To start a new discussion for this project, email

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at

Aug 5, 2009 at 12:33 AM

Michael, the guidance is not hiding, we're working on it :-)

In short I would say that we see both as solving orthagonal problems, though there is some obvious overlap. We are thinking along several lines as to how both live together in the same app. Piotr's approach is one line of thinking, another is the approach that Nick outlined in his post here. And a third involves designing a system in such a way that the paths don't cross at all.

I think it's fair to say that the most solid thing we can say is we think it makes sense that apps may want to take advantage of both MEF and Unity in the same app. How is still undetermined, though we are working on it.

Dec 5, 2010 at 12:50 PM
Edited Apr 20, 2011 at 1:03 PM