DDD,域实体/VO和JPA [英] DDD, domain entities/VO and JPA

查看:399
本文介绍了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类(代表客户并具有所有逻辑操作)
  • 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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