Ryan LanciauxNew Media Mercenary

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

May 20, 2008 by ryan

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


kick it on DotNetKicks.com
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList



Related posts

Comments

May 22. 2008 19:38

Ayende Rahien

A couple of points that were recently added:

* You don't need the <Rhino.Commons.Properties.Settings> section if you file is called windsor.boo anymore.
* You don't need to explicitly register the IRepository and IUnitOfWorkFactory, you can do just:

facility ActiveRecordUnitOfWorkFacility:
assembly = "ProductModelActiveRecord"

And it will handle everything for you

Ayende Rahien

May 22. 2008 21:01

Ryan Lanciaux

@Ayende Thanks a ton for pointing that out -- I will modify the post accordingly Smile This is really such a great framework!

Ryan Lanciaux

May 28. 2008 02:22

Michael Hanney

Thank you for the tip about not needing the Rhino.Commons.Properties.Settings. Great stuff.

Michael Hanney

June 22. 2008 23:42

Nikos Bilalis

Is it possible to connect to multiple databases with this configuration?

Nikos Bilalis

June 23. 2008 11:48

Ryan Lanciaux

@Nikos: I think it would be possible, however, I have not tried it. This link on the castle site shows how to use more than one database www.castleproject.org/.../accessingmoredbs.html. Please let me know if you try that out and how it works for you.

Ryan Lanciaux

June 23. 2008 12:19

Nikos Bilalis

It seems straight forward, if I find some spare time I'll give it a try and let you know.

Regarding Ayende's post and your example code: In order to use the more compact configuration mentioned ("ActiveRecordUnitOfWorkFacility") with your example code, I had to update the "Rhino.Commons.ActiveRecord.dll" to the latest trunk version. When I tried to use all dll's from Rhino-Commons trunk the example gave me a "Null Reference" exception. I'm just mentioning it, in case someone else had the same problem.

Thanks for your excellent articles.

Nikos Bilalis

August 15. 2008 10:46

jose

I'm using RhinoCommons trunk version and I cannot make the thing to work.
If I use the boo file as you provide I get a Null Reference Exception on
Source File: d:\Projects\OpenSource\Rhino-Tools\rhino-commons\Rhino.Commons.Binsor\Component.cs Line: 179

however using the Ayende's way with the facility ActiveRecordUnitOfWorkFacility: assembly "ProductModelActiveRecord"
I get a similar a null reference exception too, but this time in:
d:\Projects\OpenSource\Rhino-Tools\rhino-commons\Rhino.Commons.ActiveRecord\Facilities\ActiveRecordUnitOfWorkFacility.cs Line: 56

Has anyone had the same problem and could fix it?


jose

August 15. 2008 15:16

jose

I had to use the very old Rhino.Commons assemblies from Exento sample app to make it work.

Anyone working ok with the trunk version?

jose

August 15. 2008 15:20

ryan

@Jose I am giving a talk next week but after that I was hoping to look at this again to get it working against the latest trunk. In the meantime, please let me know if you have any success Smile

ryan

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

August 28. 2008 15:22





© 2008 Ryan Lanciaux :: powered by BlogEngine.NET