| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Dokkio Sidebar (from the makers of PBworks) is a Chrome extension that eliminates the need for endless browser tabs. You can search all your online stuff without any extra effort. And Sidebar was #1 on Product Hunt! Check out what people are saying by clicking here.

View
 

MEF

Page history last edited by Garo Yeriazarian 12 years, 3 months ago

Convener: Garo Yeriazarian (@garoyeri)

 

MEF is Managed Extensibility Framework: http://mef.codeplex.com/

 

This project is part of the .NET 4.0 BCL, but you can grab a download from CodePlex with the latest code that will run in .NET 3.5.  There is also a MEFContrib project here: http://mefcontrib.codeplex.com/

 

The documentation on the MEF site is a good place to get started.  I was able to quickly get going with it the first day just by browsing the documentation on codeplex.

 

Overview

To bootstrap MEF, you need a catalog (or set of catalogs) and a container.  The easiest way to start is DirectoryCatalog and CompositionContainer:

 

using System.ComponentModel.Composition.Hosting;

// ...

_catalog = new DirectoryCatalog(@".\");

_container = new CompositionContainer(_catalog);

 

 

There are several types of catalogs too: http://mef.codeplex.com/wikipage?title=Using%20Catalogs&referringTitle=Guide

You can also subclass and create a filtered dialog to do extra checking or selecting of Assemblies: for example, you could check the signing of an assembly to make sure it is "Approved" for use in the application.  Or, you could exclude parts from the system using namespace filtering.

 

You have to use attributes to identify parts that are available in the container and where they should be injected.  For example:

 

[Export(typeof(IFoo))]

public class Foo : IFoo {}

 

This creates a contract for type IFoo.  When you want to inject IFoo, you can do this:

 

public class Something

{

     [Import]  // this is implicitly asking for IFoo

     public IFoo Thingy { get; set; }

 

     [ImportMany]   // this will get all the IFoo instances

     public IFoo[] AllThingies { get; set; }

}

 

MEF doesn't care about access for properties or fields, it will inject just about anything (hence the "Crystal MEF" references).

 

You can also change the contents of the catalog at run-time and have it affect the live instances.  In the [ImportMany] case, you can add AllowRecomposition=true to say that you are ok with property being reassigned at runtime.

 

Furthermore, you can set some imports to happen lazily, these are like using Futures and will be loaded when requested:

[Import]

public Lazy<IFoo> Thingy { get; set; }

 

There was a lot of discussion on the IoC-ness of MEF.  MEF is not meant to be an IoC, it is meant to be a framework (F-Bomb!) for building pluggable systems.  One recommendation is to use MEF to probe assemblies for configuration information and create a configuration set of instances.  Then, you can plug and chug to register the appropriate parts into StructureMap or Spring or whatever container you want.  There are some articles on combining Unity with MEF and Autofac with MEF.  MEFContrib has some goodies too.

 

You can also use the Primitives from the MEF library (check the appropriate namespace for details).  Apparently, you can use parts of the MEF library independently.

Comments (0)

You don't have permission to comment on this page.