I'm a relative noob to the concept of Dependency Injection, and have been trying to glean the best practices before starting a new project.
Looking at the top answer to Dependency injection in unit of work pattern using repositories I understand the approach, but the example code seems to be missing something, and I'm stumped...
The definition of the interface IRepository
is shown as:
public interface IRepository
{
void Submit();
}
But when the interface is used as part of the definition of the GenericRepository
class, the Submit method is not implemented:
public abstract class GenericRepository<T> : IRepository<T> where T : class
{
public GenericRepository(IUnitOfWork unitOfWork)
{
unitOfWork.Register(this);
}
}
Then, a repository class for a specific entity is defined, inheriting from GenericRepository:
public class DepartmentRepository : GenericRepository<Department>
{
public DepartmentRepository(IUnitOfWork unitOfWork): base(unitOfWork) { }
}
My question is, given that each different entity's repository may need a reference to a different DataContext
, how should the generic Submit()
method be implemented using DI?