1. One hbm.xml file per class directly next to the class file.
    I know there are exceptions (SharpMapGis.hbm.xml) but this is how it should be.
  2. Say as little as possible in your hbm.xml
    so don't:
overspecifying redundant example (bad)
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false">
  <class name="DelftTools.DataObjects.Functions.IFunction, DelftTools.DataObjects.Functions"  table="function" lazy="false" >
    <id name="Id" column="id" type="System.Int64" unsaved-value="0">
      <generator class="increment" />
    </id>
    <discriminator column="type" type="string"/>
    <property name="Name" column="name" type="string" />
  </class>
  <class name="DelftTools.DataObjects.Functions.IVariable, DelftTools.DataObjects.Functions"     table="function" lazy="false" >
    <discriminator column="type" type="string"/>
  </class>
</hibernate-mapping>

But let nhibernate handle naming of tables and columns etc. This has an advantage of controlling the naming in a general way (NHibernate let's you configure naming conventions) and leaves you refactor proof. Also dont use column names,types tables etc.
So you get:

less specifying mapping(better)
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="DelftTools.DataObjects.Functions" assembly="DelftTools.DataObjects.Functions"  auto-import="false">
  <class name="IFunction" lazy="false" >
    <id name="Id">
      <generator class="increment" />
    </id>
    <discriminator column="type" type="string"/>
    <property name="Name"  />
  </class>
  <class name="IVariable" lazy="false" >
    <discriminator column="type" type="string"/>
  </class>
</hibernate-mapping>

Also check this post by Ayende about best practices: http://ayende.com/Blog/archive/2008/07/24/How-to-review-NHibernate-application.aspx

note: Unfortunately Nhibernate has some awkward defaults (like lazy classes) that require us specify lazy=false everywhere (lazy introduces some extra problems and should not be used as a default i think) maybe we can change the defaults?

  • No labels