I am using structuremap in my C# application and getting behaviour that I don't understand or expect. Hopefully someone can explain either what I'm doing wrong or why my expectation is incorrect!
When a constructor takes two arguments of the same type, I expect them to be separate instances of the same type - not references to the same instance. Sample app below demonstrates this.
Registry: (not does not use Singleton()
public class DemoRegistry: Registry
{
public DemoRegistry()
{
For<IController>().Use<Controller>();
For<IAnInterface>().Use<AClass>();
}
}
My injected types:
public interface IAnInterface { }
public class AClass : IAnInterface {}
The class that illustrates the issue (I don't expect obj1 to be equal to obj2):
public interface IController {}
public class Controller : IController
{
private readonly IAnInterface _obj1;
private readonly IAnInterface _obj2;
public Controller(IAnInterface obj1, IAnInterface obj2)
{
_obj1 = obj1;
_obj2 = obj2;
bool sameObject = ob1 == obj2; // this is TRUE
}
}
And finally the Main method to run it:
static void Main(string[] args)
{
ObjectFactory.Configure(x => x.AddRegistry<DemoRegistry>());
IController c = ObjectFactory.GetInstance<IController>();
}
I'm using .Net 4.0 and StructureMap 2.5.4.
So to summarise:
- Why does obj1 == obj2 in sample above?
Hopefully this is clear, I'm happy to provide further information if required.