Permalink
Browse files

Unity components always take precedence over MEF components when quer…

…ying for components
  • Loading branch information...
pwlodek committed Jun 18, 2011
1 parent 49d5ed5 commit 035c9ec7b364669fb2be05b30a53bbc3eb1f824f
@@ -74,6 +74,7 @@
<Compile Include="FakeMefComponents.cs" />
<Compile Include="FakeUnityComponents.cs" />
<Compile Include="LazyResolutionTests.cs" />
<Compile Include="ResolutionOrderTests.cs" />
<Compile Include="UnityContainerExtensionsTests.cs" />
<Compile Include="ContainerExportProviderTests.cs" />
</ItemGroup>
@@ -0,0 +1,75 @@
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using Microsoft.Practices.Unity;
using NUnit.Framework;
namespace MefContrib.Integration.Unity.Tests
{
[TestFixture]
public class ResolutionOrderTests
{
[Test]
public void Unity_registered_components_take_precedence_over_MEF_registered_components_if_querying_for_a_single_component_registered_in_both_containers()
{
// Setup
var unityContainer = new UnityContainer();
var typeCatalog = new TypeCatalog(typeof (Singleton));
// Register catalog and types
unityContainer.RegisterCatalog(typeCatalog);
unityContainer.RegisterType<ISingleton, Singleton>(new ContainerControlledLifetimeManager());
// Reset count
Singleton.Count = 0;
Assert.That(Singleton.Count, Is.EqualTo(0));
var singleton = unityContainer.Resolve<ISingleton>();
Assert.That(singleton, Is.Not.Null);
Assert.That(Singleton.Count, Is.EqualTo(1));
var mef = unityContainer.Resolve<CompositionContainer>();
var mefSingleton = mef.GetExportedValue<ISingleton>();
Assert.That(Singleton.Count, Is.EqualTo(1));
Assert.That(singleton, Is.SameAs(mefSingleton));
}
[Test]
public void When_querying_MEF_for_a_multiple_components_registered_in_both_containers_all_instances_are_returned()
{
// Setup
var unityContainer = new UnityContainer();
var typeCatalog = new TypeCatalog(typeof(Singleton));
// Register catalog and types
unityContainer.RegisterCatalog(typeCatalog);
unityContainer.RegisterType<ISingleton, Singleton>(new ContainerControlledLifetimeManager());
// Reset count
Singleton.Count = 0;
Assert.That(Singleton.Count, Is.EqualTo(0));
var mef = unityContainer.Resolve<CompositionContainer>();
mef.GetExportedValues<ISingleton>();
Assert.That(Singleton.Count, Is.EqualTo(2));
}
}
[Export(typeof(ISingleton))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class Singleton : ISingleton
{
public static int Count;
public Singleton()
{
Count++;
}
}
public interface ISingleton { }
}
@@ -83,9 +83,16 @@ protected override void Initialize()
private CompositionContainer PrepareCompositionContainer()
{
// Create the MEF container based on the catalog
var container = new CompositionContainer(this.aggregateCatalog, this.providers);
// Create the MEF container based on the catalog and given providers
// Important: the catalog is wrapped with CatalogExportProvider which is
// then added as a LAST catalog, this ensures that when querying Unity/MEF
// for a single component, Unity components will always take precedence
var catalogExportProvider = new CatalogExportProvider(this.aggregateCatalog);
var providerList = new List<ExportProvider>(this.providers);
providerList.Add(catalogExportProvider);
var container = new CompositionContainer(providerList.ToArray());
catalogExportProvider.SourceProvider = container;
// If desired, register an instance of CompositionContainer and Unity container in MEF,
// this will also make CompositionContainer available to the Unity
if (Register)

0 comments on commit 035c9ec

Please sign in to comment.