How to model an address type in DDD?
- by Songo
I have an User entity that has a Set of Address where Address is a value object:
class User{
...
private Set<Address> addresses;
...
public setAddresses(Set<Address> addresses){
//set all addresses as a batch
}
...
}
A User can have a home address and a work address, so I should have something that acts as a look up in the database:
tbl_address_type
------------------------------------------------
| address_type_id | address_type |
------------------------------------------------
| 1 | work |
------------------------------------------------
| 2 | home |
------------------------------------------------
and correspondingly tbl_address
-------------------------------------------------------------------------------------
| address_id | address_description |address_type_id| user_id |
-------------------------------------------------------------------------------------
| 1 | 123 main street | 1 | 100 |
-------------------------------------------------------------------------------------
| 2 | 456 another street | 1 | 100 |
-------------------------------------------------------------------------------------
| 3 | 789 long street | 2 | 200 |
-------------------------------------------------------------------------------------
| 4 | 023 short street | 2 | 200 |
-------------------------------------------------------------------------------------
Should the address type be modeled as an Entity or Value type? and Why?
Is it OK for the Address Value object to hold a reference to the Entity AdressType (in case it was modeled as an entity)? Is this something feasible using Hibernate/NHibernate?
If a user can change his home address, should I expose a User.updateHomeAddress(Address homeAddress) function on the User entity itself? How can I enforce that the client passes a Home address and not a work address in this case? (a sample implementation is most welcomed)
If I want to get the User's home address via User.getHomeAddress() function, must I load the whole addresses array then loop it and check each for its type till I found the correct type then return it? Is there a more efficient way than this?