DDD存储库的优缺点 [英] Pros and cons of DDD Repositories

查看:221
本文介绍了DDD存储库的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

优点:


  • 存储库隐藏复杂的查询。

  • 可以使用存储库方法作为事务边界。

  • ORM可以轻松模拟

缺点:


  • ORM框架已经提供了一个集合,例如与持久对象的接口,存储库的目的是什么。因此,存储库为系统增加了额外的复杂性。

  • 使用findBy方法进行组合爆炸。使用Criteria对象,查询或示例对象可以避免使用这些方法。但这不需要存储库,因为ORM已经支持这些查找对象的方法。

  • 由于存储库是聚合根的集合(在DDD的意义上),因此必须创建一个并绕过聚合根,即使仅修改了一个子对象也是如此。

问题:


  • 您知道什么利弊?

  • 您建议使用存储库吗? (为什么或为什么不呢?)

推荐答案

存储库的要点(如Single责任原则)是抽象获取具有身份的对象的概念。随着我对DDD的适应程度提高,我没有想到将存储库主要集中在数据持久性上,而是将其实例化并保留其身份作为工厂。

The main point of a repository (as in Single Responsibility Principle) is to abstract the concept of getting objects that have identity. As I've become more comfortable with DDD, I haven't found it useful to think about repositories as being mainly focused on data persistence but instead as factories that instantiate objects and persist their identity.

在使用ORM时,应该以尽可能有限的方式使用其API,从而为自己提供一个特定于域的外观。因此,无论您的域是什么,仍然只会看到存储库。另一端具有ORM的事实是实现细节。

When you're using an ORM you should be using their API in as limited a way as possible, giving yourself a facade perhaps that is domain specific. So regardless your domain would still just see a repository. The fact that it has an ORM on the other side is an "implementation detail".

这篇关于DDD存储库的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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