AddMetadata

Nov 26, 2011 at 6:22 AM
Edited Nov 26, 2011 at 6:23 AM

I have two questions about adding and retrieving metadata in convention-based part registration:

First:

What's the difference between PartBuilder.AddMetadata and ExportBuilder.AddMetadata?

When I use PartBuilder.AddMetadata I can't get that metadata by using ImportAttribute or calling GetExports but ExportBuilder.AddMetadata works fine.

Second:

Is it possible to add metadata by attributes to a part while using conventions to export that part? (I mean combining convention-based part registration with attribute-based metadata).

Nov 28, 2011 at 3:42 PM

Hello,

Your first item – PartBuilder.AddMetadata() creates “part metadata” which is otherwise specified using the [PartMetadata] attribute. It is useful for catalog-level operations like filtering. ExportBuilder.AddMetadata() creates “export metadata” which is the kind that you are accessing via imports. Part metadata is much less commonly used than export metadata, so for most purposes you should be able to ignore it.

Second item – no, this isn’t supported. You can use custom export/metadata attributes to achieve very similar results however.

Regards,

Nick

From: Palomino [email removed]
Sent: Friday, November 25, 2011 11:23 PM
To: Nicholas Blumhardt
Subject: AddMetadata [MEF:280878]

From: Palomino

I have two questions about adding and retrieving metadata in convention-based part registration:

First:

What's the difference between PartBuilder.AddMetadata and ExportBuilder.AddMetadata?

When I use PartBuilder.AddMetadata I can't get that metadata by using ImportAttribute or calling GetExports but ExportBuilder.AddMetadata works fine.

Second:

Is it possible to add metadata by attributes to a part while using conventions to export that part? (I mean combining convention-based part registration with attribute-based metadata).

Jan 2, 2012 at 8:22 AM
Edited Jan 2, 2012 at 8:37 AM

Thanks for your answer.

About my second question I have a scenario which I think needs combining convention-based part registration with attribute-based metadata. I want to have prioritized composition, i.e. if there are multiple exports which satisfies a single import the one with highest priority is chosen. I could create a custom catalog to filter multiple exports based on their priority metadata. But the problem is assigning the metadata to each export. If I use convention-based part registration, e.g export all classes implementing specific interfaces, I can't use attributes to add priority metadata to a part on demand. One way is defining a custom export/metadata attribute as you've suggested, but in this way I should define an attribute for each convention rule which makes convention-based registration meaningless. I want to have just one priority attribute and add it to a part only if it has a non-default priority (one of its usage is overriding framework's default implementations in modules).

I thought I could define an ordinary attribute and in a custom catalog GetExports method use that attribute to filter exports, but the problem is that I have no access to the types of parts in my custom catalog. ComposablePartDefinition class provides no access to the type of composable part so I can't get its attributes.

I finally found a solution for this problem. I added a part metadata to all parts storing its type and used that part metadata in my custom catalog. But I think there should be a better solution for my requirement. I would be grateful if somebody has a better workaround for this problem. Is there any chance to support convention-based part registration with attribute-based metadata in future releases or any type of built-in prioritized composition? cause I think it can be really useful.