tags:

views:

100

answers:

2

So intead of using ExportMetadata attribute, why not just extend the interface instead?

+1  A: 

Good question. I think that extending the interface is the correct thing to do whenever you have the option. It much better communicates intent.

However, we may need more fine-grained control over interfaces that we don't own. In those cases we can use ExportMetadata as a substitute.

In my opinion you should only use ExportMetadata when there is no other option available to you.

Mark Seemann
+4  A: 

if you import Lazy<> stuff via MEF you can look into the metadata without instantiate your object.

blindmeis
This is the answer. Metadata can be read without actually instantiating the object. This can be pretty useful sometimes...
Tim
So? Instead you instantiate a bunch of `Lazy<T>` objects. Sounds like you are sacrificing premature micro-optimization for clean design. Not a good trade-off IMO.
Mark Seemann
@Mark Visual Studio has catalogs that can save all the metadata information for parts, and later the catalog can be queried without having to load the assemblies. This means that using Lazy<T> not only avoids instantiating the object, but avoids loading the assembly entirely. Since the metadata is used to choose which part of many should be activated, this helps the performance significantly for Visual Studio. Smaller systems usually don't need to worry about this kind of thing and in those cases adding the information to the interface instead of the MEF metadata can be a better choice.
Daniel Plaisted
+1 @daniel plaisted
blindmeis
That metadata in those catalogs doesn't get there magically. It gets there because attributes are queried via reflection. When you query attributes, you interact with Attribute instances, so objects still end up being instantiated - not to mention the `Lazy<T>` instances and the metadata instances when you query the metadata. It *may* be slightly faster, but I still consider it a micro-optimization... and I don't buy the VS example: VS 2010 takes significantly more time to start than VS 2008.
Mark Seemann
Mark, the metadata is serialized into the catalog at build time too. VS startup performance is a very complex thing, and Daniel is the one who has the numbers.
Nicholas Blumhardt
VS2010 was a complete re-write, so its start time is hardly evidence. One project I'm working on, the interface is not in my assembly so metadata is perfect.
Tim