This project has moved. For the latest updates, please go here.
Microsoft.Composition is an update for MEF that is optimized for web and Windows 8 Store apps. It is distributed as a NuGet package compatible with the .NET 4.5 Framework and .NET for Windows Store apps.

These environments do not emphasize in-place third party extensibility, and so composition in these environments can be simplified and streamlined.

Microsoft.Composition includes a lifetime model oriented towards the 'unit of work' patterns that appear in server-side and MVVM-style applications.

Throughput under server workloads is several orders of magnitude higher than what can be achieved with CompositionContainer, and with almost zero contention.

Getting Started

First, install the Microsoft.Composition NuGet package using the Manage NuGet Packages dialog in Visual Studio, or from the Package Manager Console:

PM> Install-Package Microsoft.Composition -Pre


This will add a number of assemblies to the project.

Parts for Microsoft.Composition use the familiar MEF attributes, found in the System.Composition namespace.

[Export(typeof(IMessageHandler))]
public class MessageHandler : IMessageHandler
{
    [ImportingConstructor]
    public MessageHandler(IDatabaseConnection connection)
    {
    }
}


The container is created using a simple ContainerConfiguration class.

var configuration = new ContainerConfiguration()
    .WithAssembly(typeof(MessageHandler).Assembly);

using (var container = configuration.CreateContainer())
{
    var greeter = container.GetExport<IMessageHandler>();
    greeter.Greet();
}


ContainerConfiguration provides the methods WithPart() and WithAssembly() as well as the WithProvider() method for plugging in container extensions.

Conventions are defined in Microsoft.Composition using ConventionBuilder:

var conventions = new ConventionBuilder();
conventions.ForTypesDerivedFrom<IMessageHandler>().Export();


The WithPart() and WithAssembly() configuration methods accept conventions as parameters:

configuration.WithPart<MessageHandler>(conventions);


ConventionBuilder supports the same syntax and rules as RegistrationBuilder does in the full-framework version of MEF. The BCL Team Blog discusses how to define and override conventions.

If you are familiar with MEF in the .NET Framework 4.0 and onwards, see the migration guide for further information.

Last edited Oct 18, 2012 at 6:17 PM by alokshriram, version 5

Comments

Veleek Jul 14, 2016 at 11:56 PM 
Use typeof(X).GetTypeInfo().Assembly

JCKodel Sep 25, 2013 at 5:34 PM 
Windows Store App Class Libraries doesn't have the typeof(X).Assembly property!