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?
Currency
andFederalState
thanMoney
andAddress
. – Gley