如何绑定在Ninject通用型接口 [英] How to bind Generic-type interfaces in Ninject
问题描述
我是相当新的Ninject,发现自己绊倒,当我来到了实现一个通用存储库的模式。我要绑定的依赖关系的 IRepository< IEntityType> 一类的 ConcreteRepository<的EntityType> 其中ConcreteRepository< T>实现IRepository< T>和的EntityType实现IEntityType。我想这一点:
kernel.Bind< IRepository< IEntityType>>()为< ConcreteRepository<的EntityType>>( );
...但Ninject不会采取,因为它不知道或关心的EntityType工具IEntityType。我该如何去约束这种依赖?
更新
这是错误我收到:
错误3类型ICM.Dependency.Repository.ConcreteRepository不能用作类型参数TImplementation在泛型类型或方法Ninject.Syntax.IBindingToSyntax.To()。有一个从ConcreteRepository<的EntityType>'没有隐式引用转换为IRepository< IEntityType>。
块引用>
SOLUTION
我仍不很ŧ明白为什么我绑定不工作,但很显然,我是用仿制药不当那里。作为这种溶液并不真正涉及NInject。我结束指定ConcreteRepository明确与TEntityType连接IEntityType:
公共类ConcreteRepository< TInterface,TEntity> :IRepository< TInterface>哪里TEntity:TInterface {...}
然后,注入可如下写为:对>
kernel.Bind< IRepository< IEntityType>>()为<。ConcreteRepository< IEntityType,的EntityType>>()
解决方案kernel.Bind(typeof运算( IRepository<>))至(typeof运算(SimpleRepository<>));
如果你想在这里看看我的一种:的 http://blog.staticvoid.co.nz/2011/10/staticvoid-repository-pattern-nuget.html 我有约束力的例子
编辑:
你所得到的错误是说你的具体资料库心不是一个要绑定到通用的,实例即你需要做到这一点。
公共类ConcreteRepository< ConcreteEntity> :IRepository< IEntity> {}
不是
公共类ConcreteRepository< ConcreteEntity> :IRepository< ConcreteEntity> {}
I'm fairly new to Ninject, and found myself stumbling when I came to implement a generic repository pattern. I want to bind a dependency IRepository<IEntityType> to a class ConcreteRepository<EntityType> where ConcreteRepository<T> implements IRepository<T> and EntityType implements IEntityType. I tried this:
kernel.Bind<IRepository<IEntityType>>().To<ConcreteRepository<EntityType>>();
...but Ninject won't take that because it doesn't know or care that EntityType implements IEntityType. How can I go about binding this dependency?
UPDATE
This is the error I'm getting:
Error 3 The type 'ICM.Dependency.Repository.ConcreteRepository' cannot be used as type parameter 'TImplementation' in the generic type or method 'Ninject.Syntax.IBindingToSyntax.To()'. There is no implicit reference conversion from 'ConcreteRepository<EntityType>' to 'IRepository<IEntityType>'.
SOLUTION
I still don't quite understand why my binding doesn't work, but evidently I was using generics incorrectly there. As such the solution doesn't really relate to NInject. I ended specifying the ConcreteRepository to explicitly connect IEntityType with TEntityType:
public class ConcreteRepository<TInterface, TEntity> : IRepository<TInterface> where TEntity : TInterface { ... }
Then the injection can be written as follows:
kernel.Bind<IRepository<IEntityType>>().To<ConcreteRepository<IEntityType,EntityType>>()
解决方案kernel.Bind(typeof(IRepository<>)).To(typeof(SimpleRepository<>));
Take a look at my one if you want here: http://blog.staticvoid.co.nz/2011/10/staticvoid-repository-pattern-nuget.html i have binding examples
EDIT:
The error you are getting is saying that your concrete repository isnt an instance of the generic one you want to bind to, ie you will need to do this
public class ConcreteRepository<ConcreteEntity> : IRepository<IEntity>{}
not
public class ConcreteRepository<ConcreteEntity> : IRepository<ConcreteEntity>{}
这篇关于如何绑定在Ninject通用型接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!