I want a new instance of my object for each ObjectFactory.Getinstance() call with StructureMap. I haven’t been able to find it or figure it out myself.
AlwaysUnique isn’t doing it.
[TestMethod]
public void GetConcreteInstanceOf_ShouldReturn_DifferentInstance()
{
ObjectFactory.Initialize(registry =>
{
// setup the singleton, so that it's new every time
registry.For<ISystemData>().AlwaysUnique().Use(new SystemDataClient());
});
ISystemData result = ObjectFactory.GetInstance<ISystemData>();
ISystemData result2 = ObjectFactory.GetInstance<ISystemData>();
Assert.AreNotSame(result, result2);
}
If you want a new instance every time, then you don’t want a singleton, by definition. Instead of passing an instance, just specify the concrete type, and StructureMap’s default behavior will give you a new instance each time: