Consider my scenario of a model consisting of two aggregate roots, Customer
and Order
as well as a "shared" entity Address
.
Also note that Address
is abstract
has the following subclasses: PhysicalAddress
, PostOfficeBoxAddress
and PrivateBagAddress
.
A Customer
can have many addresses organized into some sort of address book.
Upon making an order a customer would select an Address
from their address book to be used as a delivery address.
My initial thoughts were to share an address between the two entities, but I have since opted out as it will cause trouble with managing the respective invariants.
Another option I could go for is to create two hierarchies of Address
, each for their purposes as a customer address or delivery address. This again doesn't seem right as there is a lot of repeated code.
How would I model this situation properly?
An entity is something that should be able to exist by ifself such as a customer or order. However an Address is not an entity, an Address is a value type and cannot exist on its own hence:
We find that we come across these types of entities all the time such as Address, MoneyType etc.
Solution would be to create 1 Address hierarchy value type in your domain. Then any entity can have an Address as a property where applicable