DCSIMG
MEF – Runtime Type Catalog, support multi registrations in runtime - Zuker On Foundations

Zuker On Foundations

The realm of .NET (WPF, WCF and all around)
MEF – Runtime Type Catalog, support multi registrations in runtime

Recently, I dug deep into MEF because I was involved in a project where there was a decision to upgrade the WPF client to PRISM 4 and advance to using MEF alone instead of Unity.

While many argue that MEF isn’t yet a full DI framework, it still provides an easy way to enable application extensibility in your projects.
One of the tasks that I had while upgrading the client to PRISM 4, is to keep supporting type registration in runtime, something in the sort of the following -

Code Snippet
  1. public interface IFoo
  2. {
  3. }
  4.  
  5. [Export(typeof(IFoo))]
  6. public class Foo : IFoo
  7. {
  8. }
  9.  
  10. catalog.Register<Bar>();

The built-in TypeCatalog that comes with MEF requires you to specify the types in the constructor and there is no support for registrations in runtime for the same catalog.
In order to support this feature, I could simply add a new TypeCatalog for every registration request in runtime with the desired type, but that seemed to be kind of wasteful.

MEF does support catalog changes in runtime, you need to implement ‘INotifyComposablePartCatalogChanged’.

The solution

I implemented a type catalog that supports registrations in runtime, here is the core implementation -

Code Snippet
  1. public void Register(IEnumerable<Type> types)
  2. {
  3.     if (types != null)
  4.     {
  5.         List<ComposablePartDefinition> addedParts = new List<ComposablePartDefinition>();
  6.  
  7.         foreach (Type type in types)
  8.         {
  9.             ComposablePartDefinition part = AttributedModelServices.CreatePartDefinition(type, null);
  10.  
  11.             addedParts.Add(part);
  12.         }
  13.  
  14.         using (var atomicComposition = new AtomicComposition())
  15.         {
  16.             OnChanging(new ComposablePartCatalogChangeEventArgs(addedParts, EmptyPartDefinitions, atomicComposition));
  17.  
  18.             _lock.EnterWriteLock();
  19.             try
  20.             {
  21.                 foreach (var addedPart in addedParts)
  22.                 {
  23.                     _parts.Add(addedPart);
  24.                 }
  25.             }
  26.             finally
  27.             {
  28.                 _lock.ExitWriteLock();
  29.             }
  30.  
  31.             atomicComposition.Complete();
  32.         }
  33.  
  34.         OnChanged(new ComposablePartCatalogChangeEventArgs(addedParts, EmptyPartDefinitions, null));
  35.     }
  36. }

If I need to register known types in runtime, I can just do the following -

Code Snippet
  1. RuntimeTypeCatalog catalog = new RuntimeTypeCatalog();
  2.  
  3. CompositionContainer container = new CompositionContainer(catalog);
  4.  
  5. catalog.Register<Bar>();
  6. catalog.Register<Foo>();

Feel free to download the full source code and see it in action.

Published Sunday, October 17, 2010 4:13 PM by Amir Zuker

תגים:,

Comments

# MEF – Conventional catalog for registering non-attributed parts@ Monday, October 18, 2010 1:38 PM

As part of the task where I had to upgrade an existing WPF project to PRISM 4.0 and work with MEF, one

Zuker On Foundations

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: