DDD, domain entities/VO and JPA

前端 未结 2 1045
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 06:35

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

My question is related to my domain objects (entities, VO, ...) which represents my domain concepts/log

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 07:27

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

    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.

    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 {}
    

提交回复
热议问题