DDD로 개발을 하기 위해서는 DDD를 이루는 요소들을 알아야 된다.
- Entities
- Value objects
- Aggregate roots
- Repositories
- Factories
- Services
그럼 허리띠 졸라 매시고 렛츠고!

1. Entity
- 엔티티는 가변성의 ID를 가진 plain 객체이다.
- 엔티티는 ID만으로 동일성을 보장할 수 있다.
2. Value Object
- 값 객체는 불변이고 ID도 없다.
- 모든 속성의 값이 같아야 동일성이 보장된다.
- 불변성 덕분에 API 레이어에서 외부 노출에 자유롭다.
- Some benefits
- 값 객체를 여러게 합성해서 복잡도를 줄일 수 있다.
- 엔티티의 복잡도를 낮출 수 있다.
- 잘 사용한다면 (testablilty와 동시성 이유)를 개선할 수 있다.
3. Aggregate Roots
- Aggregate - 연관 객체의 묶음, Aggregate Root - 연관 객체 묶음의 루트
- Aggregate 중 Global Identity를 가진 Entity
- Aggregate = Aggregate root + boundary
- e.g. the relationship between Order and OrderLineItem within SalesOrderDomain can be considered as an aggregate where Order acts as the aggregate root, while the OrderLineItem is the child of Order within SalesOrder boundary.
- Aggregate Root의 특징
- Aggregate 의 바깥 객체 (boundary 밖의 객체)는 Root의 Child (Boundary)에 대한 참조를 가질 수 없다.
- 참조를 가지고 싶으면 Root를 통해서 가져가라!
- 도메인 내의 Factoriest, Repositories, Services 를 제외한 다른 모든 도메인 내의 Operation들은 Aggregate Root를 통하게 하는 것이 좋다!
4. Repository
- 리포지토리는 Aggregate Root를 보관한다.
5. Factories
- 팩토리는 객체 생성을 추상화한다.
- 팩토리는 루트 객체와 그냥 엔티티, 값 객체 등을 생성할 수 있다.
- 보통 팩토리는
- domain/domain 서비스 레이어에 interface를 가지고
- infrastructure layer에 구현로직을 가진다.
6. Services
- 서비스는 루트가 아닌 operation들을 모아 놓는 곳이다.
- operation에 적절한 루트를 부여하기 어렵다고 모든 기능을 service에 때려박지 말자
VO vs DTO

- DTO는 그냥 생성자, Getter, Setter 만 가진 껍데기임
- 값 객체는 로직을 가지고 도메인 모델 안에서 활용됨