MEF 2.0 – mini series: part 1 (Open Generics)

2013/01/04

MEF 2.0 – mini series: part 1 (Open Generics)

this is the first post of a new mini series about MEF 2.0.

MEF 2.0 is a reflection of a community requests.
features like Open Generics, fluent and conventional discovery, lifetime handling of the part, better exception handling and more, was all requested by the community.

each post of this mini series will target a single enhancement.

MEF, Import, Export, CompositionContainer, RegistrationBuilder, Catalog, Generics

Open Generics

one of the most annoying missing feature in MEF 1 was the lack of support for Open generics.

the following code (which is fully functioning in MEF 2.0) didn’t worked in MEF 1.

Code Snippet
  1. public class Foo
  2. {
  3.     [Import]
  4.     public EventAggregator<int> IntAggregator { get; set; }           
  5. }
  6.  
  7. [Export]
  8. public class EventAggregator<T>
  9. {
  10.     public event Action<T> Notify = (item) => { };
  11.     public void Send(T item)
  12.     {
  13.         Notify(item);
  14.     }
  15. }

as obvious as it may seem, this piece of functionality is missing in MEF 1.
if you try to do it in MEF 1 it will look like the following code:

Code Snippet
  1. public class Foo
  2. {
  3.     [Import]
  4.     public EventAggregatorInt IntAggregator { get; set; }           
  5. }
  6.  
  7. [Export]
  8. public class EventAggregatorInt : EventAggregator<int> { }
  9.  
  10. public class EventAggregator<T>
  11. {
  12.     public event Action<T> Notify = (item) => { };
  13.     public void Send(T item)
  14.     {
  15.         Notify(item);
  16.     }
  17. }

it is quite obvious that extra EventAggregatorInt class shouldn’t have be been there in the first place.

Summary

Open Generics if fairly straight forward and it is keeping our code smaller and better. 

Shout it

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

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>