Issue
I wanted to bring my question to the bigger audience as we already discussed for some time in our company and cannot find an answer.
Let us suppose we have Transaction object which is aggregate root. Inside Transaction we have Money which is value object.
class Transaction {
private Money money;
// other stuff
}
and
class Money {
private BigDecimal amount;
private String currency;
}
Such Transaction can be persisted (we use hibernate) to db into simple table transaction
+-----------------+
| transaction |
+-----------------+
|id : bigint |
|amount: decimal |
|currency: varchar|
|other... |
+-----------------+
And everything would be great but... customer requires us to have currency table in database (we call them dictionary tables) and every table (including transaction) that have money need to point to the currency table:
+-----------------+ +-----------------+
| transaction | |curency |
+-----------------+ +-----------------+
|id : bigint | +---> | id: bigint |
|amount: decimal | | | code: varchar |
|curr_id: bigint | ----+ | name: varchar |
|other... | +-----------------+
+-----------------+
So from now on, Money object should look like this:
class Money {
private BigDecimal amount;
private Currency currency;
}
And from now we cannot call it value object :( or can we? It also complicates the way we are persisting the object as we cannot use hibernate embedable any more as we need to write our own custom type enter link description here. For sure we are not the first one facing this problems as dictionary types are popular in use, question is, how to treat them in scope of ddd modeling.
We will face similar problem when dealing with adresses. So, we know we have dictionaries like Country and FederalState (which are hierarchical). And we also know that many object in our application (e.g. Institution) have thei own adresses but also have connection to FederalState. So in simpe case we would have:
class Institution {
Address address;
// ...
}
where
class Address {
String street;
String houseNo;
// etc..
String federalState;
String country;
}
But we need it to have relation to fedral state table, therefore Address will look like:
class Address {
String street;
String houseNo;
// etc..
FederalState federalState;
}
so we face again the same problem, Address is not value object from now on. We know how to do it technically but what is the right approach from the perspective od ddd?
Solution
"what is the right approach from the perspective od ddd"
First of all, using dictionary entities is not wrong. Be it only for the reasons that, unlike a string value, using a relation to a dictionary entity:
- avoids typos (more robust)
- allows managing the set of "values" without modification in the code - you simply load the dictionary from the storage and populate a drop-down instead of defining, say, an Enum in the code (more flexible).
Letting aside the two above, business requirements may impose this design. E.g. for the Currency
case: when expressed as an entity allows the definition of the Exchange Rate
relation, which in itself may be subject to an 'auditable record/history' approach for storing the evolution of the exc.rate over time.
Having a State
dictionary is a good base for (possible future) extensions handling different sale tax policies, or legislative restrictions (product/service not allowed in some states - see 'medicinal' weeds and what not).
Answered By - Adrian Colomitchi
Answer Checked By - David Goodson (JavaFixing Volunteer)