【发布时间】:2014-03-10 18:11:09
【问题描述】:
看这个非常简单的例子:调用 CreateCar 成功,调用 GetCar 失败,说“激活 ICar 时出错:没有匹配的绑定可用,并且类型不能自绑定”。
public interface ICar { }
public class Car : ICar
{
public Car(string carType) { }
}
public interface ICarFactory
{
ICar CreateCar(string carType); // this is fine
ICar GetCar(string carType); // this is bad
}
public class CarModule : NinjectModule
{
public override void Load()
{
Bind<ICarFactory>().ToFactory();
Bind<ICar>().To<Car>();
}
}
public class Program
{
public static void Main()
{
using (var kernel = new StandardKernel(new FuncModule(), new CarModule()))
{
var factory = kernel.Get<ICarFactory>();
var car1 = factory.CreateCar("a type");
var car2 = factory.GetCar("another type");
}
}
}
假设它必须与 Get*ClassName* 的某种约定相关(类似于 NamedLikeFactoryMethod 的东西)。有没有办法避免应用这个约定?我不需要它,我也不想要它(我已经浪费了太多时间试图找出绑定失败的原因,幸运的是我在 10 家工厂中的 1 家打错了字,我注意到它工作只是因为工厂方法名称是“Ger”而不是“Get”)。
谢谢!
【问题讨论】:
标签: ninject naming-conventions factory ninject-extensions