Composition Batch

A MEF container instance is not immutable. Changes can happen if the catalog supports changes (like watching a directory for changes) or if your code add or remove parts in runtime. Previously you had to make the changes and invoke the Compose method on the CompositionContainer. In the Preview 4 release we have introduced support for a composition batch.

The batch contains a list of parts to be added and/or removed. After performing the changes, the container automatically triggers a composition which updates recomposable imports affected by the changes.
As a scenario, consider a settings window and a user selecting and deselecting options. Those would map to parts present or not on the container. To apply the batch, you would call the Compose method, as follows:

var batch = new CompositionBatch();
batch.AddPart(partInstance1);
batch.AddPart(partInstance2);
batch.RemovePart(part3);

container.Compose(batch);
Dim batch = New CompositionBatch()
batch.AddPart(partInstance1) 
batch.AddPart(partInstance2) 
batch.RemovePart(part3) 
container.Compose(batch) 

For types that actually use the attributed programming model there are some extension methods on AttributedModelServices for CompositionContainer that allow you to do hide the CompositionBatch in some common cases where it isn't needed.
container.ComposeParts(partInstance1, partInstance2,... ); // creates a CompositionBatch and calls AddPart on all the passed parts followed by Compose
container.ComposeExportedValue<IFoo>(instanceOfIFoo); // creates a CompositionBatch and calls AddExportedValue<T> followed by Compose.
container.ComposeParts(partInstance1, partInstance2,...) ' creates a CompositionBatch and calls AddPart on all the passed parts followed by Compose
container.ComposeExportedValue(Of IFoo)(instanceOfIFoo) ' creates a CompositionBatch and calls AddExportedValue<T> followed by Compose. 

Last edited Aug 9, 2010 at 6:40 PM by haveriss, version 5

Comments

chudq Oct 15, 2009 at 11:24 PM 
It would be nice to use FluentInterface to add and/or remove parts:

var batch =CompositionBatch.AddPart(partInstance1).
AddPart(partInstance2).
RemovePart(part3).GetBatchInstance();