Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Binding in Ninject without a default constructor

Ninject seems to be having issues resolving the following:

public interface IRepository<TEntity> : IDisposable where TEntity : class,IEntity
{
}

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class,IEntity
{
    protected IDbContext _context;

    public Repository(IDbContext context)
    {
        _context = context;
    }
}

When there is a need to do something special, I do:

public interface IMyEntityRepository : IRepository<MyEntity>
{
    int GetSomeStuffForAnObject();
}

That works great, but binding doesn't work if I am just using the default Repository<T>.

like image 862
bhaydin Avatar asked Dec 04 '25 15:12

bhaydin


1 Answers

Ok, I must have missed something earlier.

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

Seems to work.

like image 64
bhaydin Avatar answered Dec 06 '25 13:12

bhaydin