Skip to content

Instantly share code, notes, and snippets.

@DanTup
Created May 7, 2012 15:05
Show Gist options
  • Save DanTup/2628256 to your computer and use it in GitHub Desktop.
Save DanTup/2628256 to your computer and use it in GitHub Desktop.
StructureMap SingleImplementationsOfInterface bug
namespace StructureMapBugRepro
{
using System;
using StructureMap;
using StructureMapBugRepo.NS1;
using StructureMapBugRepo.NS2;
public class Program
{
static void Main(string[] args)
{
ObjectFactory.Initialize(i =>
{
// Add our two registries. One is shared, the other is specific to this project.
// NOTE: Changing the order of these two will determine which GetInstance call fails.
i.AddRegistry<SharedRegistry>();
i.AddRegistry<MyRegistry>();
});
// One of these will, depending on the order of the two AddRegistry calls above.
Console.WriteLine(ObjectFactory.GetInstance<IShared>().GetType().Name);
Console.WriteLine(ObjectFactory.GetInstance<IMine>().GetType().Name);
}
}
}
namespace StructureMapBugRepo.NS1
{
using StructureMap.Configuration.DSL;
public class SharedRegistry : Registry
{
public SharedRegistry()
{
Scan(s =>
{
// For this sample, we're using Namespaces, but in my real project,
// it's two different assemblies, and has the exact same issue.
s.TheCallingAssembly();
s.IncludeNamespaceContainingType<IShared>();
s.SingleImplementationsOfInterface();
});
}
}
// Dummy class/interfaces just for sample.
public interface IShared { }
public class Shared : IShared { }
}
namespace StructureMapBugRepo.NS2
{
using StructureMap.Configuration.DSL;
public class MyRegistry : Registry
{
public MyRegistry()
{
Scan(s =>
{
// For this sample, we're using Namespaces, but in my real project,
// it's two different assemblies, and has the exact same issue.
s.TheCallingAssembly();
s.IncludeNamespaceContainingType<IMine>();
s.SingleImplementationsOfInterface();
});
}
}
// Dummy class/interfaces just for sample.
public interface IMine { }
public class Mine : IMine { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment