Ninject and Generic

I am trying to reuse NopCommerce code to access data using Ninject. My question is: How can I introduce an undefined generic type for an object? I know that NopCommerce uses Autofac.

Description of my objects: I have a controller ( MyController) that contains a repository ( IRepository<T>). This repository is entered as EfRepository<T>using the ninject: command kernel.Bind(typeof(IRepository<>)).To(typeof(EfRepository<>)). EfRepositoryhas a type IDbContextthat is common DbContext. EfRepositorydoes not pass on to him the general type IDbContext, but still he is introduced to him. How is this done with Ninject?

the code;

public class MyController : Controller
{
    //a repository --> injected as EfRepository<> using generic injection using the command:
    //kernel.Bind(typeof(IRepository<>)).To(typeof(EfRepository<>));
    private readonly IRepository<Account> _repository;
}

public class EfRepository<T> : IRepository<T> where T : BaseEntity
{
    private IDbContext _context; //<<== How do I inject <T> to IDbcontext?
}

public interface IDbContext
{
    IDbSet<T> Set<T>() where T : BaseEntity;
} 
+3
source share
1 answer

IDbContext , T Set .

public class EfRepository<T> : IRepository<T> where T : BaseEntity
{
    private IDbContext context;
    public EfRepository(IDbContext dbContext)
    {
        this.context = context;
    }

    public void Do()
    {
        var dbSet = this.context.Set<T>();
    }
}
+2

All Articles