MEF ‘n IoC

If you understood the title of this post, then you’ll understand the question that’s been in my head for a few weeks.  Recently, Microsoft released a preview of the Microsoft Extensibility Framework (MEF) up on CodePlex.  As I do with most things when first explained to me, I attempted to bucket MEF in terms of something I already understood.  So, the first thing I thought of was "oh, it’s an IoC thing".  The person explaining to me said that it wasn’t, but it wasn’t clear to me at the time what it actually was.

Since then it’s been on my list to sit down and write something using MEF to get this figured out.

Fortunately, at a meeting yesterday, Brad did a quick MEF demo (which I now see is exactly what he put on his blog, making me fell less special).  This nicely knocked a "to-do" off of my list and now I get it.  So, in case you’re wondering about this same thing, here goes.

MEF is not an IoC container.  You can use it kind of like one, but that’s not what it’s really meant for.  And if you’re doing Dependency Injection, you should stick with StuctureMap or Ninject or whatever you’re using already.  MEF is basically using some IoC-like principles to enable application composition and dependency management.  So in some way it’s kind of like IoC for your application.  It’s focused on discovering components and letting your application compose itself on the fly.  It’s designed for larger applications like Visual Studio.  Now that I understand it, and, yeah, it’s pretty darn cool.

In my opinion, IoC is more about a consistent loose-coupling pattern across your app, and in many ways the factories act like smart service providers, with the containers adding a variety of ways to configure how query resolutions are done at a particular point in time (e.g. they allow you to register bindings in config or in code, statically or dynamically, etc.)

Hopefully that helps anyone else out there.  If you pull down the MEF code expecting it to be an IoC container, you’ll be pretty confused.


Reference: Ayende’s post was helpful in explaining this as well.

Skip to main content