简体   繁体   English

Ninject + Bind通用存储库

[英]Ninject + Bind generic repository

I'm trying to Bind a generic IRepository<> interface to my generic Repository<> - however it always return null? 我正在尝试将通用IRepository <>接口绑定到我的通用Repository <>-但是它总是返回null?

I have tried various things like: 我已经尝试过各种方法,例如:

Bind(typeof(IRepository<CustomerModel>)).To(typeof(Repository<CustomerModel>)); 
Bind(typeof(IRepository<>)).To(typeof(Repository<>)); 

However if I pass in a non-generic interface and class then it works like a dream? 但是,如果我传入一个非通用的接口和类,那么它像梦一样工作吗?

 Bind(typeof(IRepository<>)).To(typeof(Repository<>)); 

This is the correct syntax for binding an open generic. 这是绑定开放泛型的正确语法。

If you are receiving null back when requesting IRepository< of whatever > , then there may be some other problem in an area of code you haven't shared. 如果在请求IRepository< of whatever >时收到空返回,则在您尚未共享的代码区域中可能存在其他问题。

See my answer on MVC3 Controller constructor + Ninject . 请参阅我对MVC3控制器构造函数+ Ninject的回答。

Generic Binding works correctly in Ninject. 通用绑定在Ninject中可以正常工作。 Try using a parameterless constructor in Repository. 尝试在存储库中使用无参数构造函数。 I think the problem is there. 我认为问题就在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM