1
Vote

Add Parameter to RegisterCatalog to add UNIQUE parts

description

A suggestion that I believe would be beneficial to a lot of developers, would be to add the ability to register the parts of the Aggregate catalog, ignoring duplicates.
 
The issue is, that when I register my exported parts, I get duplicate registrations for the same Export - leading me to the exception, "ImportCardinalityMismatchException", when I try to Resolve<T>().
 
There isn't an easy work around that I have found and I believe it would be nice to have as part of this package.
 
Code sample below:
 
    private static void RegisterAssemblies(string assemblyPath, string searchPattern)
    {
        string[] directories = Directory.GetDirectories(assemblyPath, "Debug", SearchOption.AllDirectories);
        var exportedParts = new AggregateCatalog();
 
        foreach (string directory in directories)
        {
            var assemblyCatalog = new DirectoryCatalog(directory);
            exportedParts.Catalogs.Add(assemblyCatalog);
        }
 
        _container.RegisterCatalog(exportedParts);
    }

comments

montijr2007 wrote Jul 5, 2012 at 7:53 PM

Here is what I believe would work- Add this to the UnityContainerExtensions source code for the "RegisterCatalog" method:

public static void RegisterCatalog(this IUnityContainer unityContainer, ComposablePartCatalog catalog)
{
  lock (unityContainer)
  {
       var compositionIntegration = EnableCompositionIntegration(unityContainer);
       compositionIntegration.Catalogs.Add(catalog);

       IEnumerable<ComposablePartCatalog> parts = compositionIntegration.Catalogs.Distinct().ToList();
      compositionIntegration.Catalogs.Clear();

       foreach (var part in parts)
       {
           compositionIntegration.Catalogs.Add(part);
       }

   }
}

wrote Feb 14, 2013 at 8:47 PM