How Rich Models Supercharge DDD Entity Design and Boost Maintainability
Rich (or active) models embed behavior and aggregation relationships directly within DDD entities, contrasting with anemic models; this approach clarifies business logic, reduces glue code, and improves readability, though it can cause entity bloat, prompting strategies like external service classes, utility helpers, and careful injection handling.
Background
Rich model is a DDD entity design approach that focuses on business implementation, improving development efficiency and maintainability.
Overall Call Relationship in DDD Project
In the call relationship diagram, Entity is used from entering the domain service (Domain) until the final return.
Entity Design
Rich Model Overview
Rich model is a method where entities contain specific behavior methods and aggregation relationships.
Key terms: domain service → aggregate → aggregate root → entity → anemic model → rich model.
Aggregate and Aggregate Root
An aggregate is a relationship, and the aggregate root is the foundation of that relationship; without it, the aggregate cannot exist.
Example: three entities—User, UserGroup, UserGroupRelation—form an aggregate, with User as the aggregate root.
Entity
Defined in the domain layer, entities are core elements; they may map to database tables but can also represent one-to-many or in‑memory objects.
Anemic Model
Entities without behavior methods or aggregation relationships, essentially value objects used for data transfer; not recommended.
Rich Model
Entities contain behavior methods (e.g., create, save, delete) and aggregation references, allowing operations through the entity.
In practice, using rich models hides glue code, improves readability, and keeps focus on business logic.
Problems with Rich Model
Too much behavior can make entities bloated and hard to read or maintain; solutions depend on the amount of behavior code.
Solutions
Scenario 1: Behavior does not bloat the entity
public CooperateServicePackageConfig save() {
// Directly call infrastructure layer to save
cooperateServicePackageConfigRepository.save(this);
return this;
}Scenario 2: Behavior causes entity bloat
Move behavior to external classes.
1) Utility class
public CooperateServicePackageConfig save() {
// Delegate processing to utility class
ServicePackageSaveUtils.save(this);
return this;
}2) New entity for specific behavior
Practical Experience
1. Spring bean injection: static injection within entities.
private LabelInfoRepository labelInfoRepository = ApplicationContextUtils.getBean(LabelInfoRepository.class);2. Entity serialization: exclude unnecessary properties for Redis caching.
// Exclude serialization attribute
@Getter(AccessLevel.NONE)
private LabelInfoRepository labelInfoRepository = ApplicationContextUtils.getBean(LabelInfoRepository.class);
// Exclude serialization with JSONField
@JSONField(serialize = false)
private ServicePackageConfig servicePackageConfig;
// Exclude getter method from serialization
@Transient
@JSONField(serialize = false)
public static CooperateServicePackageRepositoryQuery getAllCodeQuery(Long contractId) {
CooperateServicePackageRepositoryQuery repositoryQuery = new CooperateServicePackageRepositoryQuery();
repositoryQuery.setContractIds(Lists.newArrayList(contractId));
repositoryQuery.setCode(RightsPlatformConstants.CODE_ALL);
return repositoryQuery;
}3. Using setter to establish aggregate binding.
public void setServiceSkuInfos(List<ServiceSkuInfo> serviceSkuInfos) {
if (CollectionUtils.isEmpty(serviceSkuInfos)) {
return;
}
this.serviceSkuInfos = serviceSkuInfos;
List<String> allSkuNoSet = serviceSkuInfos.stream()
.map(one -> one.getSkuNo())
.collect(Collectors.toList());
String skuJoinStr = Joiner.on(GlobalConstant.SPLIT_CHAR).join(allSkuNoSet);
this.setSkuNoSet(skuJoinStr);
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JD Cloud Developers
JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
