存储库模式是否过度杀伤力

发布于 2024-09-13 00:31:11 字数 438 浏览 3 评论 0原文

我使用存储库模式(DDD 和 POEAA)已经有一段时间了。然而,我们的一些团队成员认为这只是一个额外的抽象层,没有必要。 我可以从他们的论点中看到一些好处。现代 ORM 解决方案(NHibernate 或 EF)几乎拥有您所需的一切。我搜索并找到了一些类似 this 的文章反驳。那么存储库模式是否有点过大了?

I have been using Repository pattern (DDD and POEAA) for some time. However some of our team members have argued that it is just an extra layer of abstraction and unnecessary.
I can seen some benefit in their arguments. Modern ORM solutions (NHibernate or EF) have almost everything you need. I searched and found some article like this and counterargument on this topic. So Is repository pattern an overkill?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝對不後悔。 2024-09-20 00:31:11

这主要取决于问题的复杂性以及领域模型在解决方案中扮演的角色。对于简单的解决方案,存储库可能有点大材小用。但对于具有强大语言和不断变化的需求的复杂领域,存储库是一个很好的、干净的抽象,它拥有领域对象的生命周期。许多 ORM 都会做很多这样的事情,但是,在复杂的领域中,总会有一些领域活动在存储库中有意义,但开箱即用的 ORM 不支持这些活动。

底线:这取决于上下文。

It depends, mostly on the complexity of your problem and the role your Domain Model plays in the solution. For simple solutions, Repository is probably overkill. But for complex domains with a robust language and evolving needs/requirements, the Repository is a nice, clean abstraction that owns the domain object lifecycle. Many ORMs will do much of this, but, in a complex domain, there will always be some domain activity that makes sense in a repository and which is not supported by an ORM out of the box.

Bottom line: it depends on the context.

澉约 2024-09-20 00:31:11

在单元测试中模拟数据访问是我使用存储库接口的主要原因。另一个原因是可维护性 - 您可以轻松实现缓存策略,或切换到其他数据访问实现,例如从服务而不是数据库获取数据。

Mocking data access in unit tests is the main reason I use repository interfaces. Another reason maintainability - you can easily implement caching strategies, or switch to other data access implementation, like getting data from the service instead of DB.

他不在意 2024-09-20 00:31:11

我们在项目中使用存储库的原因之一是它强制我们的聚合根是谁(我们只允许 AR 的存储库),以便您正确地使用 AR,而不是查询您喜欢的任何内容。

正如 Al 提到的……它确实提供了一个很好的接口,可以在单元测试期间进行模拟。

The one reason we use Repositories in our project is that it enforces who our Aggregate Roots are (we only allow repositories for ARs) so that you work through the AR properly instead of querying for whatever strikes your fancy.

And as Al mentioned.. it does provide a nice interface to mock out during unit tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文