我必须使用InjectionConstructor声明所有参数

时间:2014-04-19 16:41:00

标签: c# unity-container

我需要解决对我的类构造函数“UnitOfWork”的各种调用,如示例中所示:

container.RegisterType<IDbContext, NorthwindContext>("NorthwindContext");
container.RegisterType<IDbContext, NorthwindCustomerContext>("NorthwindCustomerContext");

container.RegisterType<IUnitOfWork, UnitOfWork>(
    "NorthwindUnitOfWork", 
    new InjectionConstructor(container.Resolve<IDbContext>("NorthwindContext")));

container.RegisterType<IUnitOfWork, UnitOfWork>(
    "NorthwindCustomerUnitOfWork", 
    new InjectionConstructor(container.Resolve<IDbContext>("NorthwindCustomerContext")));

但是现在我需要在构造函数中添加新参数,如下所示:

container.RegisterType<IOtherParameter, OtherParameter>();

container.RegisterType<IUnitOfWork, UnitOfWork>(
    "NorthwindUnitOfWork", 
    new InjectionConstructor(
        container.Resolve<IDbContext>("NorthwindContext"), 
        container.Resolve<IOtherParameter>()
    ));

如果我有很多参数,但是我只需要解决一个问题并且避免重复调用container.Resolve,我该怎么办?就像第一个例子一样,因为IOtherParameter只有一个选项(RegisterType )。其他受访者(例如this)使用SmartConstructor tecx查看了该库,但需要知道Unity 3中是否有任何解决方案以避免添加其他库。

0 个答案:

没有答案