Allow Recomposition in MEF

By | September 3, 2011

MEF supports an opt-in feature that allows recomposition of the Import when the underlying object is changed either explicitely by the user or in the Catalog.  The Recomposable Import unit is called live import as it automatically changes the object cycle when actual underlying object is changed. Today I will discuss how exactly this works in MEF and how to use the same in your plugin based application.

public IEnumerable> GetNames { get; set; }

Here in the import, we have specified AllowRecomposition = true, that means the entire object will be recreated when the underlying collection is modified or some new object comes in to the catalog.

The Recomposable Import is not only useful in Collections, you can also use this trick for single Imports too.

For your Recomposable Import defination, when your catalog changes or rather say you put a new dll in the DirectoryCatalog, the whole collection will automatically recreated for you.

DirectoryCatalog dcatalog = new DirectoryCatalog("plugins", "*.dll");

Say your DirectoryCatalog is pointing to plugins, and before Refresh say we place a new plugin to it, the whole IEnumerable will automatically been recreated by MEF.

I hope this post comes handy.

Read more about it from the links :

Managed Extensibility Framework – A Look

Steps to write a plugin based application with MEF

Thank you for reading.