Microsoft.Composition (Portable MEF): Convention

2016/02/06

Microsoft.Composition (Portable MEF): Convention

This post is part of mini series, which complete the full MEF series.

this post will go over Microsoft.Composition conventions.

personally I really like convention over IoC, because it’s lead to

better consistency of the code-base’s naming.

MEF, Import, Export, Portable, NuGet, Microsoft, Composition, Convention,IoC, DI, Dependency

Convention can be simple as the following code:
Code Snippet
  1. var conventions = new ConventionBuilder();
  2. conventions.ForType<Logger>().Export<ILogger>();
  3. conventions.ForTypesDerivedFrom<ISetting>()
  4.             .ExportInterfaces(t => t == typeof(ISetting));
  5. var configuration = new ContainerConfiguration()
  6.     .WithAssembly(typeof(Program).Assembly, conventions);
  7.  
  8. using (var container = configuration.CreateContainer())
  9. {
  10.     container.GetExport<ILogger>();
  11. }
As you can see the convention can be strict as in line 2,
or more general as in line 3,4.
the configuration (line 5,6) define the assembly which the convention relate to.
 
convention can be more complex like registering all classes with is having
service locator constructor of the type CompositionContext (this type is automatically
exported by the MEF and represent the container).
this kind of convention will look like:
Code Snippet
  1. Type locatorType = typeof(CompositionContext);
  2. Type[] locatorTypeParameter = new[] { locatorType };
  3. Func<IEnumerable<ConstructorInfo>, ConstructorInfo> selector =
  4.                 ctors =>
  5.                     (from c in ctors
  6.                         let prms = c.GetParameters()
  7.                         where prms.Length == 1 &&
  8.                             prms[0].ParameterType == locatorType
  9.                         select c)
  10.                     .First();
  11.  
  12. var conventions = new ConventionBuilder();
  13. conventions.ForTypesMatching(t => t.GetConstructor(locatorTypeParameter) != null)
  14.     .SelectConstructor(selector)
  15.                 .ExportInterfaces()
  16.                 .Shared();
Line 3-10, define query which is looking for constructor with 
           single parameter of type CompositionContext.
Line 13 register any type that has constructor with single parameter 
        of type CompositionContext.
Line 14: specify the constructor which should be used for the composition
     (in case that the class has multiple constructors).
Line 15: register all interface of the class.
Line 16: set the instantiation strategy to singleton (this is not
         the default anymore).
 

Summary:

as you can see the portable MEF is quite alike the full MEF version with some refactoring.

forward on this mini series I will speak about import, property export and scoping.

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=""> <s> <strike> <strong>

*

one comment

  1. Andreas Beyer2016/02/09 ב 11:45

    Hi Bnaya! Thanks for all your posts about MEF (1,2 and lightweight/portable). Working with MEF some years now your blog was always a good starting point if I ran in some problems.
    I’m looking forward on your scoping part in your mini series. Maybe you can prepone this topic a little bit 🙂
    Thanks!

    Reply