The Managed Extensibility Framework (MEF)

Since I didn’t attend the PDC 2008, I have started to watch some recorded sessions. While the most interesting one for people extending Visual Studio (“TL32 Microsoft Visual Studio: Customizing and Extending the Development Environment” by Tim Wagner) that I mentioned in my last post is not available yet (I guess it will be posted in a few days), today I have watched the one about the new Managed Extensibility Framework (MEF) by Glenn Block:

Managed Extensibility Framework: Overview

which is the foundation of a new approach to extensibility in VS 2010.

You know what is happening here: once the core .NET Framework is good and stable (.NET 2.0) we are getting lots of Microsoft “foundations” and “frameworks” built on top of it: Windows Communication Foundation, Windows Workflow Foundation, Microsoft Sync Framework, Microsoft ADO.NET Entity Framework, etc. While I am not fan of using whatever Microsoft (or anyone else) ships in version v1 because it will require some time to get stable and really good (and even fun!), once they reach that point this is great because of two reasons:

  1. You get a lot of plumbing architecture done for free and you concentrate on your business.
  2. Microsoft is going to use them in their own products. For example, I think that Microsoft Sync Framework will be used in SQL Server, Windows Mobile, etc., Windows Workflow Foundation is / will be used in SharePoint, Biztalk workflows, etc. This guarantees a bigger level of commitment to some framework which otherwise has a much bigger chance of becoming “disposable” creations.

The session from the PDC is great to get introduced to MEF, which I was not familiar with (BTW, you can start at minute 20 to skip old approaches to extensibility). I was familiar with the problem of extensibility, discovery and catalog of extensions, and plumbing code for menus and toolwindows, because starting with the first the version (4.0) of my MZ-Tools add-in for Visual Studio .NET, despite being an extension for the Visual Studio host with about 40 features, it was in turn extensible. That is, you can build DLLs with classes extending some base class provided by the MZ-Tools SDK and implementing some methods, and you get in the MZ-Tools toolbar new operations (actions performed over the files of your solution, either to review them or to modify them):

MZ-Tools SDK

And in fact I provided several external operations as samples (C# and VB.NET) of features (not included in the MZ-Tools feature set) in the Community Section of my web site:

Community Place

I tried to do it really simple for my customers:

  1. You create a class library with one class for each new operation that you want to add.
  2. Classes inherit from a base class provided by the MZ-Tools SDK and implement very few plumbing methods
  3. You build the class library and you put the DLL in certain folders (no need for registration files at all)

When loaded, the MZ-Tools host gets the DLLs from those folders, loads them, iterates exported types searching the ones that inherit from the needed base class and then calls its methods to get menu captions, icons, etc.

The Managed Extensibility Framework addresses this kind of scenarios decoupling as much as possible the host and the extensions, using attributes to denote what the extension offers (menus, toolwindows, etc.) and what the extension needs from the host (services). It also discovers extensions, catalogs them and does the needed connections between the host and the extensions. It also addresses the performance problem that can happen with a host with tons of extensions, using delayed loading.

So, a very interesting subject. Even if you are simply creating extensions for Visual Studio, MEF will be used in VS 2010 as a new way to extend it, so it’s good to get started learning it.