1
忽略實體框架中的DbContext現在是工作單元的事實。 我想知道我可以如何簡化在類UnitOfWork中創建存儲庫,因爲現在每次創建新的存儲庫類時都必須將該屬性添加到該類中?我不想要通用存儲庫類。如何簡化在UnitOfWork類中創建存儲庫?
public class UnitOfWork
{
private SchoolContext _context = new SchoolContext();
private IDepartmentRepository _departmentRepository;
private ICourseRepository _courseRepository;
public IDepartmentRepository DepartmentRepository
{
get
{
if (this._departmentRepository == null)
{
this._departmentRepository = new DepartmentRepository(_context);
}
return _departmentRepository;
}
}
public ICourseRepository CourseRepository
{
get
{
if (this._courseRepository == null)
{
this._courseRepository = new CourseRepository(_context);
}
return _courseRepository;
}
}
public void Save()
{
_context.SaveChanges();
}
}