0

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

Dvor_nik
  • 1,091
  • 1
  • 10
  • 13

1 Answers1

5

You can use:

this.kernel.Bind<ICameraController>()
           .To<NikonCameraController>()
           .NamedLikeFactoryMethod((ICameraFactory f) => f.GetNikonCamera());
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • Thank you for answer, but I tried it and it didn't work. I have compile error like this: https://dl.dropbox.com/0/view/23e0vwi9fnjtx5b/Apps/CloudShot/shot_19072012_180915.png – Dvor_nik Jul 19 '12 at 14:07
  • But it's work: `kernel.Bind().ToMethod(x => new NikonCameraController()).NamedLikeFactoryMethod(f => f.GetCanonEOSCamera());` Where: `public class NikonCameraController : ICameraController { //Some implementation }` – Dvor_nik Jul 19 '12 at 14:13
  • Sry had it wrong in mind. The first generic argument is the implementation type (NikonCameraController) not the interface type. But there is a better syntax w/o this type. See updated answer. – Remo Gloor Jul 19 '12 at 14:26
  • Thank you for answer and for Extension.Factory! It's work fine. I think it can be added to the wiki (like best practices). – Dvor_nik Jul 19 '12 at 14:31