How to remove duplicate parts

May 12, 2011 at 9:54 AM

Hello there,

I have the following scenario in which I have the same assembly on different directories.

Then, I add each and every directory to the DirectoryCatalog and finally on composition I have multiple instances of the same assembly.

It may be a design problem, but I am wondering is there any way to remove the duplicates.

 

With Best Regards,

Rao.

May 18, 2011 at 1:47 PM

Then, I add each and every directory to the DirectoryCatalog

AFAIK it is not possible to add multiple directories to a DirectoryCatalog. A DirectoryCatalog corresponds to exactly one directory, i.e. DirectoryCatalog.FullPath. Therefore I'll assume that you are actually adding multiple DirectoryCatalog instances to an AggregateCatalog.

and finally on composition I have multiple instances of the same assembly.

I see three possible solutions:

1. Get rid of the duplicate assemblies.

2. Don't use DirectoryCatalog. Instead, scan the directories yourself and create an AssemblyCatalog for each assembly that you want to include. You could include some logic to filter out duplicates. Finally, add each AssemblyCatalog to a single AggregateCatalog.

3. Create separate export providers for each directory catalog. The order of the export providers will determine their priority in the container. For example:

using (var catalog1 = new DirectoryCatalog(...))
using (var catalog2 = new DirectoryCatalog(...))
{
    provider1 = new CatalogExportProvider(catalog1);
    provider2 = new CatalogExportProvider(catalog2);
    using (var container = new CompositionContainer(provider1, provider2)
    {
        provider1.SourceProvider = container;
        provider2.SourceProvider = container;
        var program = container.GetExportedValue<Program>();
        program.Run();
    }
}