HOME

TheInfoList



OR:

In
software engineering Software engineering is a systematic engineering approach to software development. A software engineer is a person who applies the principles of software engineering to design, develop, maintain, test, and evaluate computer software. The term '' ...
, the active record pattern is an
architectural pattern An architectural pattern is a general, reusable solution to a commonly occurring problem in software architecture within a given context. The architectural patterns address various issues in software engineering, such as computer hardware perform ...
. It is found in software that stores in-memory object data in
relational database A relational database is a (most commonly digital) database based on the relational model of data, as proposed by E. F. Codd in 1970. A system used to maintain relational databases is a relational database management system (RDBMS). Many relatio ...
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 stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases sp ...
. A
database table A table is a collection of related data held in a table format within a database. It consists of columns and rows. In relational databases, and flat file databases, a ''table'' is a set of data elements (values) using a model of vertical column ...
or view is wrapped into a
class Class 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 differentl ...
. 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 ...
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 Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
or properties for each column in the table or view. This pattern is commonly used by object persistence tools and in
object–relational mapping Object–relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is a programming technique for converting data between type systems using object-oriented programming languages. This creates, in effect, a "virtual object databa ...
(ORM). Typically,
foreign key A foreign key is a set of attributes in a table that refers to the primary key of another table. The foreign key links these two tables. Another way to put it: In the context of relational databases, a foreign key is a set of attributes subject to ...
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
framework A framework is a generic term commonly referring to an essential supporting structure which other things are built on top of. Framework may refer to: Computing * Application framework, used to implement the structure of an application for an op ...
s 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 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


Testability

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. These negative effects on testability of the active record pattern can be reduced by using
mocking Mockery or mocking is the act of insulting or making light of a person or other thing, sometimes merely by taunting, but often by making a caricature, purporting to engage in imitation in a way that highlights unflattering characteristics. Mocke ...
or dependency injection frameworks to substitute the real data tier with a simulated one.


Single responsibility principle and separation of concerns

Another critique of the active record pattern is that, also due to the strong coupling of database interaction and application logic, an active record object does not follow the
single responsibility principle The single-responsibility principle (SRP) is a computer programming principle that states that "A module should be responsible to one, and only one, actor." The term actor refers to a group (consisting of one or more stakeholders or users) that ...
and
separation of concerns In computer science, separation of concerns 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 of a computer program. A concern ...
as 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 In computer programming, create, read, update, and delete (CRUD) are the four basic operations of persistent storage. CRUD is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing information u ...
functionality, or only as one part of an architecture. Typically that part is data access and why several ORMs implement the active record pattern.


Distributed systems

Record-based patterns work poorly in distributed systems especially where concurrency is impossible (e.g. offline). i.e. two updates both may have one field that is correct but only one of the two records can win.


See also

* * * *


References

{{DEFAULTSORT:Active Record Pattern Architectural pattern (computer science) Software design patterns