在存储库模式中加载子记录 [英] Loading Subrecords in the Repository Pattern

查看:130
本文介绍了在存储库模式中加载子记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用LINQ TO SQL作为基于存储库的解决方案的基础。我的实现如下:



资源库

  FindAll 
FindByID
插入
更新
删除

然后我有扩展名用于查询结果的方法如下:

  WhereSomethingEqualsTrue()... 

我的问题如下:



我的用户资料库有N个角色。我创建一个角色存储库来管理角色吗?我担心,如果我去这条路线,我最终会创建几十个存储库(每个表几乎只有加入表)。每个表的存储库是常见的吗?

解决方案

如果您正在构建一个特定于一个实体(表)的存储库,每个实体都有您上面列出的IRepository界面中的方法列表,那么您真正在做的是执行活动记录模式。



您应该绝对不每个表有一个Repository。您需要识别域模型中的聚合,以及要对其执行的操作。用户和角色通常是紧密相关的,一般来说,您的应用程序将与其一起执行操作 - 这需要一个以用户为中心的单个存储库,并且它是一组紧密相关的实体。



我从你的帖子中猜出你有一个看到这个例子。这个例子的问题是,所有的存储库在基础级别共享相同的CRUD功能,但是他并没有超越这一点,并且实现了任何一个域功能。该示例中的所有存储库看起来都一样 - 但实际上,真正的存储库并不都看起来不一样(尽管它们仍然应该是连接的),将会有与每一个关联的特定域操作。



您的存储库域操作应该更像:

  userRepository.FindRolesByUserId(int userID)
userRepository.AddUserToRole(int userID)
userRepository.FindAllUsers()
userRepository.FindAllRoles()
userRepository.GetUserSettings(int userID)
/ pre>

等...



这些是您的应用程序希望对底层数据执行的特定操作,并且Repository应该提供。认为它是因为Repository表示您将在域上执行的一组原子操作。如果您选择通过通用存储库共享某些功能,并使用扩展方法扩展特定的存储库,那么可能对您的应用程序可能正常工作的一种方法。



一个很好的规则您的应用程序应该是罕见的,以便实例化多个存储库以完成操作。需要确实出现,但是如果您的应用程序中的每个事件处理程序都在玩杂耍6个存储库,只是为了获取用户的输入并正确实例化输入表示的实体,那么您可能会遇到设计问题。


Using LINQ TO SQL as the underpinning of a Repository-based solution. My implementation is as follows:

IRepository

FindAll
FindByID
Insert
Update
Delete

Then I have extension methods that are used to query the results as such:

WhereSomethingEqualsTrue() ...

My question is as follows:

My Users repository has N roles. Do I create a Roles repository to manage Roles? I worry I'll end up creating dozens of Repositories (1 per table almost except for Join tables) if I go this route. Is a Repository per Table common?

解决方案

If you are building your Repository to be specific to one Entity (table), such that each Entity has the list of methods in your IRepository interface that you listed above, then what you are really doing is an implementation of the Active Record pattern.

You should definitely not have one Repository per table. You need to identify the Aggregates in your domain model, and the operations that you want to perform on them. Users and Roles are usually tightly related, and generally your application would be performing operations with them in tandem - this calls for a single repository, centered around the User and it's set of closely related entities.

I'm guessing from your post that you've seen this example. The problem with this example is that all the repositories are sharing the same CRUD functionality at the base level, but he doesn't go beyond this and implement any of the domain functions. All the repositories in that example look the same - but in reality, real repositories don't all look the same (although they should still be interfaced), there will be specific domain operations associated with each one.

Your repository domain operations should look more like:

userRepository.FindRolesByUserId(int userID)
userRepository.AddUserToRole(int userID)
userRepository.FindAllUsers()
userRepository.FindAllRoles()
userRepository.GetUserSettings(int userID)

etc...

These are specific operations that your application wants to perform on the underlying data, and the Repository should provide that. Think of it as the Repository represents the set of atomic operations that you would perform on the domain. If you choose to share some functionality through a generic repository, and extend specific repositories with extension methods, that's one approach that may work just fine for your app.

A good rule of thumb is that it should be rare for your application to need to instantiate multiple repositories to complete an operation. The need does arise, but if every event handler in your app is juggling six repositories just to take the user's input and correctly instantiate the entities that the input represents, then you probably have design problems.

这篇关于在存储库模式中加载子记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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