开发者

Ninject + Bind generic repository

开发者 https://www.devze.com 2023-01-28 23:03 出处:网络
I\'m trying to Bind a generic IRepository<> interface to my generic Repository<> - however it always return null?

I'm trying to Bind a generic IRepository<> interface to my generic Repository<> - however it always return null?

I have tr开发者_高级运维ied 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消