0
我想配置Structuremap像下面這樣,但我似乎可以得到它完全正確幫助structuremap配置
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").???
});
在我的代碼點我希望能夠做到:
var instances = ObjectFactory.GetAllInstances<IFishRepository>();
也:
var instance = ObjectFactory.GetNamedInstance<IFishRepository>("Cod");
,還可以使用IFishRepository作爲arguement到我的控制器:
public TunaController(IFishRepository repo ...
所以我的問題是什麼的最佳方式來配置我的ObjectFactory?
感謝您的信息。 (lambda參數有一個輸入arg雖然:)) – Dve 2011-05-17 08:58:59