简体   繁体   English

JPA实体在域驱动设计中的责任

[英]JPA entity responsibility in domain driven design

I am wondering if JPA entity and DDD entity should be the same class? 我想知道JPA实体和DDD实体是否应该是同一个类? I can see in examples that it is a common practice, but doesn't it violate single responsibility principle? 我可以在例子中看到这是一种常见做法,但它是否违反了单一责任原则?

I am wondering if JPA entity and DDD entity should be the same class? 我想知道JPA实体和DDD实体是否应该是同一个类? I can see in examples that it is a common practice, but doesn't it violate single responsibility principle? 我可以在例子中看到这是一种常见做法,但它是否违反了单一责任原则?

You may want to review Classes vs Data Structures , by Robert Martin. 您可能希望查看Robert Martin的Classes vs Data Structures

I would normally expect JPA entities to be "anemic bags of data"; 我通常认为JPA实体是“贫穷的数据包”; they are essentially messages written in the past to be consumed in the future. 它们本质上是过去写的消息 ,将来会被消费。

Domain Model Entities, on the other hand, are not anemic - they have direct understanding of how to mutate their own data structures in accordance with the rules of the domain in which they serve. 另一方面,领域模型实体并非贫血 - 他们直接了解如何根据服务领域的规则改变自己的数据结构。

In the DDD book, Evans describes using the "factory" pattern to create an instance of a domain entity from raw data. 在DDD书中,Evans描述了使用“工厂”模式从原始数据创建域实体的实例。 That pattern fits equally well with creating a domain entity from a jpa entity. 该模式同样适用于从jpa实体创建域实体。

The transformation in the other direction -- taking a domain entity and extracting from it the data you need to save, is not clearly addressed, but the mechanics are the same. 另一方面的转变 - 采用域实体并从中提取您需要保存的数据,并没有明确解决,但机制是相同的。 You read data out of your domain entity, and write it into your jpa entity (whether or you writing into a new jpa entity, or updating one that already exists, will depend on the details of your persistence strategy). 您从域实体中读取数据,并将其写入您的jpa实体(无论是否写入新的jpa实体,或者更新已存在的实体,都将取决于持久性策略的详细信息)。

You aren't guaranteed to make a mess if you try to make the two entities "the same", but there are definitely different concerns. 如果你试图使两个实体“相同”,你不能保证弄得一团糟,但肯定存在不同的问题。

For example, our persistent representation of data is expected to have a life cycle that spans many releases, new versions of our domain model are supposed to be to work our previously stored data. 例如,我们对数据的持久表示应该具有跨越许多版本的生命周期,我们的域模型的新版本应该用于处理我们先前存储的数据。 At the same time, we should be able to change the data structures that we use inside the domain model freely. 同时,我们应该能够自由地更改我们在域模型中使用的数据结构。 Semantically, it's the same information, but the pressures on structure and organization are very different. 在语义上,它是相同的信息,但结构和组织的压力是非常不同的。

I believe this to be common practice due to a level of convenience but it's not particularly clean design. 我认为这是一种常见的做法,因为它具有一定的便利性,但它并不是特别干净的设计。 In most cases it's coincidental that the JPA entities match, or are at least close enough, to domain objects. 在大多数情况下,JPA实体与域对象匹配或至少足够接近是巧合。

JPA is an abstraction to the persistent data layer and its core focus is providing an object to relational data mapping. JPA是持久数据层的抽象,其核心焦点是为关系数据映射提供对象。 The JPA entities therefore really only represent object hierarchies of the data model. 因此,JPA实体实际上只表示数据模型的对象层次结构。

It may well be that your domain objects consist of only elements that are represented and stored within a persistent data store and this would feel somewhat like duplication if creating both domain and JPA entities that contain the exact same data structures. 很可能你的域对象只包含在持久数据存储中表示和存储的元素,如果同时创建包含完全相同数据结构的域和JPA实体,这会有点像重复。

True domain objects live at the center of the application's architecture where all dependencies point towards them and this would also include the data layer. 真正的域对象位于应用程序架构的中心,所有依赖关系都指向它们,这也包括数据层。 I would always recommend this approach purely as it clarifies the actual intent for the architectural boundaries. 我总是会推荐这种方法,因为它澄清了架构边界的实际意图。

Edit. 编辑。 To answer your second part of the question on SRP violation in JPA - it depends. 要回答关于JPA中SRP违规问题的第二部分 - 这取决于。 The responsibilities (in SRP) do tend to match relational tables since we tend to logically group related data together (think Account table, or Contact table). 责任(在SRP中)确实倾向于匹配关系表,因为我们倾向于将相关数据逻辑分组在一起(想想Account表或Contact表)。 This does fall down in JPA more often though when thinking about relationships (Employee -> Salary). 虽然在考虑关系(员工 - >薪水)时,这在JPA中会更常出现。

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

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