注入通用接口为MVC控制器我有以下情况:如何使用Ninject
接口:
public interface ITest<T> where T:class { void Delete(T item); }
抽象实现:
public abstract class Test<T>:ITest<T> where T:class { private readonly ApplicationDbContext _context; protected Test(ApplicationDbContext context){ _context=context; } public void Delete(T item) { } }
最终课程:
public class RepoTest:Test<FirstEntity> { public void DoSomething() { } }
我有一个MVC控制器,它看起来像这样:
public abstract class MyController<T>:Controller where T:class
{
private readonly ITest<T> _test;
protected MyController(ITest<T> test)
{
_test = test;
}
}
对于每个实体,我创建了一个控制器,从myController的继承,并在实体基础我想ninject具体注入类。
为此,我尝试使用这个绑定:
kernel.Bind(typeof(ITest<>)).To(typeof(Test<>)).InRequestScope();
kernel.Bind(x=>x.FromAssemblyContaining(typeof(Test<>))
.SelectAllClasses()
.InheritedFrom(typeof(Test<>))
.BindToSelf());
不幸的是我alwasys了这种错误的:
错误激活ITEST {工具} 没有匹配的绑定是可用的,并且类型不可自行绑定。 激活路径:依赖ITEST {工具}的 2)注入用于ToolsController
类型的构造ToolsController 1)请求的参数测试建议:1)确保您已经定义了 ITEST {工具}绑定。 2)如果在模块中定义了绑定,请确保该模块已加载到内核中。 3)确保你没有 意外创建了多个内核。 4)如果您正在使用 构造函数参数,请确保参数名称与 构造函数参数名称匹配。 5)如果您正在使用自动模块 加载,请确保搜索路径和过滤器是正确的。
我该如何告诉Ninject,要将实体类型的类基础注入?
异常文本引用类'ToolsController',它不在列表中。你能拿出来吗? – zaitsman
此外,请显示'Test' –
zaitsman
的构造函数我编辑了我的帖子,现在可见构造函数。我正在寻找一种有bacth绑定方法的方法,因为我有更多的50个实体。 –