I created a few simple ReSharper templates to make working with NHibernate and Castle ActiveRecord easier. They currently default to SQL Server 2005 because that’s what I do most of my work against. Feel free to customize to your heart’s content.

You can grab them from here. Simply import the templates into ReSharper via ReSharper… Options… Templates… Live (or File) Templates… User Templates… Import Templates from File… You’ll want to add the file templates to your Quick access list.

The first file template is hibernate.cfg.xml. (N.B. ReSharper will try to name it hibernate.cfg1 and add the xml extension. Delete the “1″ and hit enter.) The template will prompt you for the database server, database name, and mapping assembly.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="hibernate.connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="hibernate.connection.connection_string">Data Source=localhost;Initial Catalog=DATABASE;Integrated Security=True</property>
    <property name="hibernate.dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="hibernate.use_outer_join">true</property>
    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <mapping assembly="SimpleConfiguration"/>
  </session-factory>
</hibernate-configuration>

The second file template is <Class>.hbm.xml. You will want to rename it <Class>.hbm as ReSharper will add the xml extension for you. The template will prompt you for the class name and primary key name. You’ll also want to modify the PK generator. You’ll note that default-access is “field.pascalcase-m-underscore”, which means that if you map a property called “Name”, NHibernate will access it by the corresponding field name, “m_Name”. Change your default-access to whichever naming convention you prefer. (See property access and naming strategies in the NHibernate documentation.)

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="SimpleConfiguration"
                   assembly="SimpleConfiguration"
                   default-access="field.pascalcase-m-underscore"
                   default-lazy="false"
                   default-cascade="none" >
  <class name="CLASS">
    <id name="PRIMARYKEY">
      <generator class="assigned" />
    </id>
  </class>
</hibernate-mapping>

Lastly we have the Live template for Castle ActiveRecord configuration. You can insert this into app.config or web.config. Simple position your cursor inside <configuration> and type arconfig<TAB>. If you already have other configuration sections, you’ll have to merge the activerecord section with the others.

<configSections>
    <section name="activerecord"
             type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
</configSections>
 
<activerecord>
  <config>
    <add key="hibernate.connection.driver_class" 
         value="NHibernate.Driver.SqlClientDriver" />
    <add key="hibernate.dialect"                 
         value="NHibernate.Dialect.MsSql2005Dialect" />
    <add key="hibernate.connection.provider"     
         value="NHibernate.Connection.DriverConnectionProvider" />
    <add key="hibernate.connection.connection_string" 
         value="Data Source=DBSERVER;Initial Catalog=DATABASE;Integrated Security=SSPI" />
  </config>      
</activerecord>

Enjoy!