Home

Castle Stronghold

Bringing ActiveRecord to the party

In order to explorer some more realistics scenarios you will face in daily developments, why not use ActiveRecord as our database access approach?

All you have to do is reference the assemblies, set up a database and configure and initialize ActiveRecord:

  1. First of all, add references to the following assemblies:

    • Castle.ActiveRecord.dll
    • Castle.DynamicProxy.dll
    • Iesi.Collections.dll
    • log4net.dll
    • NHibernate.dll
    • NHibernate.ByteCode.Castle.dll

  2. Now include a section on the web.config so you can keep ActiveRecord's configuration there:

    
    <configuration>
        <configSections>
            <section 
                name="monorail" 
                type="Castle.MonoRail.Framework.Configuration.MonoRailSectionHandler, Castle.MonoRail.Framework" />
            <section 
                name="activerecord"
                type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
        </configSections>
        
        <activerecord isWeb="true">
            
            <config>
                <add 
                    key="connection.driver_class" 
                    value="NHibernate.Driver.SqlClientDriver" />
                <add 
                    key="dialect"                 
                    value="NHibernate.Dialect.MsSql2005Dialect" />
                <add 
                    key="connection.provider"     
                    value="NHibernate.Connection.DriverConnectionProvider" />
                <add 
                    key="connection.connection_string" 
                    value="Data Source=.;Initial Catalog=test;Integrated Security=SSPI" />
                <add
                    key="proxyfactory.factory_class"
                    value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"  />
            </config>
            
        </activerecord>
    
    Quick Note

    The configuration above uses SQL Server 2005. If you are using a different database you need to change the dialect. Please refer to the Xml Configuration Reference document for more information.

    This configuration access a database catalog named test. Make sure it exists or change the configuration to access an existing database catalog.

  3. Create two models to represent products and suppliers. You can create the classes on the Models folders

    The Supplier.cs:

    
    namespace GettingStartedSample.Models
    {
        using Castle.ActiveRecord;
    
        [ActiveRecord]
        public class Supplier : ActiveRecordBase<Supplier>
        {
            [PrimaryKey]
            public int Id { get; set; }
    
            [Property]
            public string Name { get; set; }
        }
    }

    The Product.cs:

    
    namespace GettingStartedSample.Models
    {
        using Castle.ActiveRecord;
    
        [ActiveRecord]
        public class Product : ActiveRecordBase<Product>
        {
            [PrimaryKey]
            public int Id { get; set; }
    
            [Property]
            public string Name { get; set; }
    
            [Property]
            public decimal Price { get; set; }
    
            [BelongsTo("SupplierId")]
            public Supplier Supplier { get; set; }
        }
    }
    }
  4. The last step is to initialize ActiveRecord passing the configuration. The best place to do that in a web application is on the global.asax related class. So create a Global.asax if none exists and on the related class, use the Application_OnStart event to initialize ActiveRecord:

    The Global.asax file:

    
    <%@ Application Codebehind="Global.asax.cs" Inherits="GettingStarted.Global" Language="C#" %>

    The Global.asax.cs file:

    
    namespace GettingStartedSample
    {
        using System;
        using System.Web;
        using Castle.ActiveRecord;
        using Castle.ActiveRecord.Framework.Config;
        using Models;
        public class Global : HttpApplication
        {
    
            protected void Application_Start(object sender, EventArgs e)
            {
                ActiveRecordStarter.Initialize(typeof(Supplier).Assembly, ActiveRecordSectionHandler.Instance);
    
                // If you want to let ActiveRecord create the schema for you:
                ActiveRecordStarter.CreateSchema();
            }
        }
    }
    

The next time you run the application it will create the tables for you, so you can comment the line that invokes CreateSchema after that.

In the following pages we will have some fun using scaffolding and creating CRUD pages.

Proceed with Using ActiveRecord Scaffolding.

Google
Search WWW Search castleproject.org