From the official MEF documentation:
A Composable Part should contain at least one export.
Composable Parts are either added to the container explicity or created through the use of catalogs.
The default catalogs that MEF ship with identify Composable Parts through the presence of an export attribute.
Does this mean that this snippet form the same documentation would not work, when using the default catalogs, since it doesn't export anything?
class Program
{
[Import]
public IMessageSender MessageSender { get; set; }
}
I find it hard to believe that all classes participating in the MEF mix, must all 'bring food to the table', even if they just want to 'consume'.
The Program
above being a simple example: there is nothing that this class could add to the MEF mix.
Secondly,
How are Parts being added "explicitly to the container"?
The documentation of e.g. CompositionContainer doesn't help me any further.
Thanks in advance Jan
A composable part is a part that can be imported into another part. The example here:
class Program
{
[Import]
public IMessageSender MessageSender { get; set; }
}
Program
is not a composable part. It iself does not get automatically imported into another part. Your exports for IMessageSender
are composable parts.
You can add parts explicity to the container using a CompositionBatch
, which allows you to add Export
and ComposablePart
instances to your container explicitly, or through extension you can add raw values:
var user = new User() { Name = "Matt" };
var batch = new CompositionBatch();
batch.AddExportedValue(user);
CompositionContainer.Compose(batch);
Where the final call is adding your parts to the container.