C# NInject:使用两个参数解析构造函数注入
我创建了以下接口和类:C# NInject:使用两个参数解析构造函数注入,c#,ninject,C#,Ninject,我创建了以下接口和类: interface IInterpreter. interface IViz. interface IVizDescriptor. 在哪里, class IntOne : IInterpreter. 以及 以及 我发现我能够在IInterpreter和IntOne之间创建绑定,以便在请求VizOne时,将IntOne注入第一个参数构造函数 问题是没有办法为VizDescOne创建合适的绑定 VizDescOne的构造函数参数太依赖于每种情况,我无法为其创建绑定 是否有
interface IInterpreter.
interface IViz.
interface IVizDescriptor.
在哪里,
class IntOne : IInterpreter.
以及
以及
我发现我能够在IInterpreter
和IntOne
之间创建绑定,以便在请求VizOne
时,将IntOne
注入第一个参数构造函数
问题是没有办法为VizDescOne
创建合适的绑定<代码>
VizDescOne的构造函数参数太依赖于每种情况,我无法为其创建绑定
是否有办法手动提供并解析VizOne(IntOne,IVizDescriptor)
构造函数
但是,IVizDescriptor
太依赖于任何具体情况您有很多选择:
您可以使用构造函数将IVizDescriptor绑定到VizDescriptor
论据:
kernel.Bind<IVizDescriptor>().To<VizDescOne>()
.WithConstructorArgument("title", "someTitle").WithConstructorArgument("type", typeof(int))...
kernel.Bind()到()
.WithConstructorArgument(“title”,“someTitle”)。WithConstructorArgument(“type”,typeof(int))。。。
您可以将IVizDescriptor绑定到常量:
IVizDescriptor vizDescOne = new VizDescOne(...);
kernel.Bind<IVizDescriptor>().ToConstant(vizDescOne);
IVizDescriptor-vizDescOne=新的vizDescOne(…);
kernel.Bind().ToConstant(vizDescOne);
您可以将IVizDescriptor绑定到方法:
kernel.Bind<IVizDescriptor>().ToMethod(o=> new VizDescOne(...));
kernel.Bind().ToMethod(o=>newVizDescone(…);
您可以阅读有关这些选项的更多信息,以及更多信息。
作为旁注,我真的建议您阅读@Steven comment和他链接的文章,因为如果构造函数参数是do运行时值,您应该重新考虑您的设计 我希望title
、type
和desc
等参数都是运行时值;在应用程序的生命周期内,它们不是恒定的。内核是什么?ninject文档似乎相当难以捉摸。
IVizDescriptor vizDescOne = new VizDescOne(...);
kernel.Bind<IVizDescriptor>().ToConstant(vizDescOne);
kernel.Bind<IVizDescriptor>().ToMethod(o=> new VizDescOne(...));