Ang3lFir3 – Life as a Code Poet

August 17, 2009

Updates to Subclass and JoinedSubclass mapping in Fluent nHibernate 1.0 RC

Filed under: .NET, C#, Fluent NHibernate, NHibernate — Tags: , , , , — ang3lfir3 @ 3:20 pm

With the release of Fluent nHibernate 1.0 RC (download here) there are some great changes to the mapping structure of Subclass and JoinedSubclass. This excerpt from the 1.0 RC release notes gives a highlight of what those changes are and what is expected of us:

Separated subclass mapping – Subclasses can (and should be) defined separately from their parent mapping. Use SubclassMap<T> the same way as you would ClassMap<T>; if the top-most mapping (ClassMap) contains a DiscriminateSubclassesOnColumn call, the subclasses will be mapped as table-per-class-hierarchy, otherwise (by default) they’ll be mapped as table-per-subclass.

Okay so now we have quite a bit of info, but what does it all mean for the code. For those of you who just want to see the new mappings… I’ve got that for you. Using the same samples that I used in Table Per Class Hierarchy Inheritance Mapping with Fluent nHibernate and Table Per Subclass Inheritance Mapping with Fluent nHibernate I will show you the changes. Which are pretty simple and vary only slightly between the two approaches. This allows you to change mapping styles very quickly and flexibly.

 

The Mappings:

The Mappings for the Subclasses pretty much stay the same. The optional column name portions of the mapping are most helpful in mapping Table Per Class Hierarchy.

  public class BankAccountMap : SubclassMap<BankAccount>
  {
    public BankAccountMap()
    {
      Map(x => x.BankName, "BankAccount_BankName");
      Map(x => x.RoutingNumber, "BankAccount_RoutingNumber");
    }
  }

  public class CreditCardMap : SubclassMap<CreditCard>
  {
    public CreditCardMap()
    {
      Map(x => x.Type, "CreditCard_Type");
      Map(x => x.ExpirationMonth, "CreditCard_ExpirationMonth");
      Map(x => x.ExpirationYear, "CreditCard_ExpirationYear");
    }
  }

Here you can see the base class mapping for BillingDetails. To use a JoinedSubclass mapping as opposed to the Subclass mapping you just simply omit the DiscriminateSubClassesOnColumn() call.

  public class BillingDetailsMap : ClassMap<BillingDetails>
  {
    public BillingDetailsMap()
    {
      Id(x => x.Id);
      Map(x => x.Number);
      Map(x => x.Owner);
      Map(x => x.DateCreated);
      DiscriminateSubClassesOnColumn("BillingDetailsType");
    }
  }

kick it on DotNetKicks.com

Advertisements

August 6, 2009

Table Per Subclass Inheritance Mapping with Fluent nHibernate

Filed under: .NET, C#, Fluent NHibernate, NHibernate, ORM, Patterns — Tags: , , , , , , — ang3lfir3 @ 2:45 pm

Note: Updates for Fluent nHibernate 1.0 RC can be read here

This is the third in a short series of posts I am working on. While reading through NHibernate in Action I thought to my self that I should create these same examples of Inheritance mapping using Fluent nHibernate to show how simple these mappings can be.

The three types of Inheritance mappings that are mentioned on page 92 of NHibernate in Action are :

Model

  • Table Per Concrete Class
  • Table Per Class Hierarchy
  • Table Per Subclass

I will be using the example from NHibernate in Action to illustrate the mappings. This post covers the Table Per Subclass example. In Table per Subclass inheritance mapping we represent inheritance by using a foreign key to join tables together as needed.  This most closely represents the object model in the relational model of the database by using a table for the base class and one for each subclass.

TablePerSubclassFiles

Here you can see that again I only have one mapping file which covers the entire class Hierarchy.

Below you can see the Fluent Mappings for this approach. The JoinedSubClass<T> uses the Type as the table name by default (“CreditCard” and “BankAccount”) and the first parameter of the method is the name to use for the foreign key column. Additionally the mapping for the class being joined is included.

  public class BillingDetailsMap : ClassMap<BillingDetails>
  {
    public BillingDetailsMap()
    {
      Id(x => x.Id);
      Map(x => x.Number);
      Map(x => x.Owner);
      Map(x => x.DateCreated);
      JoinedSubClass<CreditCard>("CreditCard_Id", m =>
                                                    {
                                                      m.Map(c => c.Type);
                                                      m.Map(c => c.ExpirationMonth);
                                                      m.Map(c => c.ExpirationYear);
                                                    });
      JoinedSubClass<BankAccount>("BankAccount_Id", m =>
                                                      {
                                                        m.Map(b => b.BankName);
                                                        m.Map(b => b.RoutingNumber);
                                                      });
    }
  }

Which maps to the following nHibernate XML mapping file.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="">
  <class name="TablePerSubclass.Model.BillingDetails, TablePerSubclass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`BillingDetails`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int32" column="Id">
      <generator class="identity" />
    </id>
    <property name="Number" type="String">
      <column name="Number" />
    </property>
    <property name="Owner" type="String">
      <column name="Owner" />
    </property>
    <property name="DateCreated" type="DateTime">
      <column name="DateCreated" />
    </property>
    <joined-subclass name="TablePerSubclass.Model.CreditCard, TablePerSubclass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <key column="CreditCard_Id" />
      <property name="Type" type="Int32">
        <column name="Type" />
      </property>
      <property name="ExpirationMonth" type="String">
        <column name="ExpirationMonth" />
      </property>
      <property name="ExpirationYear" type="String">
        <column name="ExpirationYear" />
      </property>
    </joined-subclass>
    <joined-subclass name="TablePerSubclass.Model.BankAccount, TablePerSubclass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <key column="BankAccount_Id" />
      <property name="BankName" type="String">
        <column name="BankName" />
      </property>
      <property name="RoutingNumber" type="String">
        <column name="RoutingNumber" />
      </property>
    </joined-subclass>
  </class>
</hibernate-mapping>

And the corresponding Database Schema. I followed the naming from the NHibernate in Action so you can see here maybe this is a little confusing. But basicly the “BankAccount_Id” is the primary key of the BankAccount table and contains is also a foreign key to the BillingDetails table.

TablePerSubclassSchema

kick it on DotNetKicks.com

August 4, 2009

Table Per Class Hierarchy Inheritance Mapping with Fluent nHibernate

Filed under: .NET, C#, Fluent NHibernate, NHibernate, ORM, Patterns — Tags: , , , , , — ang3lfir3 @ 2:32 pm

Note: Updates for Fluent nHibernate 1.0 RC can be read here

This is the second in a short series of posts I am working on. While reading through NHibernate in Action I thought to my self that I should create these same examples of Inheritance mapping using Fluent nHibernate to show how simple these mappings can be.

The three types of Inheritance mappings that are mentioned on page 92 of NHibernate in Action are :Model

  • Table Per Concrete Class
  • Table Per Class Hierarchy
  • Table Per Subclass

I will be using the example from NHibernate in Action to illustrate the mappings. This post covers the Table Per Class Hierarchy example. In Table per Class Hierarchy  inheritance mapping we enable polymorphism by using a type discriminator column to determine the Type when rehydrating objects. This means that all of the properties of all the subclasses including those of the base class are included in the single table. All columns that are not part of the base class must be nullable allowing the single row to reflect only the data required for the type being stored.

TablePerHierarchyFiles

Here you can see I have only one mapping file. This mapping file covers the entire class Hierarchy in a single file.

Below you can see the Fluent Mappings for this approach. The “DiscriminateSubClassOnColumn” indicates that name of the of the column in the Database that should be used to descriminate the types of each subclass. Fluent nHibernates default behaviour is to use the Class name as the descriminiator value. You can see how each Subclass is mapped. Notice that I used a prefix on the column names for the Subclass mappings. This allows the database schema to be easily readable.

  public class BillingDetailsMap : ClassMap<BillingDetails>
  {
    public BillingDetailsMap()
    {
      Id(x => x.Id);
      Map(x => x.Number);
      Map(x => x.Owner);
      Map(x => x.DateCreated);
      DiscriminateSubClassesOnColumn("BillingDetailsType")
        .SubClass<CreditCard>(m =>
                                {
                                  m.Map(c => c.Type, "CreditCard_Type");
                                  m.Map(c => c.ExpirationMonth, "CreditCard_ExpirationMonth");
                                  m.Map(c => c.ExpirationYear, "CreditCard_ExpirationYear");
                                })
        .SubClass<BankAccount>(m =>
                                 {
                                   m.Map(b => b.BankName, "BankAccount_BankNumber");
                                   m.Map(b => b.RoutingNumber, "BankAccount_RoutingNumber");
                                 });
    }
  }

Which maps to the following nHibernate XML mapping file.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="">
  <class name="TablePerHierarchy.Model.BillingDetails, TablePerHierarchy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`BillingDetails`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int32" column="Id">
      <generator class="identity" />
    </id>
    <discriminator column="BillingDetailsType" type="String" />
    <property name="Number" type="String">
      <column name="Number" />
    </property>
    <property name="Owner" type="String">
      <column name="Owner" />
    </property>
    <property name="DateCreated" type="DateTime">
      <column name="DateCreated" />
    </property>
    <subclass name="TablePerHierarchy.Model.CreditCard, TablePerHierarchy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <property name="Type" type="Int32">
        <column name="CreditCard_Type" />
      </property>
      <property name="ExpirationMonth" type="String">
        <column name="CreditCard_ExpirationMonth" />
      </property>
      <property name="ExpirationYear" type="String">
        <column name="CreditCard_ExpirationYear" />
      </property>
    </subclass>
    <subclass name="TablePerHierarchy.Model.BankAccount, TablePerHierarchy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <property name="BankName" type="String">
        <column name="BankAccount_BankNumber" />
      </property>
      <property name="RoutingNumber" type="String">
        <column name="BankAccount_RoutingNumber" />
      </property>
    </subclass>
  </class>
</hibernate-mapping>

And finally you can see the relational model this maps to. Every property of every type is represented as a column. Those of the base class are included as well. You can see the discriminator column “BillingDetailsType” as well.

TablePerHierarchySchema

kick it on DotNetKicks.com

Table Per Class Inheritance Mapping with Fluent nHibernate

Filed under: C#, Fluent NHibernate, NHibernate, ORM, Patterns — Tags: , , , , , — ang3lfir3 @ 10:45 am

This is the first in a short series of posts I am working on. While reading through NHibernate in Action I thought to my self that I should create these same examples of Inheritance mapping using Fluent nHibernate to show how simple these mappings can be.

The three types of Inheritance mappings that are mentioned on page 92 of NHibernate in Action are :

  • Table Per Concrete Class Model
  • Table Per Class Hierarchy
  • Table Per Subclass

I will be using the example from NHibernate in Action to illustrate the mappings. This post covers the most basic and often most common example, Table Per Class. In Table per concrete class inheritance mapping we discard polymorphism and inheritance from the relational schema and create as the name suggests a single table for each concrete class. This means that the DateCreated , Number, and Owner properties of the base class will be reflected in each table. This can be dangerous as these column’s datatypes must now stay in sync across the two tables. If a change needs to be made to the schema you will have to find all the tables that inherit from BillingDetails and edit their schemas independently. This is not necessarily a problem when dealing with a few classes but can be come a problem in a larger and more complex model.

TablePerClassFiles

Here you can see that in this case I have only two mapping files. One for each inheritor.

Below you can see the Fluent nHibernate mapping classes that show how simple this mapping is.

  public class BankAccountMap : ClassMap<BankAccount>
  {
    public BankAccountMap()
    {
      Id(x => x.Id, "BankAccount_Id");
      Map(x => x.Number);
      Map(x => x.Owner);
      Map(x => x.DateCreated);
      Map(x => x.BankName);
      Map(x => x.RoutingNumber);
    }
  }

  public class CreditCardMap : ClassMap<CreditCard>
  {
    public CreditCardMap()
    {
      Id(x => x.Id, "CreditCard_Id");
      Map(x => x.Number);
      Map(x => x.Owner);
      Map(x => x.DateCreated);
      Map(x => x.Type);
      Map(x => x.ExpirationMonth);
      Map(x => x.ExpirationYear);
    }
  }

Which maps to the following nHibernate mapping XML:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="">
  <class name="TablePerClass.Model.BankAccount, TablePerClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`BankAccount`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int32" column="BankAccount_Id">
      <generator class="identity" />
    </id>
    <property name="Number" type="String">
      <column name="Number" />
    </property>
    <property name="Owner" type="String">
      <column name="Owner" />
    </property>
    <property name="DateCreated" type="DateTime">
      <column name="DateCreated" />
    </property>
    <property name="BankName" type="String">
      <column name="BankName" />
    </property>
    <property name="RoutingNumber" type="String">
      <column name="RoutingNumber" />
    </property>
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="">
  <class name="TablePerClass.Model.CreditCard, TablePerClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`CreditCard`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int32" column="CreditCard_Id">
      <generator class="identity" />
    </id>
    <property name="Number" type="String">
      <column name="Number" />
    </property>
    <property name="Owner" type="String">
      <column name="Owner" />
    </property>
    <property name="DateCreated" type="DateTime">
      <column name="DateCreated" />
    </property>
    <property name="Type" type="Int32">
      <column name="Type" />
    </property>
    <property name="ExpirationMonth" type="String">
      <column name="ExpirationMonth" />
    </property>
    <property name="ExpirationYear" type="String">
      <column name="ExpirationYear" />
    </property>
  </class>
</hibernate-mapping>

And finally here you see the relational model that this maps to. The base class properties are stored as columns of each table.
TablePerClassSchema

kick it on DotNetKicks.com

Blog at WordPress.com.