【发布时间】:2012-02-05 08:12:10
【问题描述】:
在 Ninject3 中有一个新的.ToConstructor feature。
如上所述,它有助于强类型构造函数参数,例如:
Bind<IMyService>().ToConstructor(
ctorArg => new MyService(ctorArg.Inject<IFoo>(), ctorArg.Inject<IBar>()));
以几乎相同的方式使用.ToConstructor和.ToMethod之间实际上有什么区别:
Bind<IMyService>().ToMethod(
x => new MyService(x.Kernel.Get<IFoo>(), x.Kernel.Get<IBar>()));
这只是避免使用 Kernel.Get() 的语法糖还是我还缺少什么?
【问题讨论】:
-
注意:
Get<T>是一种扩展方法,您必须是using Ninject;才能使用它。我花了一分钟,因为直到现在我已经摆脱了using Ninject.Modulesl)