Import only one of the available Extension

Dec 1, 2010 at 8:46 PM
Edited Dec 1, 2010 at 10:14 PM

Hello,

I am learning MEF and I am kind of stuck at one point.

In my application, I have written 2 extensions 1) Basic Calculation and 2) Customized Calculation, both implementing ICalculate interface. Now what I want to do is that by default my application should use "Basic calculation" extension BUT when the "Customized Calculation" is available, it should use the Customize one...and not the base one.

Can anyone guide me how should i write the import statments to load only one of the available extensions?

many thanks!

Coordinator
Dec 1, 2010 at 11:22 PM

Check out this blog post: Import Cardinality, and Picking Which Export to Use.  It gives several options for how you can handle this.

Thanks,
Daniel 

Dec 1, 2010 at 11:25 PM
Edited Dec 1, 2010 at 11:27 PM

If you pass multiple export provider to a container, the container will query each export provider in turn when it looks for an export. The first export provider to provide the part, wins. In the following example the exports provided by assemblies in the "customized" subfolder override the exports provided by assemblies in the executable's folder.

var defaultExportProvider = new DirectoryCatalog(".","*");
var customizedExportProvider = new DirectoryCatalog(@".\customized");
var container = new CompositionContainer(customizedExportProvider, defaultExportProvider);
defaultExportProvider.SourceProvider = container;
customizedExportProvider.SourceProvider = container;



        
    
Dec 2, 2010 at 4:44 PM
Edited Dec 2, 2010 at 9:57 PM

Hello Wim and  Daniel,

Thank you so much for your prompt reply.

@Wim - using your adivce I was able to load the customized Extension before the default one, but it still loaded both of them. What I want is...if the customized extension is available, then the defaultExtension should not be loaded.

I just changed my Import declaration from ImportMany to Import and its now loading only the customize export which is what I wanted.

A big thanks again for your reply and guidance

Dec 2, 2010 at 5:52 PM
Edited Dec 2, 2010 at 9:58 PM
wcoenen wrote:

If you pass multiple export provider to a container, the container will query each export provider in turn when it looks for an export. The first export provider to provide the part, wins. In the following example the exports provided by assemblies in the "customized" subfolder override the exports provided by assemblies in the executable's folder.

var defaultExportProvider = new DirectoryCatalog(".","*");
var customizedExportProvider = new DirectoryCatalog(@".\customized");
var container = new CompositionContainer(customizedExportProvider, defaultExportProvider);
defaultExportProvider.SourceProvider = container;
customizedExportProvider.SourceProvider = container;

 I just changed my Import declaration from ImportMany to Import and its now loading only the customize export which is what I wanted.

A big thanks again for your reply and guidance.