C# 在ASP.NET中实现存储库和工作单元模式
有人在www.asp.net上读过这篇题为 “在ASP.NET MVC应用程序中实现存储库和工作单元模式(第9页,共10页)” 文章中说:“这个通用存储库将处理典型的CRUD需求。当特定实体类型有特殊需求时,例如更复杂的筛选或排序,您可以创建一个派生类,该类具有该类型的其他方法。” 有人能详细解释我如何创建派生类并使用这些附加方法吗?我是否将其他方法作为虚拟方法放在通用存储库中 请帮忙,我不知道怎么做。非常感谢。以下是一个示例:C# 在ASP.NET中实现存储库和工作单元模式,c#,asp.net,entity-framework,repository,unit-of-work,C#,Asp.net,Entity Framework,Repository,Unit Of Work,有人在www.asp.net上读过这篇题为 “在ASP.NET MVC应用程序中实现存储库和工作单元模式(第9页,共10页)” 文章中说:“这个通用存储库将处理典型的CRUD需求。当特定实体类型有特殊需求时,例如更复杂的筛选或排序,您可以创建一个派生类,该类具有该类型的其他方法。” 有人能详细解释我如何创建派生类并使用这些附加方法吗?我是否将其他方法作为虚拟方法放在通用存储库中 请帮忙,我不知道怎么做。非常感谢。以下是一个示例: public class GenericRepository&l
public class GenericRepository<TEntity> where TEntity : class
{
public virtual TEntity GetByID(object id)
{
// ...
}
public virtual void Insert(TEntity entity)
{
// ...
}
public virtual void Delete(TEntity entityToDelete)
{
// ...
}
}
编辑-在文章中,他们在工作单元中使用了一个通用存储库,但当您的存储库更具体时,您可以使用它。例如:
public class UnitOfWork : IDisposable
{
private GenericRepository<Department> departmentRepository;
private GenericRepository<Course> courseRepository;
// here is the one we created, which is essentially a GenericRepository as well
private UserRepository userRepository;
public UserRepository UserRepository
{
get
{
if (this.userRepository== null)
{
this.userRepository= new UserRepository(context);
}
return this.userRepository;
}
}
// ...
}
公共类UnitOfWork:IDisposable
{
私人通用存储部门存储库;
私人普通储蓄课程储蓄;
//这是我们创建的一个,本质上也是一个一般性的描述
私有用户存储库用户存储库;
公共用户存储库用户存储库
{
得到
{
if(this.userRepository==null)
{
this.userRepository=新的userRepository(上下文);
}
返回此.userRepository;
}
}
// ...
}
非常感谢您的回复。我想我的困难在于如何使用这个用户存储库?在那篇文章中,Course=unitOfWork.CourseRepository.GetByID(id);unitOfWork.CourseRepository.Delete(id);其中GetByID()和Delete()是GenericRepository中的方法。现在GetByEmail()位于UserRepository中。当我需要使用UnitOfWork时,我如何使用它?@军事检查我的编辑,我希望它现在能有意义。@军事:)很高兴我能帮忙:)干杯
public class UnitOfWork : IDisposable
{
private GenericRepository<Department> departmentRepository;
private GenericRepository<Course> courseRepository;
// here is the one we created, which is essentially a GenericRepository as well
private UserRepository userRepository;
public UserRepository UserRepository
{
get
{
if (this.userRepository== null)
{
this.userRepository= new UserRepository(context);
}
return this.userRepository;
}
}
// ...
}