In
software engineering
Software engineering is a branch of both computer science and engineering focused on designing, developing, testing, and maintaining Application software, software applications. It involves applying engineering design process, engineering principl ...
, the active record pattern is an
architectural pattern
Software architecture pattern is a reusable, proven solution to a specific, recurring problem focused on architectural design challenges, which can be applied within various architectural styles.
Examples
Some examples of architectural patte ...
. It is found in software that stores in-memory object data in
relational database
A relational database (RDB) is a database based on the relational model of data, as proposed by E. F. Codd in 1970.
A Relational Database Management System (RDBMS) is a type of database management system that stores data in a structured for ...
s. 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.
The active record pattern is an approach to accessing data in a
database
In computing, a database is an organized collection of data or a type of data store based on the use of a database management system (DBMS), the software that interacts with end users, applications, and the database itself to capture and a ...
. A
database table or
view is wrapped into a
class
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
. Thus, an
object
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an a ...
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
A foreign key is a set of attributes in a table that refers to the primary key of another table, linking these two tables. In the context of relational databases, a foreign key is subject to an inclusion dependency constraint that the tuples ...
relationships will be exposed as an object instance of the appropriate type via a property.
Implementations
Implementations of the concept can be found in various
frameworks for many programming environments. For example, if there is a table
parts
in a database with columns
name
(string type) and
price
(number type), and the Active Record pattern is implemented in the class
Part
, the pseudo-code
will create a new row in the
parts
table with the given values, and is roughly equivalent to the
SQL
Structured Query Language (SQL) (pronounced ''S-Q-L''; or alternatively as "sequel")
is a domain-specific language used to manage data, especially in a relational database management system (RDBMS). It is particularly useful in handling s ...
command
INSERT INTO parts (name, price) VALUES ('Sample part', 123.45);
Conversely, the class can be used to query the database:
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
Criticism
Large files
Because the data and the database access methods are in the same file, those files end up being bigger.
Single responsibility principle and separation of concerns
Another critique of the active record pattern is that, due to the strong coupling of database interaction and application logic, an active record object does not follow the
single responsibility principle and
separation of concerns
In computer science, separation of concerns (sometimes abbreviated as SoC) is a design principle for separating a computer program into distinct sections. Each section addresses a separate '' concern'', a set of information that affects the code o ...
. This is opposed to
multitier architecture
In software engineering, multitier architecture (often referred to as ''n''-tier architecture) is a client–server architecture in which presentation, application processing and data management functions are physically separated. The most wide ...
, which properly addresses these practices. Because of this, the active record pattern is best and most often employed in simple applications that are all forms-over-data with
CRUD functionality, or only as one part of an architecture. Typically that part is data access and why several ORMs implement the active record pattern.
See also
*
*
*
*
References
{{DEFAULTSORT:Active Record Pattern
Architectural pattern (computer science)
Software design patterns