MEF Export

May 5, 2010 at 5:43 PM
Hi, I'm new to MEF and was wondering, when importing MEF components into the catalog and added an import as a property (or whatever) in a class, if there are two component implementing the requested import contract, how do you know (or specify etc) which one is loaded? Cheers, Simon
May 6, 2010 at 3:33 AM
Edited May 6, 2010 at 8:28 PM
This is where metadata comes in. With MEF's metadata support you can import multiple contracts in a lazy manner, decide which o e you want via metadata and then grab that instance,
F

On May 5, 2010, at 10:43 AM, "s0upyd" <notifications@codeplex.com> wrote:

From: s0upyd

Hi, I'm new to MEF and was wondering, when importing MEF components into the catalog and added an import as a property (or whatever) in a class, if there are two component implementing the requested import contract, how do you know (or specify etc) which one is loaded? Cheers, Simon
May 6, 2010 at 8:13 PM

Ah, ok. Thanks for the answer. I am just starting to touch on the metadata stuff, so it's nice to have some qualification on what this part of MEF is for.

The link you gave seems very informative, so I will follow that pattern to get what I need done.

As an aside, assuming I follow your advice, I have a collection with all of the metadata descriptors for a given contract. How would one typically approach presenting a UI to the user so they can choose the one they want? I guess this boils down to displaying a list or combo box with some information from the metadata, they choose one and this is what is used when matching from the collection? Taking this theory forward, you'd have to defend against the user's default choice disappearing from the components on a future execution?

I think what MEF offers is pretty cool, I can definately see some uses for it, but is it intended that an application has to provide this extra management/configuration stuff?

Again, thanks for the quick informative answer.

Si

May 6, 2010 at 8:23 PM

Sent from your iPad!?!

May 6, 2010 at 8:27 PM

Just forget you saw that :-)

Jun 26, 2010 at 5:20 AM

Glenn,

What about the situation where assembly A imports some implementation of an interface, but assembly A also exports a default implementation.  Perhaps assembly B wants to consume assembly A but export its own implementation of the interface.  Assembly B cannot insert logic into assembly A, to examine metadata and make the decision at runtime, right?  What can asembly B do in this instance?

Jun 28, 2010 at 9:40 PM

This is not unusual. You can implement a custom ExportProvider that defines that all exports coming from your assembly (B) takes lower priority.

Jun 29, 2010 at 5:36 PM

Thanks, I'll look into that. 

Jun 29, 2010 at 6:00 PM

You don't even necessary need a custom export provider to do that. You can configure MEF container's with 'default catalogs' to handle that. http://codebetter.com/blogs/glenn.block/archive/2009/05/14/customizing-container-behavior-part-2-of-n-defaults.aspx