MEF 2.0 – mini series: part 4 (Fluent Import)

2013/01/12

MEF 2.0 – mini series: part 4 (Fluent Import)

this is the 4th post in the MEF 2.0 mini series.
you can see the following TOC for other posts in this series.

in this post I will discuss the Fluent import API.

MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog

we was talking about Fluent Export and Constructor Injection in the previous posts. this post will complete the main scenarios of Import’s fluent API.

I will demonstrate the import technique using the following classes:

Code Snippet
  1. public class Foo
  2. {
  3.     public Logger Log { get; set; }
  4. }
  5.  
  6. public class Logger
  7. {
  8.     public void Write(string message) { /* … */ }
  9. }

we can Export both classes by one of the fluent techniques which we discuss in previous posts, for example:

Code Snippet
  1. var picker = new RegistrationBuilder();
  2. picker.ForType<Logger>()
  3.     .Export<Logger>();
  4. picker.ForType<Foo>()
  5.     .Export();
  6.  
  7. var catalog = new AssemblyCatalog(typeof(Foo).Assembly, picker);
  8. var container = new CompositionContainer(catalog);

but this is not enough, because Foo’s Log property has no [Import] attribute, therefore it will be ignored by the composition.

using the fluent ImportProperties, you can introduce it into the composition :

Code Snippet
  1. picker.ForType<Foo>()
  2.     .Export()
  3.     .ImportProperties<Logger>(pi => pi.Name == "Log");

the property’s Type is referred by the generics parameter while the Lambda used for filtering the target properties.

actually you can take it further and configure the import behaviors.
for example you can set the import to Allow Default:

Code Snippet
  1. picker.ForType<Foo>()
  2.     .Export()
  3.     .ImportProperties<Logger>(pi => pi.Name == "Log",
  4.                 (propInfo, builder) => builder.AllowDefault());

it’s consider as best practice to Allow Default and implement IPartImportsSatisfiedNotification interface which is an interception point which occurs right after the composition.
the idea is to check the composition state within the OnImportsSatisfied()
method in order to verify the composition state.
It is true that MEF exceptions have made better, but it still not perfect and sometimes it is really hard to trace the root cause of the exception. implementation of IPartImportsSatisfiedNotification will make the tracing easier.

so what about Select Many?

let change our Foo class to the following code:

Code Snippet
  1. public class Foo: IPartImportsSatisfiedNotification
  2. {
  3.     public Logger[] Log { get; set; }
  4.  
  5.     public void OnImportsSatisfied()
  6.     {
  7.     }
  8. }

the following code will define the Log property as [ImportMany]:

Code Snippet
  1. picker.ForType<Foo>()
  2.     .Export()
  3.     .ImportProperties<Logger>(pi => pi.Name == "Log",
  4.                 (propInfo, builder) => builder.AsMany());

as you can see we are using the same overload that can manipulate the Import’s behaviors.

by the same technique you can define a re-composition and the creation policy:

Code Snippet
  1. picker.ForType<Foo>()
  2.     .Export()
  3.     .ImportProperties<Logger>(pi => pi.Name == "Log",
  4.                 (propInfo, builder) =>
  5.                 {
  6.                     builder.AsMany();
  7.                     builder.AllowRecomposition();
  8.                     builder.RequiredCreationPolicy(CreationPolicy.NonShared);
  9.                 });

summary

fluent Import is complimentary to fluent Export.
it’s fairly flexible API which enable to assign your own or 3rd party class’s properties.

actually you can take a 3rd party class and inject it’s public properties through those techniques.

Shout it

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. ショッピング2013/11/25 ב 21:11

    and that keeps relating to Two hundred popular music Compact discs (taking ATRAC3 handheld compression).

    Reply