Active record pattern: Difference between revisions

Content deleted Content added
Criticism: added inline citation needed tags
Adding local short description: "Concept in software engineering", overriding Wikidata description "design pattern for software that stores in-memory object data in relational databases, with interface functions for insert, update and delete, and properties corresponding to the columns in the underlying database table"
 
(31 intermediate revisions by 25 users not shown)
Line 1:
{{Short description|Concept in software engineering}}
In [[software engineering]], the '''active record pattern''' is an [[Architectural pattern (computer science)|architectural pattern]]. It is 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>[https://martinfowler.com/eaaCatalog/activeRecord.html P of EAA Catalog - Active Record]</ref><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&lpgq=PA1&dq=Patterns%20of%20Enterprise%20Application%20Architecture%20by%20Martin%20Fowleractive+record&pg=PT187#v=onepage&q=active%20record&f=false }}</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.
 
This pattern is commonly used by object persistence tools and in [[object-relationalobject–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 [[Software framework|framework]]s for many programming environments. For example, if in a database there is a table <code>parts</code> in a database with columns <code>name</code> (string type) and <code>price</code> (number type), and the Active Record pattern is implemented in the class <code>Part</code>, the pseudo-code
{{sxhl|2=ruby|1=
 
part = new Part()
part.name = "Sample part"
part.price = 123.45
part.save()
}}
 
will create a new row in the <code>parts</code> table with the given values, and is roughly equivalent to the [[SQL]] command
 
<sourcesyntaxhighlight lang="sql">
INSERT INTO parts (name, price) VALUES ('Sample part', 123.45);
</syntaxhighlight>
</source>
 
Conversely, the class can be used to query the database:
{{sxhl|2=ruby|1=
 
b = Part.find_first("name", "gearbox")
}}
 
This will find a new <code>Part</code> object based on the first matching row from the <code>parts</code> table whose <code>name</code> column has the value "gearbox". The SQL command used might be similar to the following, depending on the SQL implementation details of the database:
 
<sourcesyntaxhighlight lang="sql">
SELECT * FROM parts WHERE name = 'gearbox' LIMIT 1; -- MySQL or PostgreSQL
</syntaxhighlight>
</source>
 
== Criticism ==
 
=== TestabilityLarge files ===
Because the data and the database access methods are in the same file, those files end up being bigger.
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 |date=May 2015}} The negative effects on testability in the active record pattern can be minimized by using [[mock object|mocking]] or [[dependency injection]] frameworks to substitute the real data tier with a simulated one.{{citation needed |date=May 2015}}
 
=== 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]]. This is opposed to [[multitier architecture]], which properly addresses these practices.{{citation needed |date=November 2019}}{{Clarify|date=August 2018}} Because of this, the active record pattern is best and most often employed in simple applications that are all forms-over-data with [[Create, read, update and delete|CRUD]] functionality, or only as one part of an architecture.{{citation needed |date=November 2019}} Typically that part is data access and why several ORMs implement the active record pattern.
[[single responsibility principle]] and [[separation of concerns]] as opposed to [[multitier architecture]] which properly addresses these practices.{{citation needed |date=November 2019}} Because of this, the active record pattern is best and most often employed in simple applications that are all forms-over-data with [[Create, read, update and delete|CRUD]] functionality, or only as one part of an architecture.{{citation needed |date=November 2019}} 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.{{Clarify|date=August 2018}}
 
== See also ==
 
* [[{{Annotated link |Business object]]}}
* [[{{Annotated link |Create, read, update and delete|CRUD]]}}
* [[{{Annotated link |Data mapper pattern]]}}
* [[Object-relational mapping]]
* {{Annotated link |Object–relational mapping}}
 
== References ==
{{Reflist|colwidth=35em}}
 
 
 
{{Design Patterns Patterns}}
Line 56 ⟶ 52:
{{DEFAULTSORT:Active Record Pattern}}
[[Category:Architectural pattern (computer science)]]
[[Category:Software design patterns]]