Recomposition

Some applications are designed to dynamically change at runtime. For example, a new extension may be downloaded, or others might become unavailable for a variety of reasons. MEF is prepared to handle these kinds of scenarios by relying on what we call recomposition, which is changing values of imports after the initial composition.

An import can inform MEF that it supports recomposition through the [System.ComponentModel.Composition.ImportAttribute] using the Allowrecomposition property. See the code snippet below:

[Export]
public class HttpServerHealthMonitor
{
    [ImportMany(AllowRecomposition=true)]
    public IMessageSender[] Senders { get; set; }

This tells MEF that your class is ready to handle recomposition, and if the availability of IMessageSender implementations changes (either a new one is available, or made unavailable), the collection should be changed to reflect it. Once a part has opted in for recomposition, it will get notified whenever there is a change to the implementations available in the catalog, or if instances have been manually added / removed from the container.

Note:
  • When recomposition occurs, we will replace the instance of the collection / array with a new instance, we will not update the existing instance. In the example above, if a new IMessageSender appears, Senders will be completely replaced with a new array. This is in order to facilitate thread-safety.
  • Recomposition is valid for virtually all types of imports supported: fields, properties and collections, but it is not supported for constructor paramters.
  • If your type happens to implement the interface [System.ComponentModel.Composition.IPartImportsSatisifiedNotification], be aware that ImportCompleted will also be called whenever recomposition occurs.
Last edited Jul 9 at 11:07 PM by weshaggard, version 5
Comments
obiwanjacobi Mar 17 at 4:40 PM 
will it set a new value (collection) or just add/remove the instances?

Gutek Apr 7 at 11:39 AM 
Recomposition will work without DirectoryCatalog("dir", true")? as far as I understand, this is the only default catalog that is monitoring changes to load/unload new extenstions.

lancecontreras Jul 28 at 12:51 PM 
I think they removed the bool parameter that monitors changes in the directory, instead they put a Refresh method to the DirectoryCatalog to discover additional extensions in the folder, though you can write your own logic for watching directory and call the refresh method when the folder is modified.

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2009.10.27.15987