Jump to content

Active record pattern: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Undid revision 1004554818 by Nicereddy (talk) These are published references. If these were opinions we may also need to remove Martin Fowler opinions
Tags: Undo Reverted
One of these references does not resolve, one is literally a Stack Exchange question, one is some dude's blog, and the O'Reilly link barely has any context. It is not an appropriate comment for the start of the article without. Please see WP:NPOV. It is not "Currently regarded as an anti-pattern", at best you could say it is currently regarded as an anti-pattern _by some_.
Line 1: Line 1:
In [[software engineering]], the '''active record pattern''' is an [[Architectural pattern (computer science)|architectural pattern]] (currently regarded as an [[anti-pattern]])<ref>[https://www.oreilly.com/library/view/sql-antipatterns/9781680500073/f_0159.html Antipattern: The Model Is an Active Record]</ref><ref>[https://www.mehdi-khalili.com/orm-anti-patterns-part-1-active-record ORM anti-patterns - Part 1: Active Record]</ref><ref>[http://misko.hevery.com/2009/05/05/the-problem-with-active-record/ The problem with Active Record]</ref><ref>[https://softwareengineering.stackexchange.com/questions/119352/does-the-activerecord-pattern-follow-encourage-the-solid-design-principles Does the ActiveRecord pattern follow/encourage the SOLID design principles?]</ref> found in software that stores in-memory object data in [[relational database]]s. It was named by [[Martin Fowler (software engineer)|Martin Fowler]] in his 2003 book ''Patterns of Enterprise Application Architecture''.<ref>{{cite book |last=Fowler |first=Martin |title=Patterns of enterprise application architecture |publisher=Addison-Wesley |year=2003 |isbn=978-0-321-12742-6 |url=https://books.google.com/books?id=FyWZt5DdvFkC&q=active+record&pg=PT187 }}</ref> 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.
In [[software engineering]], the '''active record pattern''' is an [[Architectural pattern (computer science)|architectural pattern]] found in software that stores in-memory object data in [[relational database]]s. It was named by [[Martin Fowler (software engineer)|Martin Fowler]] in his 2003 book ''Patterns of Enterprise Application Architecture''.<ref>{{cite book |last=Fowler |first=Martin |title=Patterns of enterprise application architecture |publisher=Addison-Wesley |year=2003 |isbn=978-0-321-12742-6 |url=https://books.google.com/books?id=FyWZt5DdvFkC&q=active+record&pg=PT187 }}</ref> 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]]. A [[database table]] or [[View (database)|view]] is wrapped into a [[class (computer science)|class]]. Thus, an [[object (computer science)|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]] [[Method (computer programming)|methods]] or properties for each column in the table or view.
The active record pattern is an approach to accessing data in a [[database]]. A [[database table]] or [[View (database)|view]] is wrapped into a [[class (computer science)|class]]. Thus, an [[object (computer science)|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]] [[Method (computer programming)|methods]] or properties for each column in the table or view.

Revision as of 00:48, 6 February 2021

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.[1] 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. 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

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

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

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.[citation needed] 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.[citation needed]

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.[citation needed][clarification needed] 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.[citation needed] 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.[clarification needed]

See also

References

  1. ^ Fowler, Martin (2003). Patterns of enterprise application architecture. Addison-Wesley. ISBN 978-0-321-12742-6.