Constructor with multiple arguments with Ninject

不问归期 提交于 2019-12-03 05:28:47

It's very easy. No matter how many constructor arguments, the binding stays the same:

Bind<IAuthorizationService>().To<MyAuthenticator>();

Let's say MyAuthenticator had a constructor with one parameter of type IFoo. All you got to do is tell ninject how it can resolve/create an IFoo. Again, very simple:

Bind<IFoo>().To<Foo>();

You don't need WithConstructorArgument ever, except in case you want to override the default behavior of ninject. Let's say MyAuthenticator has a parameter of type IFoo plus another parameter string seed which you want to configure specifically. All you'd need is:

Bind<IFoo>().To<Foo>();
Bind<IAuthorizationService>().To<MyAuthenticator>()
    .WithConstructorArgument("seed", "initialSeedValue");

no need to specify the value of the IFoo parameter!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!