我首先使用 ASP.NET MVC 和实体框架代码创建一个应用程序。我正在使用存储库和工作单元模式,并受到以下链接的影响。
http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an- asp-net-mvc-应用程序 http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
在这里,我对工作单元的实现有疑问,因为链接工作单元是通过直接在类本身中编写实体来实现的。
public class UnitOfWork : IDisposable
{
private SchoolContext context = new SchoolContext();
private GenericRepository<Department> departmentRepository;
public GenericRepository<Department> DepartmentRepository
{
get
{
if (this.departmentRepository == null)
{
this.departmentRepository = new GenericRepository<Department>(context);
}
return departmentRepository;
}
}
}
您认为该实现足够好吗,因为每次添加/删除实体时,我都需要更改我的工作单元类。我认为工作单元不应该依赖于实体。因为在我的基于客户反馈的应用程序中,我们将经常添加/删除实体。
我可能听起来很愚蠢,但请让我知道您对此的看法。
工作单元模式已在实体框架中实现。
DbContext 是您的工作单元。
每个 IDbSet 都是一个存储库。
using (var context = new SchoolContext()) // instantiate our Unit of Work
{
var department = context.Departments.Find(id);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)