【发布时间】:2016-07-30 18:10:33
【问题描述】:
忽略实体框架中的 DbContext 现在是工作单元这一事实。 我想知道如何简化在 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();
}
}
【问题讨论】:
标签: c# unit-of-work