Castle AR and NHibernate

It's been a while since my last post. So a bit update first.

Lots of things actually happened or happening in July. I started a new job from the start of the July, hence very busy! I also needed to help out my former employer for the hiring of the new developer. The interview process was quite tiring, but very different and very challenging. I actually enjoyed it and I believe I learnt a lot from it.

As for the technical development side, with my new job, I'm doing a new project in Zend Framework of course. I'm getting my grasp of it and hopefully I can make some valuable contributions along the way. I'm also finishing off a C#.Net project for my former employer as well. This blog post will be about the 2 .Net libraries that I used for this project.

For this .Net project, I started with using the NHibernate ORM library. I found my hate of XML configuration pretty soon after I kick started the project. Consider the complexity of this application is quite low, I jumped on the Castle ActiveRecord library, which uses NHibernate as the underlying ORM.

Don't want to offend anyone, but the documentation of both libraries are quite average. So here are some STUFF I had to trial and error to find out myself.

1st, for a ASP.Net project, here's part of the Web.config to get Castle going and also logging the raw SQLs that are generated by NHibernate.

<configSections>
    <!-- your normal stuff ... -->
    <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>

<activerecord isWeb="true" isDebug="true" >
    <config>
        <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
        <add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
        <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
        <add key="connection.connection_string" value="Data Source=JIMLIWIN7\SQLEXPRESS;Initial Catalog=DBNAME;Persist Security Info=True;User ID=USER;Password=PASSWORD" />
        <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
        <add key="show_sql" value="true" />
    </config>
</activerecord>

<log4net>
    <appender name="NHibernateRollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="LogNHibernate.txt"/>
        <appendToFile value="true"/>
        <rollingStyle value="Size"/>
        <datePattern value="yyyyMMdd"/>
        <maxSizeRollBackups value="10"/>
        <maximumFileSize value="10MB"/>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date - %message%newline"/>
        </layout>
    </appender>

    <logger name="NHibernate.SQL" additivity="false">
        <level value="DEBUG"/>
        <!-- ALL, DEBUG, INFO, WARN, ERROR, FATAL or OFF -->
        <appender-ref ref="NHibernateRollingFileAppender"/>
    </logger>
</log4net>

2nd, in my entity classes, I have properties that are linked to nullable fields. To get this whole AR thing happy, I do the following. The trick is the "?".

private System.DateTime? _dateDeleted;

[Property("DateDeleted", ColumnType = "Timestamp")]
public virtual System.DateTime? DateDeleted
{
    get
    {
        return this._dateDeleted;
    }
    set
    {
        this._dateDeleted = value;
    }
}

There are other things that have killed me hours. But I think those are just because of my own stupidity and incompetency in .NET ... So I'll keep those to myself. Bye for now.

Published: 2010-08-01