NHibernate: Mapping multiple classes from a single table row
Posted
by Michael Kurtz
on Stack Overflow
See other posts from Stack Overflow
or by Michael Kurtz
Published on 2010-04-06T06:06:56Z
Indexed on
2010/04/06
6:13 UTC
Read the original article
Hit count: 362
I couldn't find an answer to this specific question. I am trying to keep my domain model object-oriented and re-use objects where possible. I am having an issue determining how to provide a mapping to multiple classes from a single row. Let me explain with an example:
I have a single table, call it Customer. A customer has several attributes; but, for brevity, assume it has Id, Name, Address, City, State, ZipCode.
I would like to create a Customer and Address class that look like this:
public class Customer {
public virtual long Id {get;set;}
public virtual string Name {get;set;}
public virtual Address Address {get;set;}
}
public class Address {
public virtual string Address {get;set;}
public virtual string City {get;set;}
public virtual string State {get;set;}
public virtual string ZipCode {get;set;}
}
What I am having trouble with is determining what the mapping would be for the Address class within the Customer class. There is no Address table and there isn't a "set" of addresses associated with a Customer. I just want a more object-oriented view of the Customer table in code. There are several other tables that have address information in them and it would be nice to have a reusable Address class to deal with them. Addresses are not shared so breaking all addresses into a separate table with foreign keys seems to be overkill and, actually, more painful since I would need foreign keys to multiple tables.
Can someone enlighten me on this type of mapping? Please provide an example if you can.
Thanks for any insights!
-Mike
© Stack Overflow or respective owner