Handling Master Information in Domain Driven Design (DDD)

Asked 2 years ago, Updated 2 years ago, 121 views

I'm designing a system with DDD and clean architecture, but I'm having trouble handling ValueObjects that are persistent in the master table.

ProductEntity {id, name, price, rank}
Yes, rank is based on the price value.
In terms of DB, there is a rank table that maintains the base price and the rank based on the base price.
(Example: Rank A for 0 yen or more, Rank B for 5000 yen or more, Rank C for 10,000 yen or more)

From the persistent state, searching for rank based on price can be done by ProductRepository, so there is no problem.

The problem is that the factory that generates the product cannot get rank information.
When adding a new product, only name and price are input externally.

ProductEntity cannot be created because use cases cannot directly retrieve Rank, which is a ValueObject.
Repository and Service cannot take external data as arguments = By taking domain model as arguments, it cannot implement the part of creating a new Entity and perpetuating it.

Is there any good way to implement it in such a case?
Or is the design strange?

domain-driven-design architecture

2022-09-30 21:18

1 Answers

I don't really understand what the restrictions are, perhaps because I'm not familiar with the rules of clean architecture, but I would design it like this...

First of all, it is domain knowledge that decides Rank based on Price, so we model the domain.
Now let's say RankPolicy entity

RankPolicy
 + calculateRank (price:Price): Rank

RankPolicyRepository
 + get() —RankPolicy

First of all, I will get the RankPolicy from RankPolicyRepository.
Then, pass this RankPolicy to ProductFactory and call RankPolicy#calculateRank() from ProductFactory to find the rank.


2022-09-30 21:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.