MEF 2.0 – mini series: part 5 (Fluent export properties)

2013/01/15

MEF 2.0 – mini series: part 5 (Fluent export properties)

this is the 5th 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 cover the fluent property’s export.

MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog

Exporting properties is a less known feature of MEF.
MEF 1 was supporting this feature by using the attribute model.
you could decorate a property with a [Export] attribute and then it become available for imports.

the following code demonstrate property exporting in MEF 1:

the Foo class is importing multiple SymmetricAlgorithm:

Code Snippet
  1. [Export]
  2. public class Foo
  3. {
  4.     [ImportMany]
  5.     public SymmetricAlgorithm[] CryptoProviders { get; private set; }
  6. }

and the CryptoComposer class is exporting a few symmetric algorithms:

Code Snippet
  1. [Export]
  2. public class CryptoComposer
  3. {
  4.     [Export]
  5.     public SymmetricAlgorithm Aes { get { return new AesManaged(); } }
  6.     [Export]
  7.     public SymmetricAlgorithm TripleDES { get { return new TripleDESCryptoServiceProvider(); } }
  8.     [Export]
  9.     public SymmetricAlgorithm RC2 { get { return new RC2CryptoServiceProvider(); } }
  10. }

with the following composition the Foo instance will have the CryptoComposer’s symmetric algorithms:

Code Snippet
  1. var catalog = new AssemblyCatalog(typeof(Program).Assembly);
  2. var container = new CompositionContainer(catalog);
  3.  
  4. var foo = container.GetExportedValue<Foo>();

so, how do we do it with the fluent export API?

to keep it simple I will leave the Foo class with the attribute model (we learned how to use fluent import in the previous post):

Code Snippet
  1. [Export]
  2. public class Foo
  3. {
  4.     [ImportMany]
  5.     public SymmetricAlgorithm[] CryptoProviders { get; private set; }
  6. }
  7.  
  8. public class CryptoComposer
  9. {
  10.     public SymmetricAlgorithm Aes { get { return new AesManaged(); } }
  11.     public SymmetricAlgorithm TripleDES { get { return new TripleDESCryptoServiceProvider(); } }
  12.     public SymmetricAlgorithm RC2 { get { return new RC2CryptoServiceProvider(); } }
  13. }

in order to export the CryptoComposer’s properties you should use the following code:

Code Snippet
  1. var picker = new RegistrationBuilder();
  2. picker.ForType<CryptoComposer>()
  3.     .ExportProperties(p => p.PropertyType == typeof(SymmetricAlgorithm));
  4.  
  5. var catalog = new AssemblyCatalog(typeof(Program).Assembly, picker);
  6. var container = new CompositionContainer(catalog);
  7.  
  8. var foo = container.GetExportedValue<Foo>();

summary

exporting properties using the fluent API is fairly straightforward.

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