Nov 302009

I just heard the news announced at PDC that the Managed Extensibility Framework, or MEF, is now slated to be included in Silverlight 4.  Great news!

I did a bit of proof of concept work using MEF about a year ago.  MEF is all about composition of providers and consumers.  MEF’s main job is to connect the providers to the consumers so that when the consumer code executes, the providers they require are “just there.”

As trivial as that sounds, it significantly changes how you think about programming.  It forces you to think in terms of composition instead of in terms of plumbing and bushwhacking.  As a result, you can focus more energy on solving your actual problem instead of spending the majority of your time building the plumbing and scaffolding so that you can get to the point where you can begin to solve your problem.

For example, if you’re writing some code and you require “X” to get your work done, you write your MEF code module to declare your need for “X”.  When your module is loaded by MEF, MEF will take care of the dirty details of finding a provider to fill your need for “X”.  The provider may be the one you had in mind when you wrote the code, or it may be completely different, but as long as it implements “X” faithfully, you don’t really care.  The application defines the list of providers to choose from – whether a fixed list determined at build time or a list gathered at runtime – the point is, you and your code don’t need to deal with the how, just declare the what and keep going.

MEF alters your coding thought process from

“I need to figure out how to load and connect to something to get the X that I need”


“I need X.  I can rely on X being here when my code executes.  If for whatever reason X isn’t available, my code won’t be loaded so I don’t have to mess with all that.”

This change in thought process is similar to the shift that can and should occur when you go from writing traditional fire-brigade error handling code to writing code in an exception handling environment. For most of the code in your app (the middle layers), you can operate under the mantra of “If execution gets this far, then everything prior to this must have succeeded.  If anything back there failed, it would have thrown an exception so I wouldn’t be here now.”

I’ll see if I can get clearance to blog about my little MEF experiment from awhile ago.