Chris Carter's Web Log

Today I was running some batch processes that made it nearly impossible to code up anything while the jobs were running.  That is, nearly impossible to code inside of visual studio(2005).  So I decided to fire up good ol' Notepad and write what I'm pretty sure is the simplest "Hello World" program for using Castle ActiveRecord. 

The only thing needed is a target database, in my example I used SQL Server 2005.  Armed with a ready and willing database, we need to set up our config settings for ActiveRecord.  Since this is a hello world program, I'm going to do this in code rather than use a config file.  Here are the config settings we need to set when the app first starts:

Hashtable properties = new Hashtable();
properties.Add("hibernate.connection.driver_class", 
	"NHibernate.Driver.SqlClientDriver");
properties.Add("hibernate.dialect", 
	"NHibernate.Dialect.MsSql2005Dialect");
properties.Add("hibernate.connection.provider", 
	"NHibernate.Connection.DriverConnectionProvider");
properties.Add("hibernate.connection.connection_string", 
	"user id=scratch;password=scratch;database=scratch;server=(local)");

I've basically taken the simple example posted here and swapped out my settings.

Next we initialize the ActiveRecord framework and call the very handy CreateSchema method. This gem will do exactly that, make all of our dreams come true in the database. But right now is a good time to actually make something.

Enter the Customer. They have a first name and a last name and that's as complicated as I'm going to make them. They also have an ID we'll use as the primary key. The Customer looks like this:

[ActiveRecord("customer")]
public class Customer 
	: ActiveRecordBase{
	private int _id;
	[PrimaryKey(
		PrimaryKeyType.Identity, 
		"customer_id"
	)]
	public int ID{
		get{ return _id; }
		protected set{ _id = value; }
	}
	private string _firstName;
	[Property(
		"first_name", 
		ColumnType="AnsiString(30)", 
		NotNull=true
	)]
	public string FirstName{
		get{ return _firstName; }
		set{ _firstName = value; }
	}
	private string _lastName;
	[Property(
		"last_name", 
		ColumnType="AnsiString(30)", 
		NotNull=true
	)]
	public string LastName{
		get{ return _lastName; }
		set{ _lastName = value; }
	}
}

The Customer is very simple to keep in line with "Hello World". So the app is going to do this. Initialize the ActiveRecord framework, create our database schema for us, create a new Customer, assign first and last name, save, and assert that we have an ID.

Compiling

Since we're not in visual studio we'll need to grab the assemblies needed for ActiveRecord.  This is a "Hello World" example so I think I've got only the bare minimum needed for this example.  I grabbed the latest successful build from the Castle Build Server found here.  The assemblies we'll need are the following:

  • Castle.ActiveRecord.dll
  • Castle.Components.Validator.dll
  • Castle.Core
  • Iesi.Collections.dll
  • log4net.dll
  • NHibernate.dll

These assemblies are included in the download accompanying this post. I've included my ghetto build script(buildme.cmd), ie a command file that contains the csc.exe commandline with options set. 

Download the code here.

 
Author: , 0000-00-00