Managed Extensibility Framework 2: Upcoming Changes for .NET 4.5
Version 2 of the Managed Extensibility Framework (MEF) is currently in Preview mode, and portions of it are now available in .NET Framework 4.5 Beta. According to Microsoft, the final supported release of MEF 2 will be included with .NET Framework 4.5, which is expected later this year.
In anticipation of its release, Dean Oliver detailed several of the updates in MEF 2. (As he mentions in his article, these items are subject to change before the final release.) A few of these updates include:
RegistrationBuilder: Attribute-less Registration
RegistrationBuilder is a new class in System.ComponentModel.Composition.Registration that handles part creation and export registration automatically, so that the developer no longer needs to define attributes on objects for injection. The RegistrationBuilder uses rules (called 'conventions') to map types to parts. For unique cases, it's also possible to override these conventions.
Flexible Type Registration
When using RegistrationBuilder, there are three ways to determine which types a rule will apply to:
(): selects a single type
(): selects types that are assignable to either a base class or an interface (the same as InheritedExport in MEF 1)
predicate): selects types that match a Boolean selector. This is the most flexible means of selection.
Multiple rules can be applied to a type, and will all be applied as long as none of the rules are in conflict.
Finer-Grained Control Over Lifetime
MEF 1 has two options for lifetime: per instance, or a shared global lifetime. ExportFactory
According to Oliver:
Instead of having the object generated when the parent object is generated you can gain access to the instance only when needed. ... This is sort of like a scoped import for a unit of work.
Microsoft is still soliciting feedback and feature suggestions for both MEF 2 and 3 on the MEF Discussion forum at CodePlex. For more information on recent changes in MEF 2, please visit the BCL Team Blog.
Brandon Holt, Preston Briggs, Luis Ceze, Mark Oskin May 21, 2015
Kai Kreuzer, Olaf Weinmann May 21, 2015