Repository 中的单一职责原则和多种方法
Single Responsibility Principle and Many methods in Repository
我正在尝试理解单一职责原则 (SRP)。它说 class 应该只有一个责任和改变的理由。这是下面的典型存储库。这是否意味着每个项目都应该是它自己的 class?目前Insert,Delete,Search,都在1class?如果是这样,人们为什么不将所有项目分成多个 classes?
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly DbContext _dbContext;
public BaseRepository(DbContext dbContext)
{
_dbContext = dbContext;
}
public void Insert(TEntity entity)
{
_dbContext.Set<TEntity>().Add(entity);
_dbContext.SaveChanges();
}
public void Delete(TEntity entity)
{
_dbContext.Set<TEntity>().Remove(entity);
_dbContext.SaveChanges();
}
public IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate)
{
return _dbContext.Set<TEntity>().Where(predicate);
}
public IQueryable<TEntity> GetAll()
{
return _dbContext.Set<TEntity>();
}
public TEntity GetById(int id)
{
return _dbContext.Set<TEntity>().Find(id);
}
}
问题:这个存储库的职责是什么class?
answer : 对数据库执行操作 (CRUD)
进一步阅读:http://pragmaticcraftsman.com/2006/07/single-responsibility-principle
"I would rephrase what you said about SRP into being. A class should have only ONE reason to change. The responsibility of the Repository class would be to execute operations against a database (CRUD operations for example). Many people get confused thinking that a class should only contain one method, but that's not what Robert Martin describes...There are plenty of ways, if its just the 4 crud methods, leave them in one and that's fine. if you have update/insert/delete and many (5+) read operations, consider splitting that into a read and write repository. If you have a lot more read/write operations, consider applying CQRS with Command Handlers for each writing (insert, update, delete, bulk delete etc) operation and Query handlers for each read operation –"
我正在尝试理解单一职责原则 (SRP)。它说 class 应该只有一个责任和改变的理由。这是下面的典型存储库。这是否意味着每个项目都应该是它自己的 class?目前Insert,Delete,Search,都在1class?如果是这样,人们为什么不将所有项目分成多个 classes?
public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly DbContext _dbContext;
public BaseRepository(DbContext dbContext)
{
_dbContext = dbContext;
}
public void Insert(TEntity entity)
{
_dbContext.Set<TEntity>().Add(entity);
_dbContext.SaveChanges();
}
public void Delete(TEntity entity)
{
_dbContext.Set<TEntity>().Remove(entity);
_dbContext.SaveChanges();
}
public IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate)
{
return _dbContext.Set<TEntity>().Where(predicate);
}
public IQueryable<TEntity> GetAll()
{
return _dbContext.Set<TEntity>();
}
public TEntity GetById(int id)
{
return _dbContext.Set<TEntity>().Find(id);
}
}
问题:这个存储库的职责是什么class?
answer : 对数据库执行操作 (CRUD)
进一步阅读:http://pragmaticcraftsman.com/2006/07/single-responsibility-principle
"I would rephrase what you said about SRP into being. A class should have only ONE reason to change. The responsibility of the Repository class would be to execute operations against a database (CRUD operations for example). Many people get confused thinking that a class should only contain one method, but that's not what Robert Martin describes...There are plenty of ways, if its just the 4 crud methods, leave them in one and that's fine. if you have update/insert/delete and many (5+) read operations, consider splitting that into a read and write repository. If you have a lot more read/write operations, consider applying CQRS with Command Handlers for each writing (insert, update, delete, bulk delete etc) operation and Query handlers for each read operation –"