存储库模式:每个实体一个存储库类? [英] Repository pattern: One repository class for each entity?
问题描述
假设您在 LINQ 类中定义了以下实体:
Say you have the following entities defined in a LINQ class:
Product
Customer
Category
我是否应该为所有人设置一个存储库类:
Should I have one repository class for all:
StoreRepository
...或者我应该有:
ProductRepository
CustomerRepository
CategoryRepository
什么是亲每个的缺点?就我而言,我的解决方案中有几个应用程序"……Store 应用程序只是其中之一.
What are the pro & cons of each? In my case, I have several "applications" within my solution... the Store application is just one of them.
推荐答案
这是我的观点.我是 Repository 模式的严格追随者.应该有 3 个方法接受单个实体.添加、更新、删除,一般定义.
Here's my point of view. I'm a strict follower of the Repository pattern. There should be 3 methods that take a single entity. Add, Update, Delete, generically defined.
public interface IRepository<T>
{
void Add(T entity);
void Update(T entity);
void Delete(T entity);
}
除了这些方法之外,您还要处理查询"或服务方法.如果我是你,我会让存储库按照上面的一般定义,添加一个QueryProvider",如下所示,并将你的业务逻辑放在它所属的位置在服务"或命令/查询"(来自 CQRS,Google it).
Beyond those methods, you're dealing with a "Query" or a service method. If I were you, I'd make the repository genrically defined as above, add a "QueryProvider" as shown below and put your business logic where it belongs in either "Services" or in "Commands/Queries" (comes from CQRS, Google it).
public interface IQueryProvider<T>
{
TResult Query<TResult>(Func<IQueryable<T>, TResult> query);
}
(希望我的意见有点用:))
(Hope my opinion is somewhat useful :) )
这篇关于存储库模式:每个实体一个存储库类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!