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 EntityAdressType
(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 theUser
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 find the correct type then return it? Is there a more efficient way than this?
Best Answer
I think that the type is not a property of the Address, it's a property of the connection between a User and an Address.
An Address normally represents a specific location in the world, a type would be connected to the use that a User makes of the Address.
An Address might even be used for both Home and Business for some Users. Or be the Home of one User and the Workplace of another User.
So: