Overriding an Export with a New One

Mar 5, 2010 at 11:48 PM

I could be missing the point...or not quite getting things...but here's my question.

I have my base implementation assembly, which will IMPORT a Config-class (that contains some config values), and an implementation of this exists in the same base-assembly.  This is so I have by default some values to work with.

Then, I want the consumer of this assembly to EXPORT their own version of this Config-class, effectively overriding my default implementation.

What I'm not sure of is how to override the initial import, which is registered within the catalog by default in the base-implementation.  If I let both exist, then I'll get a resolution error when I try to do the import.

What's the approach here?  Thanks (and sorry if this is a misguided question).


[PS: I'm doing this in Silverlight, if that has any impact on the approach].

Mar 10, 2010 at 5:24 AM
Edited Mar 10, 2010 at 5:25 AM

I think you might consider creating a custom MEF "ExportProvider" that encapsulates the logic to filter/select between your Parts.  A search should bring up some articles on this.

Alternatively, you could use ExportManyAttribute and satisfy the Import with multiple (config) Parts.  Then choose between them via metadata, or by data within the Part itself.

-- Alex Hoffman


Mar 10, 2010 at 5:57 AM
Edited Mar 10, 2010 at 5:57 AM

ExportMany? Alex are you working on a part of MEF I don't know about  :-)

You may not need a custom MEF export provider as we support a prioritization of exports with our existing AggregateExportProvider. It would allow overriding as you described. Check this post for more: http://codebetter.com/blogs/glenn.block/archive/2009/05/14/customizing-container-behavior-part-2-of-n-defaults.aspx



Mar 10, 2010 at 7:10 AM

Oops, I meant ImportMany of course.  I must have been channeling Export's exasperation at Import getting all the love


Mar 10, 2010 at 7:16 AM

That's awesome :-) And well said.

Apr 4, 2010 at 6:30 PM

G'day Glenn!

Sorry for the delay - getting back into this now.  Appreciate the explanation around AggregateExportProvider.  Hopefully that'll do it.