ActiveRecord, the ugly design pattern
I first encountered the Gang of Four Design Patterns book back in 2001 when a friend lent me a copy. I didn’t immediately get it, most likely because my object oriented experience up to that point consisted primarily of small Delphi applications.
In the last few years I’ve been working on much larger systems and have come to appreciate design patterns enough to get my own copy and also to invest in Martin Fowler’s excellent Patterns of Enterprise Architecture which provides higher-level patterns aimed at large data-driven applications.
One of the design patterns presented, and indeed a core component of the Ruby on Rails framework, is that of the ActiveRecord which attempts to provide object-relational mapping.
It achieves this by specifying a single class per database table where instances of that class represent a row in the table – exposing a property for each field the row has in the database. Each ActiveRecord instance is therefore effectively a domain/business object.
So far so good but then the ActiveRecord pattern ignores the single responsibility principle by specifying that the object should also include a bunch of static methods for managing the table’s contents and retrieving instances.
What you end up with is one object with two distinct responsibilities separated by nothing but the static keyword. Static methods and fields can serve a useful purpose but dividing up responsibilities isn’t one of them and neither is to provide global-like access across your application (Singleton abusers take note).
I can think of at least two reasons why gluing two candidate objects into one physical one using the ‘static’ split causes ActiveRecord to suffer with problems:
Sometimes an application requires connections to more than one database (e.g. reporting, aggregation, upgrading tools, per-user switching of database in a web application).
Static methods often rely on static fields which means you’re going to have trouble making the
data you’re going to have trouble making them behave like objects.
There are three types of inheritance mapping techniques available.
- Concrete Table Inheritance – one table per concrete class containing columns for all fields of the class regardless of where they are declared
- Class Table Inheritance – one table per class in the heirarchy containing columns for fields of the class they are declared in only
- Single Table Inheritance – a single table for all classes in the heirarchy containing all columns for all possible subclasses many of which will be null
Concrete Table is most likely where the tables created by the parent classes would have little or no value and Class Table where they do.
Single Table is what ActiveRecord implementations such as Ruby on Rails tend to use although quite how it works I’ve yet to discover… Does the parent class magically know enough about it’s child classes that it’s static methods can handle the update/select/delete? Can I ask the parent class for all objects and get a mixed collection back? <shrug>
Fowler presents a number patterns as alternatives and many object-relational mapping (ORM) solutions including LINQ for SQL utilise them.