简体   繁体   English

具有多个表的存储库模式

[英]Repository Pattern with multiple tables

Recently I have been reading up on using the repository pattern and DI to help create easily testable code, I think I understand it for the most part. 最近,我一直在阅读使用存储库模式和DI来帮助创建易于测试的代码的方法,我想我大部分时间都了解它。 However I'm having difficulty with one issue. 但是我遇到一个问题。 I need to create a Rules object for my applications business layer. 我需要为我的应用程序业务层创建一个Rules对象。 To create a rule, I need the ability to read and write to two tables. 要创建规则,我需要具有读取和写入两个表的功能。 How would you go about implementing a repository that uses two tables for one object? 您将如何实现对一个对象使用两个表的存储库?

for example: 例如:

ICollection<type> GetAllRules();

What would I put in for type as it requires two tables? 我需要输入什么类型的表,因为它需要两个表?

Thanks 谢谢

Steve 史蒂夫

I wouldn't insist on having a repository for that. 我不会坚持为此提供存储库。

As Fowler says 正如福勒所说

Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. 从概念上讲,存储库封装了持久存储在数据存储中的对象集以及对其执行的操作,从而提供了持久层的更面向对象的视图。

This is probably why most implementations tend to expose pure domain objects rather than derivatives (which your Rule object seems to be). 这可能就是为什么大多数实现倾向于公开纯领域对象而不是派生对象(您的Rule对象似乎是派生对象)的原因。

I would have two repositories for the two tables you mention, then I would have a unit of work to expose all repositories and then I would have a business layer service responsible for the compound processing. 对于您提到的两个表,我将有两个存储库,然后将有一个工作单元公开所有存储库,然后将有一个业务层服务负责复合处理。

An advantage of such approach would be that the repository layer remains clean, there is no business processing involved here, no unclear rules introduced to the persistence layer. 这种方法的优点是,存储库层保持干净,此处不涉及任何业务处理,也没有向持久层引入不明确的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM