I am trying to configure Structuremap something like the following but I can seem to get it quite right
ObjectFactory.Initialize(x => {
x.For<TunaRepository>()
.Use(new TunaRepository(serviceEndpoint))
.Named("Tuna");
x.For<CodRepository>()
.Use(new CodRepository(serviceEndpoint))
.Named("Cod");
x.For<HaddockRepository>()
.Use(new HaddockRepository(serviceEndpoint))
.Named("Haddock");
x.For<IFishRepository>().AddInstances(y =>
{
y.OfConcreteType<TunaRepository>().
// Somehow add all instances here???
});
x.For<TunaController>().Use<TunaController>()
.Ctor<IFishRepository>("repo").Is<TunaRepository>(); // This Is should use the instance registered above
x.For<CodController>().Use<CodController>()
.Ctor<IFishRepository>("repo").???
x.For<HaddockController>().Use<HaddockController>()
.Ctor<IFishRepository>("repo").???
});
At points in my code I want to be able to do:
var instances = ObjectFactory.GetAllInstances<IFishRepository>();
and also:
var instance = ObjectFactory.GetNamedInstance<IFishRepository>("Cod");
and also use IFishRepository as an arguement to my controllers:
public TunaController(IFishRepository repo ...
So my question is whats the best way to configure my ObjectFactory?