Sunday, November 29, 2009

 

Getting started with Managed Extensibility Framework (MEF)

Recently I got a chance to work with WordPress and Drupal, those are the coolest PHP based blogging and content management systems out there, while I was impressed with the simplified installation and configuration, one thing that struck me was how simple it is to add plugins to the system. For instance I wanted a photogallery, so I downloaded the plug-in from WordPress.com, unzipped, dropped it in the plugins folder, went to the Administration panel and there it was, the plug-in sitting right there ready to activate.

Fortunately .NET now gets its own plug-in architecture framework in the form of Managed Extensibility Framework or MEF.

MEF makes it really easy to build extensible .NET applications with just a few lines of code.

Here’s how. 

In the application that needs to be extended you define a common interface that your plugins can implement.

   1: public interface IRule   
   2: {   
   3:     void DoIt();   
   4:     string Name { get; }   
   5:     string Version { get; }   
   6:     string Description { get; }   
   7: }

Then you make a collection where all the plugins are stored and mark it with Import or ImportMany attribute.



   1: [Import( typeof( IRule ) )]      
   2: internal IList<IRule>: _rules { get; set ;} 

Then all you need to do is write your plug-in that implements the above IRule interface that you defined in your extensible application and mark it with Export attribute.



   1: [Export( typeof( IRule ) )]   
   2: internal class RuleInstance1 : IRule   
   3: {   
   4:     public void DoIt() {}   
   5:   
   6:     public string Name   
   7:     {   
   8:         get { return "Rule Instance 1"; }   
   9:     }   
  10:   
  11:     public string Version   
  12:     {   
  13:         get { return "1.0.0.0"; }   
  14:     }   
  15:   
  16:     public string Description   
  17:     {   
  18:         get { return "Some Rule Instance"; }   
  19:     }   
  20: }   

With that only a few lines of code is required to load the plug-in into your extensible application.



   1: public void Init()   
   2: {   
   3:     var catalog = new AggregateCatalog();   
   4:     var container = new CompositionContainer( catalog );   
   5:     var batch = new CompositionBatch();   
   6:     batch.AddPart( this );   
   7:     // because all our types are in the same assembly we simply use the current one.       
   8:     catalog.Catalogs.Add( new AssemblyCatalog( Assembly.GetExecutingAssembly() ) );   
   9:     container.Compose( batch );   
  10:     foreach ( var rule in _rules )   
  11:     {   
  12:         Debug.WriteLine( rule.Name );   
  13:     }   
  14: }  

While this simple example is great for exploring MEF and building plug-in that live in the same assembly, in the real world scenario your plugins will be built as part of a different solution or a project, do check out some of the resources below that demonstrate in detail how to leverage MEF in your applications.

Resources:


MEF on CodePlex – contains assemblies, source code and sample applications, do checkout the Silverlight grid sample.



The PDC09 demo
http://twurl.nl/3cj8dp

Hanselminutes podcast on MEF (with Glenn Block, the PM)
http://twurl.nl/v6zoio

ScottGu’s awesome demo from PDC08 showing how MEF is used in the new VS2010 IDE to make it extensible, a must see.
http://blogs.msdn.com/brada...

The code sample shown above is taken from here, uses the Preview 8 drop of MEF on Codeplex
http://devlicio.us/blogs/derik_whittaker/archive..

MEF on MSDN – covers MEF in detail including the scenario to load multiple plugins and other details.

Labels: , ,


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?