SRT's ActiveRecord Coding Sprint (and a "gotcha" to watch out for)

After our open house, a few of SRT's consultants stayed to have a little Coding Sprint with Castle -- specifically, ActiveRecord.

Darrell Hawley, Jay Wren, Rocky Krcatovich and I started with the typical, employee database design: A company, which has many departments and many employees.  Each employee exists in a department.  Pretty simple.  Darrell and Jay have used Castle before.  I just played around with it a little this weekend in preparation for this Coding Sprint.  Darrell and I did a little pair programming -- I watched him set up the database, the Company class and unit tests for creating a new company.  Later, I did the Department and Employee classes along with their unit tests which he watched.

Darrell began by creating a SQL Server database.  Jay decided to go the open-source route and use SQLite.  He was hoping that SQLite's in-memory database option would speed up his unit tests, but I don't think he ever got that far.  Darrell finished his Company set up (table, class and unit tests) before Jay had his database set up.  :)

When it came to my turn, I typed out the Department class pretty quickly, but when it came to my unit tests, I kept getting an error from ActiveRecord that I hadn't initialized my Department type.  We looked over the code for close to an hour and could not figure out what we did wrong.  Here's a super-simple recreation that will show you the error:

using System;
using System.Collections.Generic;
using Castle.ActiveRecord.Framework.Config;
using Castle.ActiveRecord;

namespace ARoops
{
public class MainApp
{
static void Main()
{
XmlConfigurationSource source = new XmlConfigurationSource("appconfig.xml");
ActiveRecordStarter.Initialize(source, typeof(Department));

Department dept = new Department();
dept.Name = "Accounting";
dept.Create();
}
}

public class Department : ActiveRecordBase<Department>
{
private int _id;
private string _name;

[PrimaryKey]
public int DeptID
{
get { return _id; }
set { _id = value; }
}

[Property]
public string Name
{
get { return _name; }
set { _name = value; }
}
}
}

If you create this ActiveRecord project and run it, you'll get the following error:

You have accessed an ActiveRecord class that wasn't properly initialized. The only explanation is that the call to ActiveRecordStarter.Initialize() didn't include Department class

I'm sure many of you who are familiar with ActiveRecord will spot the error right away.  When Darrell finally found it, we were both pretty disappointed that Castle didn't catch this right away.  And it was such a simple little error.

What did we forget?  We didn't decorate the Department class with the ActiveRecord attribute:

    [ActiveRecord]
public class Department : ActiveRecordBase<Department>
{

Once we did that, everything worked fine.  I wonder why the ActiveRecordStart.Initialize() method doesn't check to make sure the types passed to it are decorated with the ActiveRecord attribute?

No Comments