rou*_*uan 3 c# ninject asp.net-mvc-4
如果我有以下内容:
public MyClass(IServiceOne serviceOne, IServiceTwo serviceTwo, IServiceThree serviceThree = null)
{
this.serviceOne = serviceOne;
this.serviceTwo = serviceTwo;
this.serviceThree = serviceThree ?? new ServiceThree("some-info");
}
Run Code Online (Sandbox Code Playgroud)
我如何告诉Ninject绑定前两个参数,而不是IServiceThree类型的参数?这甚至可能吗?
我希望serviceThree作为构造函数参数的原因是可测试性 - 我需要能够从我的测试中注入一个mock.
如果您不打算使用构造函数注入,那么您也可以从构造函数参数列表中将其删除,并按照您的方式手动初始化它.换句话说,只传递两个参数而不是三个.
更新:你能做的就是注入null价值.据我所知,你必须做以下事情:
_kernel.Settings.AllowNullInjection = true;
Run Code Online (Sandbox Code Playgroud)
然后,您可以删除第三个参数的绑定(如果存在).
| 归档时间: |
|
| 查看次数: |
1479 次 |
| 最近记录: |