是否有一种简单的方法可以使用Ninject将所有Factory接口绑定到ToFactory()扩展方法?
public class Foo
{
readonly IBarFactory barFactory;
public Foo(IBarFactory barFactory)
{
this.barFactory = barFactory;
}
public void Do()
{
var bar = this.barFactory.CreateBar();
...
}
}
public interface IBarFactory
{
Bar CreateBar();
}
对于上面的代码,我可以使用:
kernel.Bind<IBarFactory>().ToFactory();
如果我有10个或20个需要绑定的IFactory接口,我该怎么办?
答案 0 :(得分:6)
是使用约定:https://github.com/ninject/ninject.extensions.conventions
this.Bind(x => x.From_UseSomeOverloadToSpecifyTheAssemblies()
.SelectAllInterfaces().EndingWith("Factory")
.BindToFactory();