Repository模式:每个实体的一个repository类? [英] Repository pattern: One repository class for each entity?

查看:753
本文介绍了Repository模式:每个实体的一个repository类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个LINQ类中定义以下实体:

Say you have the following entities defined in a LINQ class:

Product
Customer
Category

我应该有一个存储库类的所有:

Should I have one repository class for all:

StoreRepository

...或者我应该有:

... or should I have:

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);
}

除了这些方法,你正在处理一个查询或服务的方法。如果我是你,我会做genrically如上定义的库中,增加了QueryProvider,如下图所示和把你的业务逻辑的它所属的任一服务或命令/查询(来自CQRS,谷歌吧)

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 :) )

这篇关于Repository模式:每个实体的一个repository类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆