MEF – Best Practice

2011/07/27

MEF – Best Practice

In this post I will offer MEF practice that may ease your MEF life.

one of the MEF headache is to understand what’s goes wrong,

when MEF reject the composition, you can read more about

MEF diagnostic in this post. 

MEF, Import, Export, Import Many, Best practice

Most of the obscure loading failure happens when your Export dependencies doesn’t

satisfied. in those case you solely relay on the MEF error message which is not enough

in cases of deep dependencies graph.

It will normally occurs when you are using the Import attribute (not ImportMany).

 

let take the following test case:

Code Snippet
  1. public interface IFoo
  2. {
  3.     void Write();
  4. }
  5.  
  6. [Export(typeof(IFoo))]
  7. public class Foo : IFoo
  8. {
  9. }
  10.  
  11. public class Bar
  12. {
  13.     [Import]
  14.     public IFoo Foo { get; private set; }
  15. }

class Bar importing IFoo, which mean that the composition will fail whenever

MEF find no valid IFoo export or find more than single IFoo export.

 

we can use 2 patterns:

  • [ImportMany] which will handle both the case of no export and too many export.
  • [Import(AllowDefault=true)] which cover the case of no export.

using ImportMany and the IPartImportsSatisfiedNotification pattern.

we can validate the composition right after the composition stage.

Code Snippet
  1. public class Bar : IPartImportsSatisfiedNotification
  2. {
  3.     public IFoo Foo { get; private set; }
  4.  
  5.     [ImportMany]
  6.     private IFoo[] FoosInternal { get; set; }
  7.  
  8.     public void OnImportsSatisfied()
  9.     {
  10.         if (FoosInternal == null || FoosInternal.Length == 0)
  11.             throw new Exception("MEF composition find no IFoo");
  12.         if (FoosInternal.Length > 1)
  13.             throw new Exception("MEF composition find to many IFoo");
  14.     }
  15. }

at the OnImportsSatisfied method you can check whether

you got the composition right (instead of letting the MEF infrastructure

doing this job).

 

in most cases it is enough to ignore the to many IFoo composition case

and to apply the [Import(AllowDefault=true)] pattern.

Code Snippet
  1. public class Bar : IPartImportsSatisfiedNotification
  2. {
  3.     [Import(AllowDefault=true)]
  4.     public IFoo Foo { get; private set; }
  5.  
  6.     public void OnImportsSatisfied()
  7.     {
  8.         if (Foo == null)
  9.             throw new Exception("MEF composition find no IFoo");
  10.     }
  11. }

after the composition we can validate whether IFoo composition goes right.

 

Summary

MEF failure at the composition phase can be frustrating.

in this post I have tried to offer pattern to ease that pain.

 

Shout it kick it on DotNetKicks.com

Digg This
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>