Active record pattern
In software engineering, the active record pattern is an architectural pattern found in software that stores its data in relational databases. It was named by Martin Fowler in his 2003 book Patterns of Enterprise Application Architecture. The interface of an object conforming to this pattern would include functions such as Insert, Update, and Delete, plus properties that correspond more or less directly to the columns in the underlying database table.
Active record is an approach to accessing data in a database. A database table or view is wrapped into a class. Thus, an object instance is tied to a single row in the table. After creation of an object, a new row is added to the table upon save. Any object loaded gets its information from the database. When an object is updated the corresponding row in the table is also updated. The wrapper class implements accessor methods or properties for each column in the table or view.
This pattern is commonly used by object persistence tools, and in object-relational mapping (ORM). Typically, foreign key relationships will be exposed as an object instance of the appropriate type via a property.
Implementations of the concept can be found in various frameworks for many programming environments. For example, if in a database there is a table
parts with columns
name (string type) and
price (number type), and the Active Record pattern is implemented in the class
Part, the pseudo-code
part = new Part() part.name = "Sample part" part.price = 123.45 part.save()
will create a new row in the
parts table with the given values, and is roughly equivalent to the SQL command
INSERT INTO parts (name, price) VALUES ('Sample part', 123.45);
Conversely, the class can be used to query the database:
b = part.find_first("name", "gearbox")
This will find a new
Part object based on the first matching row from the
parts table whose
name column has the value "gearbox". The SQL command used might be similar to the following, depending on the SQL implementation details of the database:
SELECT * FROM parts WHERE name = 'gearbox' LIMIT 1; -- MySQL or PostgreSQL
ColdFusion has an open source implementation of the Active Record pattern.
The ColdFusion on Wheels framework has an implementation of the Active Record pattern. It is open source and has the added advantage of requiring no complex configuration.
Several PHP open-source frameworks bundle their own ORM implementing the Active Record pattern, including Agile Toolkit, Kohana, Doctrine (Before version 2), Propel, CakePHP, Horde, K2F, Laravel, Yii, SilverStripe and FuelPHP, aMember. Most implementations support relationships, behaviors, validation, serialization and support for multiple adapters.
The framework CodeIgniter has a query builder it calls "ActiveRecord", but which doesn't implement the Active Record pattern. Instead it implements what the user guide refers to as a modified version of the pattern. The Active Record functionality in CodeIgniter can be achieved by using either CodeIgniter DataMapper library or CodeIgniter Gas ORM library.
The Ruby library ActiveRecord implements ORM. It creates a persistable domain model from business objects and database tables, where logic and data are presented as a unified package. ActiveRecord adds inheritance and associations to the pattern above, solving two substantial limitations of that pattern. A set of macros acts as a domain language for the latter, and the Single Table Inheritance pattern is integrated for the former; thus, ActiveRecord increases the functionality of the active record pattern approach to database interaction. ActiveRecord is the default ‘model’ component of the model-view-controller web-application framework Ruby on Rails, and is also a stand-alone ORM package for other Ruby applications. In both forms, it was conceived of by David Heinemeier Hansson, and has been improved upon by a number of contributors.
Other, less popular ORMs have been released since ActiveRecord first took the stage. For example, DataMapper and Sequel show major improvements over the original ActiveRecord framework.[neutrality is disputed] As a response to their release and adoption by the Rails community, Ruby on Rails v3.0 is independent of an ORM system, so Rails users can easily plug in DataMapper or Sequel to use as their ORM of choice.
The Java language has a new library called ActiveJDBC. ActiveJDBC is an implementation of Active Record design pattern inspired by Ruby on Rails ActiveRecord. ActiveJDBC is lightweight, fast, small and does not require any configuration.
Another library implementing the Active record pattern is jOOQ (for Java Object Oriented Querying). It combines active records with source code generation and a querying DSL similar to SQL allowing for retrieving active records using complex SQL statements.
Due to the coupling of database interaction and application logic when using the active record pattern, unit testing an active record object without a database becomes difficult. The negative effects on testability in the active record pattern can be minimized by using mocking or dependency injection frameworks to substitute the real data tier with a simulated one.