Add an export metadata to all parts

Jan 3, 2012 at 7:00 AM

This question is somewhat related to my previous question.

In one of my projects I have a requirement to add an export metadata to ALL exported parts based on some rules. In convention-based part registration If I want to add an export metadata to a part I should specify that metadata as an export configuration (built by ExportBuilder) and pass it to PartBuilder's Export method :

  .Export<Interface1>(c => c.AddMetadata("Name", "Value"));

But when I want to add an export metadata to all parts this won't be a good solution and because the rule I use to add this metadata to parts is not simple and needs resolving some services this can't be nicely simplified by extension methods or something.

One way I thought of is using part metadata instead of export metadata. Because part metadata can be added to all parts easily and I can put all the codes in one place :

rb.ForTypesMatching(t => true)
  .AddMetadata("Name", "Value");

But I need export metadata not part metadata. I thought I could create a custom catalog wrapper and in its GetExports method change the ExportDefinition's Metadata property of all exports I got from the wrapped catalog (based on their part metadata). But I face another problem: the Metadata property of ExportDefinition is a readonly dictionary and of course I can't change the whole ExportDefinition with another one with modified Metadata because I get an exception saying "definition did not originate from the ExportDefinitions property on this ComposablePart or its ComposablePartDefinition"

Any idea how can I achieve the same result without these headaches? Maybe I should create a customized CatalogExportProvider instead of PartCatalog?