I have a BaseRepository that depends a DbContext to perform the database operations:
public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : Entity
{
...
}
I don’t want to insert this dependency using Constructor Dependency Injection, because if I use I’ll need to pass these dependency in constructors of derived repositories. I don’t want to use Property/Setter Dependency Injection too because Property/Setter Dependency Injection indicates dependency is optional, which is not the case.
My DbContext inherit from IDbContext interface, where is my UnitOfWork Pattern:
public class DbContext : System.Data.Entity.DbContext, IDbContext
{
...
}
And I’ve set my IDbContext using Ninject:
public override void Load()
{
Bind<IDbContext>().To<DbContext>().InRequestScope();
}
My question is how I inject DbContext in Base Repository, and I need a single instance of DbContext in a requestScope. (Using a Factory?)
I found the solution using the Service Locator of Ninject and took back the instance of
DbContext:And my Dependency Resolver class: