Ryan Lanciaux

Random programming blog.

RhinoCommons, NHibernate and ASP.NET MVC Part 2 - Configuration

Following up on my last post, we're going to setup a project and get everything ready for the code (we'll be doing the coding very soon – I promise).  First off, create a new MVC application (make sure you're using the latest preview from codeplex) and a new Class library. From here, you'd normally want to want to do some TDD to create your model but that's a little outside the scope of this example.

Add the references to Boo, Castle, NHibernate, RhinoCommons and Log4Net to the MVC application. In the class library, add Castle.ActiveRecord, Iesi.Collections, NHibernate, Rhino.Commons and Rhino.Commons.NHibernate. Switch over to your web.config file and Underneath the ConfigSections node add the following custom tags:

        <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />

        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

            <section name="Rhino.Commons.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

        </sectionGroup>   

Next add the specific custom tag properties somewhere after the </ConfigSections> : 

    <activerecord isWeb="true">

        <config>

            <add key="hibernate.connection.driver_class"

                value="NHibernate.Driver.SqlClientDriver" />

            <add key="dialect"

                value="NHibernate.Dialect.MsSql2005Dialect" />

            <add key="hibernate.connection.provider"

                value="NHibernate.Connection.DriverConnectionProvider" />

            <add key="hibernate.show_sql"

                value="false" />

            <add key="connection.connection_string" value="Data Source=___________;Initial Catalog=NHibernateTest;Integrated Security=True" />

        </config>

    </activerecord>

These active record settings should be pretty straight-forward but for more information on specific dialects or other properties check out the Castle's Configuration Reference. Be sure to swap out my Data Source and Initial Catalog settings with yours.


    <applicationSettings>

        <Rhino.Commons.Properties.Settings>

            <setting name="WindsorConfig"

                    serializeAs="String">

                <value>windsor.boo</value>

            </setting>

        </Rhino.Commons.Properties.Settings>

    </applicationSettings>

With this tag, we're telling Castle that we're going to configure Windsor with a boo file instead of an xml document. Ayende Rahien pointed out in the comments that this tag is no longer necessary as long as the file is named windsor.boo

Windsor Configuration With Boo 

Up until this point, we've been dealing with the web.config to configure our application – now we want to configure Windsor but instead of using another xml file, we're going to use a boo file. What is Boo you might ask? According to wiki

Boo is an object oriented, statically typed programming language developed starting in 2003, which seeks to make use of the Common Language Infrastructure support for Unicode, internationalization and web style applications, while using a Python-inspired syntax and a special focus on language and compiler extensibility. 

The mere fact that you can use a programming language instead of an XML file to configure Windsor is pretty sweet. I would be lying if I claimed to know boo very well, however, the Exesto and Hibernating-Forums samples (from the Rhino-Tools project) have enough information to get you up and running. I plan on learning boo well enought to create my own config files from scratch but in the mean time, here's what my boo file looks like (heavily influenced by the sample applications mentioned above)…

import Rhino.Commons

import System.Reflection

import Castle.Core

import Castle.Services.Transaction

import Castle.Facilities.AutomaticTransactionManagement

 

activeRecordAssemblies = ( Assembly.Load("ProductModelActiveRecord"), )

 

Component("active_record_repository", IRepository, ARRepository)

Component("active_record_unit_of_work",

    IUnitOfWorkFactory,

    ActiveRecordUnitOfWorkFactory,

    assemblies: activeRecordAssemblies )

Check out Ayende's comment for a more succinct way to register these components. As you might have noticed, I still have to set up the colors for boo files in Visual Studio :) What this file is doing is loading the assemblies and setting up the repository / unit of work (we'll see those in action in the next parts of this series). Your project configuration should be all set. Next time we will actually be writing some code so stick around for that. View Part Three - The Model