Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject + Bind generic repository

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?

like image 788
ebb Avatar asked Dec 06 '10 20:12

ebb


2 Answers

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.

like image 58
quentin-starin Avatar answered Oct 17 '22 08:10

quentin-starin


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.

like image 32
Remo Gloor Avatar answered Oct 17 '22 10:10

Remo Gloor