Blog Stats
  • Posts - 13
  • Articles - 0
  • Comments - 5
  • Trackbacks - 0

 

Wednesday, April 23, 2008

Testing Active Record

While trying to get up to speed on using Castle’s ActiveRecord framework, one of the first things I needed to do was figure out how to test my ActiveRecord objects.  There are some a good   articles  that describes how to us a base test class to get your unit tests up and running.

In a nutshell you need to have a TestFixtureStartup to initialize ActiveRecord.  You need to call the ActiveRecordStarter.Initialize to get AR ready to go.

However, I have yet to find to opposite of this method to stop ActiveRecord on TestFixtureTearDown.  This becomes a problem when you have multiple test classes.  AR will yell at for already creating an instance.  I solved this with a static guard variable.

private static bool _isActivated = false;

if(!_isActivated)

{

_isActivated = true;

      ActiveRecordStarter.Initialize(source, typeof());   

}

If there is a better way of doing this please let me know.

One very cool thing is that you can leverage Nhibernate’s ability to create and drop schemas.  ActiveRecordStarter.CreateSchema() will create your database schema based off of your ActiveRecord classes.  I like this feature, though I wouldn’t use it for real integration tests because I want to make sure that my database migration process works when I deploy to production.

Using Database Repository Pattern with ActiveRecord with ActiveRecordMediator

Castle’s ActiveRecord frame work is an easy way to get introduced to NHibernate if you’re not familiar with setting up and using NHibernate (which I’m not).  However many people are not fond of the ActiveRecord pattern.  It can be a leaky abstraction, putting persistence related functions on your domain model is not a very clean separation of concerns for many people.  I tend to agree with this.  It really does depend on the complexity of your application.

When learning about AR I read a lot of blog entries or the documentation, people elude to how you can use AR API in a repository fashion, but I could find very few examples of how to uses.

It turns out there is an innocuous object within the API call ActiveRecordMediator<T>.  This little guy has all of the persitance related methods you normally call directly on your ActiveRecord object.  With this object you can use a Repository style DAL.  Here is my simple BaseRepository that I inherit from to take advantage of ActiveRecordMediator.  You can extend this further of course, but it’s all I need at the moment.

public class BaseRepository<T> : IRepository<T> where T :class

    {

        protected ActiveRecordMediator<T> mediator;

 

        public virtual void Save(T item)

        {

            ActiveRecordMediator<T>.Save(item);

           

        }

        public virtual T FindById(object id)

        {

            return ActiveRecordMediator<T>.FindByPrimaryKey(id);

        }

 

        public virtual T FindOne(params ICriterion[] criteria)

        {

           

            return ActiveRecordMediator<T>.FindOne(criteria);

           

        }

        public virtual T[] FindAll()

        {

            return ActiveRecordMediator<T>.FindAll();

        }

 

        public virtual int Count()

        {

            return ActiveRecordMediator<T>.Count();

        }

 

 

       

    }

 

 

Copyright © John Teague