Ang3lFir3 – Life as a Code Poet

July 30, 2010

My technology stack

So following in the footsteps of my friend Liam I decided to respond to David Burela’s ‘What is your preferred technology stack?’ question.

By day I am lucky enough to work with the likes of @NotMyself @codingreflection and other great developers as an Enterprise Developer. Our customers are internal and the corporate preference is for .Net solutions. Our organization and management sees the value in letting developers be productive by allowing them to use the tools that best fit the job. I am proud to work with a team of developers that pushes me to improve myself each and every day.

In my day to day life I am just me, a geek with a passion for learning and all things… well… geeky.

quoting Liam I also favor tools that are :

  • are open source. Open source libraries are often designed to facilitate quality practices, not to sell tools. Also, access to the source and the price are bonuses.
  • support the software craftsmanship ideology. Small, sharp tools that stay out of the way and don’t force any design decisions.

My work stack:

web framework => MonoRail, ASP.Net MVC 2+
ui => JQuery w/ plugins, Ext JS
viewEngine => Brail, Spark, ASPX
Testing => MbUnit for unit testing, MSpec for BDD … I tend to do mostly just BDD now
TestRunner => TD.Net
Mocking => RhinoMocks. I’ve tried Moq and really don’t like it compared to Rhino
IoC => Windsor
exceptions => Log4net, we are increasing moving towards including Elmah as well
data access => NHibernate + Fluent NHibernate, SqlServer 2000
build => powershell + psake (make in powershell)
CI => TeamCity
version control =>  Git w/ git-svn
misc => NBuilder, SQLite for inmemory testing of repositories and mappings, FluentMigrator, WIX, FileHelpers, A Common lib of tools and useful bits our team has collected over time

My Personal dream stack:

web framework => ASP.Net MVC2+, RoR, Sinatra
ui => JQuery w/ plugins, Ext Js, Coffee Script
viewEngine => spark, haml
testing => MSpec, RSpec , Cucumber
mocking => RhinoMocks, some ruby mocking framework (lol)
version control => Git
build => rake + albacore where needed
IoC => Ninject
exceptions => Elmah, NLog
datastorage => MongoDb ….. pure awesome… on a stick

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

March 18, 2009

Fluent NHibernate new style mappings – powerful semantics

Filed under: C#, Fluent NHibernate, NHibernate, ORM, Uncategorized — Tags: , , , , , , — ang3lfir3 @ 5:56 pm

So today I updated to the latest build of Fluent NHibernate.  As any of you who might have done the same have discovered there are some breaking changes. I wasn’t sure I liked the new class based conventions at first, especially since it wasn’t clear at first how to tackle altering my mappings. Then it dawned on me how powerful the conventions would end up being while also promoting DRY.

as @jagregory said :

“brevity was sacrificed for power in this case.”

This can be seen in the example below which is a self referencing Parent Child relationship.

The description of the relationship is:

Categories can have one or none parents.

Categories can have many or no children.

The parent Category is always found in a property called “Parent”.

The children are always found in a property called “Children”.

Before

Original Mapping:

public class CategoryMap : ClassMap<Category>
{
  public CategoryMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    References(x => x.Parent).TheColumnIs("parent_id").Cascade.All();
    HasManyToMany(x => x.Products).Inverse();
    HasMany(x => x.Children).WithKeyColumn("parent_id").Cascade.All().Inverse();
  }
}

**Note:  These aren’t exactly “Conventions” but it turned out that ‘WithKeyColumn’ got dropped and I had to look for a better way. The new style conventions offered that even over older convention styles.

After

The new style Convention Mappings :

The Convention classes below create a convention that reads like:

“For a HasMany when the child type matches the type of the root and the name of the property is ‘Children’  then use the column ‘parent_id’ as the KeyColumn. For a Reference when the child type matches the type of the root and the name of the property is ‘Parent’ then set its reference ColumnName to ‘parent_id’ “

public class SelfReferencingHasManyConvention : IHasManyConvention
{
  public bool Accept(IOneToManyPart target)
  {
     return target.Member.ReflectedType == target.EntityType && target.Member.Name == "Children";
  }

  public void Apply(IOneToManyPart target)
  {
     target.KeyColumnNames.Clear();
     target.KeyColumnNames.Add("parent_id");
  }
}
public class SelfReferencingReferenceConvention : IReferenceConvention
{
  public bool Accept(IManyToOnePart target)
  {
    return target.Property.ReflectedType == target.EntityType && target.Property.Name == "Parent";
  }

  public void Apply(IManyToOnePart target)
  {
     target.ColumnName("parent_id");
  }
}

The Mapping after the convention :

Clean and clear, the conventions themselves are not cluttering the Mapping. More importantly the conventions help me stay DRY.

public class CategoryMap : ClassMap<Category>
{
  public CategoryMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    References(x => x.Parent).Cascade.All();
    HasManyToMany(x => x.Products).Inverse();
    HasMany(x => x.Children).Cascade.All().Inverse();
  }
}

Adding Mappings to my Persistence Model

You can see that adding the conventions was pretty easy and straight forward. This applies to the fact that I am using the PersistenceModel approach.

public class DataModel : PersistenceModel
{
  public DataModel()
  {
     AddMapping(new ProductMap());
     AddMapping(new CategoryMap());
     ConventionFinder.AddFromAssemblyOf<DataModel>();
   }
}

kick it on DotNetKicks.com

Blog at WordPress.com.