Content deleted Content added
GraziePrego (talk | contribs) 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" |
|||
(29 intermediate revisions by 23 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&
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 [[
== Implementations ==
Implementations of the concept can be found in various [[Software framework|framework]]s for many programming environments. For example, if
{{sxhl|2=ruby|1=
}}
will create a new row in the <code>parts</code> table with the given values, and is roughly equivalent to the [[SQL]] command
<
INSERT INTO parts (name, price) VALUES ('Sample part', 123.45);
</syntaxhighlight>
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:
<
SELECT * FROM parts WHERE name = 'gearbox' LIMIT 1; -- MySQL or PostgreSQL
</syntaxhighlight>
== Criticism ==
===
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,
== See also ==
*
*
*
* {{Annotated link |Object–relational mapping}}
== References ==
{{Reflist
{{Design Patterns Patterns}}
|