必须在存储库模式中创建多少存储库接口?
How much repository interfaces must created in Repository Pattern?
假设我们有以下 类:
public class User
{
//User Definitions Goes Here
}
public class Product
{
//Product Definitions Goes Here
}
public class Order
{
//Order Definitions Goes Here
}
有了以上模型,我是否应该只创建一个存储库,如:
public interface IRepository
{
//IRepository Definition Goes Here
}
或者最好有多个仓库:
public interface IUserRepository
{
//IUserRepository Definition Goes Here
}
public interface IProductRepository
{
//IProductRepository Definition Goes Here
}
public interface IOrderRepository
{
//IOrderRepository Definition Goes Here
}
各自的优缺点是什么?
如果您采用第一种方法,则可以避免重复自己,满足 DRY 原则。但是你通过将未连接的项目集中在一个界面和任何实现 class.
中来破坏关注点分离原则
如果您采用第二种方法,您实现了良好的关注点分离,但有重复自己的风险,因此违反了 DRY 原则。
一种解决方案是第三种方法:混合。
public interface IRepository<T>
{
IEnumerable<T> Query {get;}
void Add(TEntity entity);
void Delete(TEntity entity);
}
public interface IUserRepository : IRepository<IUser>;
public interface IProductRepository : IRepository<IProduct>;
public interface IOrderRepository : IRepository<IOrder>;
这种方法满足了这两个原则。
没有必须。您创建尽可能多的应用程序 needs.You 可以为每个业务对象和通用接口提供一个存储库接口。
类似
interface ICRUDRepo<T> //where T is always a Domain object
{
T get(GUid id);
void Add(T entity);
void Save(T entity);
}
//then it's best (for maintainability) to define a specific interface for each case
interface IProductsRepository:ICRUDRepo<Product>
{
//additional methods if needed by the domain use cases only
//this search the storage for Products matching a certain criteria,
// then returns a materialized collection of products
//which satisfy the given criteria
IEnumerable<Product> GetProducts(SelectByDate criteria);
}
一切都是为了拥有一个干净清晰的抽象,这将允许将域与持久性适当地分离。
存在通用抽象,因此我们可以节省一些击键次数,并且可能有一些通用的扩展方法。然而,为这些目的使用一个通用的通用接口并不真正算作 DRY
假设我们有以下 类:
public class User
{
//User Definitions Goes Here
}
public class Product
{
//Product Definitions Goes Here
}
public class Order
{
//Order Definitions Goes Here
}
有了以上模型,我是否应该只创建一个存储库,如:
public interface IRepository
{
//IRepository Definition Goes Here
}
或者最好有多个仓库:
public interface IUserRepository
{
//IUserRepository Definition Goes Here
}
public interface IProductRepository
{
//IProductRepository Definition Goes Here
}
public interface IOrderRepository
{
//IOrderRepository Definition Goes Here
}
各自的优缺点是什么?
如果您采用第一种方法,则可以避免重复自己,满足 DRY 原则。但是你通过将未连接的项目集中在一个界面和任何实现 class.
中来破坏关注点分离原则如果您采用第二种方法,您实现了良好的关注点分离,但有重复自己的风险,因此违反了 DRY 原则。
一种解决方案是第三种方法:混合。
public interface IRepository<T>
{
IEnumerable<T> Query {get;}
void Add(TEntity entity);
void Delete(TEntity entity);
}
public interface IUserRepository : IRepository<IUser>;
public interface IProductRepository : IRepository<IProduct>;
public interface IOrderRepository : IRepository<IOrder>;
这种方法满足了这两个原则。
没有必须。您创建尽可能多的应用程序 needs.You 可以为每个业务对象和通用接口提供一个存储库接口。
类似
interface ICRUDRepo<T> //where T is always a Domain object
{
T get(GUid id);
void Add(T entity);
void Save(T entity);
}
//then it's best (for maintainability) to define a specific interface for each case
interface IProductsRepository:ICRUDRepo<Product>
{
//additional methods if needed by the domain use cases only
//this search the storage for Products matching a certain criteria,
// then returns a materialized collection of products
//which satisfy the given criteria
IEnumerable<Product> GetProducts(SelectByDate criteria);
}
一切都是为了拥有一个干净清晰的抽象,这将允许将域与持久性适当地分离。
存在通用抽象,因此我们可以节省一些击键次数,并且可能有一些通用的扩展方法。然而,为这些目的使用一个通用的通用接口并不真正算作 DRY