Ninject + Bind generic repository

孤街浪徒 提交于 2019-12-17 09:37:49

问题


I'm trying to Bind a generic IRepository<> interface to my generic Repository<> - however it always return 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?


回答1:


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.




回答2:


See my answer on MVC3 Controller constructor + Ninject.

Generic Binding works correctly in Ninject. Try using a parameterless constructor in Repository. I think the problem is there.



来源:https://stackoverflow.com/questions/4370515/ninject-bind-generic-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!