Sun 12 Aug 2007
Using NHibernate mapping with AdventureWorks
Posted by Duvall Buck under AWSandbox, NHibernate
No Comments
There is not a lot of good examples for NHibernate mapping. I struggle to figure this stuff out but once I get it it always seems so simple.
In the AdventureWorks Person schema there is a foreign key reference from the StateProvince table to the CountryRegion table by the CountryRegionCode column. Here is the StateProvince class code:
namespace AWSandbox.Domain.Person {
public class StateProvince {
private int id;
private string stateProvinceCode;
private CountryRegion countryRegion;
private bool isOnlyStateProvinceFlag;
private string name;
private int territoryId;
public int Id {
get { return id; }
set { id = value; }
}
public string StateProvinceCode {
get { return stateProvinceCode; }
set { stateProvinceCode = value; }
}
public CountryRegion CountryRegion {
get { return countryRegion; }
set { countryRegion = value; }
}
public bool IsOnlyStateProvinceFlag {
get { return isOnlyStateProvinceFlag; }
set { isOnlyStateProvinceFlag = value; }
}
public string Name {
get { return name; }
set { name = value; }
}
public int TerritoryId {
get { return territoryId; }
set { territoryId = value; }
}
public string CountryRegionName {
get { return CountryRegion.Name; }
}
}
}
Notice that the CountryRegionName only has a getter and that it really is using the CountryRegion class to return the Name property.
Here is the mapping:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<hibernate-mapping xmlns=”urn:nhibernate-mapping-2.2″ assembly=”AWSandbox.Domain” namespace=”AWSandbox.Domain”>
<class name=”AWSandbox.Domain.Person.StateProvince, AWSandbox.Domain” table=”Person.StateProvince” lazy=”false”>
<id name=”Id” column=”StateProvinceID”>
<generator class=”native”/>
</id>
<property name=”StateProvinceCode” column=”StateProvinceCode” type=”string” />
<property name=”IsOnlyStateProvinceFlag” column=”IsOnlyStateProvinceFlag” type=”Boolean” />
<property name=”Name” column=”Name” type=”string” />
<many-to-one name=”CountryRegion”class=”AWSandbox.Domain.Person.CountryRegion” column=”CountryRegionCode” cascade=”all” />
</class>
</hibernate-mapping>
I’m using the <many-to-one> mapping to include the CountryRegion data with the StateProvince data. Now this is how you would display the data in a GridView:
<asp:GridView ID=”StateProvinceGridView” AutoGenerateColumns=”false” runat=”server”>
<Columns>
<asp:BoundField DataField=”Id” HeaderText=”ID”/>
<asp:BoundField DataField=”StateProvinceCode” HeaderText=”Code” />
<asp:BoundField DataField=”IsOnlyStateProvinceFlag” HeaderText=”Flag” />
<asp:BoundField DataField=”Name” HeaderText=”Name” />
<asp:BoundField DataField=”CountryRegionName” HeaderText=”Country Region” />
</Columns>
</asp:GridView>
I’m assigning an IList<StateProvince> objects to the GridView.DataSource and calling GridView.DataBind() to populate the grid. I’m also using the MVP (model view presenter) design pattern to move the data out of the database, through my domain, presentation and service layers and arriving in tabular format on the web page.