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.
Convention can be simple as the following code:
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:
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).
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.