NamedLikeFactoryMethod in Ninject Extensions Factory working in non-compliance with documentation

限于喜欢 提交于 2019-12-04 06:41:20

问题


I have a small issue with my simple example.

I have simple factory interface:

public interface ICameraFactory
{
  ICameraController GetNikonCamera();
  ICameraController GetCanonCamera();
}

I bind it as a factory:

IKernel kernel = new StandardKernel();
kernel.Bind<ICameraFactory>().ToFactory();

When i try to convert:

kernel.Bind<ICameraController>().To<NikonCameraController>()
.Named("NikonCamera");

to:

kernel.Bind<ICameraController>().To<NikonCameraController>()
.NamedLikeFactoryMethod<ICameraFactory>(f => f.GetNikonCamera());

it's don't compile.

For example, this code is compiled (but it's terrible):

kernel.Bind<ICameraController>()
.ToMethod<ICameraController>(c=>new NikonCameraController())
.NamedLikeFactoryMethod<ICameraController, ICameraFactory>(f => f.GetNikonCamera());

What am I doing wrong? Ninject 3.0.1.10 Ninject.Extension.Factory 3.0.1.0

Compile error: https://dl.dropbox.com/u/21806986/Screenshots/shot_19072012_133454.png


回答1:


You can use:

this.kernel.Bind<ICameraController>()
           .To<NikonCameraController>()
           .NamedLikeFactoryMethod((ICameraFactory f) => f.GetNikonCamera());


来源:https://stackoverflow.com/questions/11557856/namedlikefactorymethod-in-ninject-extensions-factory-working-in-non-compliance-w

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