本文介绍了DDD、域实体/VO 和 JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 DDD 开始,你可以想象我的大脑正在沸腾.

I'm starting with DDD and you can image my brain is boiling.

我的问题与我的域对象(实体、VO、...)有关,它代表我的域概念/逻辑以及如何持久化/检索它们.

My question is related to my domain objects (entities, VO, ...) which represents my domain concepts/logic and how to persist/retrieve them.

蓝皮书说存储库是在域对象上表示集合的一种方式,并负责与基础设施层进行通信.我也在一些帖子中读到基础设施层是你必须使用休眠、JPA 或其他任何东西的地方.

The blue book says the repository is a way to represent collections on domain objects and is responsible to communicate with the infrastructure layer. I read also at some post the infrastructura layer is where you must use hibernate, JPA or whatever.

然后我看到这个 Spring-data-jpa 示例 http://spring.io/guides/gs/访问-data-jpa/,我变得疯狂.

Then I see this Spring-data-jpa example http://spring.io/guides/gs/accessing-data-jpa/ and I become crazy.

口号是 Spring-data-jpa 是为了轻松创建存储库,而前面的示例似乎将 JPA 注释合并到域对象(customer)中.

The slogan say Spring-data-jpa is to create repositories easily and the previous samples seems to merge JPA annotations into a domain object (the customer).

样品是对的吗?还是我说得对?

Is the sample right? or Am I right?

如果我是对的并且域和基础设施必须分开,那就意味着存储一个我必须拥有的客户:

If I'm right and the domain and infrastructure must be separated, that means to store a customer I must have:

  • 域层中的 Customer 类(代表客户并具有所有逻辑操作)
  • a CustomerRepository 是我的域层(从基础设施层检索或存储客户)
  • 基础设施层的Customer类,可能用@Entity注解
  • 一些CustomerReposityJPA 知道如何从数据库中存储/检索客户.
  • a Customer class in my domain layer (that represents a customer and has all the logic operations)
  • a CustomerRepository un my domain layer (that retrieves or stores customers from infrastructure layer)
  • a Customer class in infrastructure layer, probably annotated with @Entity
  • Some CustomerReposityJPA that know how to store/retrieve customers from database.

感谢您的澄清.

推荐答案

在 DDD 中,存储库是一个对象,它参与域但实际上抽象了一些后备存储.

In DDD, a repository is an object that participates in the domain but really abstracts away some backing store.

如果您使用 JPA 注释来注释域对象,那么您的持久性机制已经渗透到您的域中.您已将域结构与不理想的持久性结构联系起来.

If you annotate your domain objects with JPA annotations, your persistence mechanism has bled into your domain. You have tied your domain structure to your persistence structure which is not ideal.

您的 JpaCustomerRepository(实现 ICustomerRepository)可以将未注释的域类 (Customer) 映射到带注释的 JPA 表示 - JPA 客户.这将注释排除在域类之外,因此更干净.它允许您独立于域结构改变 JPA 持久性结构.这种好处的代价是映射代码的复杂性.

Your JpaCustomerRepository (implements ICustomerRepository) could map the unannotated domain classes (Customer) into an annotated JPA representation - JPA customer. This keeps the annotations out of your domain classes, so is cleaner. It allows you to vary the JPA persistence structure independently of your domain structure. The cost for this benefit is the complexity of the mapping code.

interface ICustomerRepository {}

class JpaCustomerRepository implements ICustomerRepository {
     void save(Customer customer) {
          JpaCustomer jpaCustomer = map(customer);
          save(jpaCustomer);
     }
}

class Customer {}

class JpaCustomer {}

这篇关于DDD、域实体/VO 和 JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:45