active record pattern
In computer science, the active record pattern is a design pattern frequently found in enterprise applications.
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 persistance tools, and in object-relational mapping. Typically foreign key relationships will be exposed as an object instance of the appropriate type via a property.
Implementations of Active Record can be found in various frameworks for many programming environments (see External Links).
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 following
pseudo-code:
a = new Part a.name = "Sample part" a.price = 123.45 a.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_part("name", "gearbox")
This will create a new Part based on the first matching row from the parts table whose
name column has the value "gearbox". The SQL command used would be
SELECT * FROM parts WHERE name = 'gearbox' LIMIT 1;
See also
External links
- Active Record page from the Patterns of Enterprise Application Architecture catalog by Martin Fowler
- Castle Project Active Record an open-source Microsoft .NET Implementation
- Dunn & Churchill Diamond Binding a commercially supported Microsoft .NET Implementation
- Ruby on Rails contains a Ruby implementation of Active Record.
- Subsonic Project contains a .NET implementation of Active Record.
- Akelos PHP Framework contains a PHP version of Rails Active Record.
- Cake PHP contains a PHP implementation of Active Record.
- Elixir is a Python implementation of Active Record.
- Grails contains a Groovy implementation of Active Record.
- Apache Torque is an open-source Java implementation of Active Record.
- pork.dbObject is a lightweight (< 500 lines of code) and simple (no XML files) OR Mapper for PHP5 with Active Record support.
This entry is from Wikipedia, the leading user-contributed encyclopedia. It may not have been reviewed by professional editors (see full disclaimer)



