The following post will give you a resource with a good run through of how to begin coding with the core functionality of Managed Extensibility Framework (MEF) which is part of .NET Framework 4.0.
Background
At Microsoft Tech Days Sweden this year I presented an upcoming key part of .NET Framework 4.0; MEF. Though it might just get another product name before it ships since the abbreviation is too close to METH for comfort. ;~) I got involved with MEF because I became friends with MEF Program Manager Glenn Block just prior to his joining the MEF team. Subsequently I wrote sort of an epic blog post on adopting MEF for ASP.NET, (Managed Extensibility Framework (MEF) used to make a Composite Web ASP.NET Forms Application). That post is based on the MEF Preview 2 drop and a lot has changed since then so the code does not really run any more if you use a new version of MEF. I am planning to update that sample for the release of MEF and I will post that to MEF Contrib where another Swede and friend of mine Andreas Håkansson are project coordinators. In fact I am preparing a complete code demo project based on the code you can download from this post to be published on MEF contrib. The purpose is to make it quick and easy to learn MEF and enable you to start leveraging MEF fully in your applications after just reviewing the functional samples in these demos.
What is MEF again?
The purpose of MEF is to make it dirt simple to extend your applications. Another purpose is to ultimately make your applications more easy to maintain. With MEF you can focus on writing your application and extensions while completely being able to ignore how to fit your pieces together. MEF assembles your application for you.
Here is a sample piece of code that I asked the attendees of my session if they had ever coded. I swear it was near 100% of the audience. MEF kills this piece of code and I for one will not miss it! ;~)
var dlls = Directory.GetFiles("/Extensions", "*.dll");
foreach (string dll in dlls)
{
var assembly = Assembly.LoadFile(dll);
foreach (Type type in assembly.GetTypes())
{
// [...];
}
}
In MEF all you need to do is export what ever functionality you like from one place in code and import it somewhere else. Then all you need to do to load up extension assemblies is to use one of the out of the box catalogs that reads assemblies or even directories of assemblies and you’re good to go! It really is that simple and I’ve just told you something like three quarters of what MEF is! Then there are some tips and tricks below…
Want to learn how to use MEF?
My code samples goes through the basics of MEF as well as some more exotic samples or advanced samples.
So now all you need to do is download my presentation slides and code below and review it your for yourself.
If you find any inaccuracies in this material please report this back to me so that I may refine this project!
Here are the slides from my presentation:
www.noop.se/files/Open.up.your.applications.with.MEF.pptx
Note: A good portion of these slides were “begged, borrowed and stolen” from a presentation by Glenn Block. Since I was presenting at Tech Days exactly what he would have in respect to theory on MEF I figured re-use and credits is more attractive than re-creating the exact same thing myself.
Here are the code demos I used at Tech Days. No changes since my presentation.
Note: This means that the code files have a lot of extra line feeds in them! You have to scroll down and more down to get to all samples. When I present I like to keep little code on each screen so that I can enable the delegates to follow just one piece of code at a time.
www.noop.se/files/MEFContrib.Demos.zip
Keep an eye on the MEF Contrib site if you want a current version of these demos since that is the place I will ultimately drop this code!
Final word: I had a great time at Microsoft Tech Days – this promises to be a good annual Microsoft event even though I feel it was a pity that there was so little focus on development and a much larger focus on IT.
Cheers,
M.
posted @ Thursday, April 02, 2009 3:35 PM