In software engineering, the active record pattern is an architectural pattern found in software that stores in-memory object 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 - wikipedia ![]()
# See also * Implementations * See also * References * External links
The active record pattern 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.
# Criticism
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 (mock object) or dependency injection frameworks to substitute the real data tier with a simulated one - wikipedia ![]()
# 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 and separation of concerns as opposed to multitier architecture 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.