Jump to content

Active record pattern: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
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"
 
(37 intermediate revisions by 28 users not shown)
Line 1: Line 1:
{{Short description|Concept in software engineering}}
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&lpg=PA1&dq=Patterns%20of%20Enterprise%20Application%20Architecture%20by%20Martin%20Fowler&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.
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&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.


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.
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 ==
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> 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
Implementations of the concept can be found in various [[Software framework|framework]]s for many programming environments. For example, if 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 = new Part()
part.name = "Sample part"
part.name = "Sample part"
part.price = 123.45
part.price = 123.45
part.save()
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
will create a new row in the <code>parts</code> table with the given values, and is roughly equivalent to the [[SQL]] command


<source lang="sql">
<syntaxhighlight lang="sql">
INSERT INTO parts (name, price) VALUES ('Sample part', 123.45);
INSERT INTO parts (name, price) VALUES ('Sample part', 123.45);
</syntaxhighlight>
</source>


Conversely, the class can be used to query the database:
Conversely, the class can be used to query the database:
{{sxhl|2=ruby|1=

b = Part.find_first("name", "gearbox")
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:
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:


<source lang="sql">
<syntaxhighlight lang="sql">
SELECT * FROM parts WHERE name = 'gearbox' LIMIT 1; -- MySQL or PostgreSQL
SELECT * FROM parts WHERE name = 'gearbox' LIMIT 1; -- MySQL or PostgreSQL
</syntaxhighlight>
</source>

=== ColdFusion ===
[[ColdFusion]] has an open source implementation of the active record pattern.

The [[ColdFusion on Wheels]] framework has an implementation of the active record pattern. It is open source and has the added advantage of requiring no complex configuration.

=== PHP ===

PHP ActiveRecord is one [[open-source]] library designed to fulfill the active record pattern.<ref>{{cite web|title=PHP ActiveRecord|url=http://www.phpactiverecord.org/|access-date=2009-10-23|archive-url=https://web.archive.org/web/20100208131627/http://www.phpactiverecord.org/|archive-date=2010-02-08|dead-url=yes|df=}}</ref>

Several open-source [[PHP]] frameworks also bundle their own [[Object-relational mapping|ORM]] implementing the active record pattern. Most implementations support relationships, behaviors, [[input validation|validation]], [[serialization]] and support for multiple data sources.

=== Ruby ===
The [[Ruby (programming language)|Ruby]] library ActiveRecord implements ORM. It creates a [[Persistence (computer science)|persistable]] domain model from business objects and database tables, where logic and data are presented as a unified package. It largely simplifies object-record mapping by assuming homogeneous identification method (i.e. [[Surrogate key]]) by virtue of [[convention over configuration]]. ActiveRecord adds [[Inheritance (computer science)|inheritance]] and [[Association (object-oriented programming)|associations]] to the pattern above, solving two substantial limitations of that pattern. A set of macros acts as a domain language for the latter, and the [[Single Table Inheritance]] pattern is integrated for the former; thus, ActiveRecord increases the functionality of the active record pattern approach to database interaction. ActiveRecord is the default ‘model’ component of the [[model-view-controller]] web-application framework [[Ruby on Rails]], and is also a stand-alone ORM package for other Ruby applications. In both forms, it was conceived of by [[David Heinemeier Hansson]], and has been improved upon by a number of contributors.<ref>{{cite web|url=https://github.com/rails/rails/tree/master/activerecord|title=Ruby Active Record|accessdate=2013-04-12}}</ref>

Other ORMs have been released since ActiveRecord first appeared. For example, [[DataMapper (Ruby)|DataMapper]] and [[Sequel (Ruby)|Sequel]]. These ORMs include improvements over the original ActiveRecord framework.{{POV statement|date=May 2012}} As a response to their release and adoption by the Rails community, Ruby on Rails v3.0 became independent of an ORM system, so that Rails users can instead use DataMapper or Sequel as their ORM.

=== Python ===
[[Django_(web_framework)|Django]], one of [[Python_(programming_language)|Python]]'s many web frameworks, uses the Active Record pattern for its ORM.<ref>{{cite web|url=https://docs.djangoproject.com/en/1.11/misc/design-philosophies/#models|accessdate=2017-06-28}}</ref>

=== Java ===
The [[Java (programming language)|Java language]] implements the Active Record pattern via the [[ActiveJDBC]] library. ActiveJDBC is an implementation of Active Record design pattern inspired by [[Ruby on Rails]] ActiveRecord. ActiveJDBC is lightweight, fast, small and does not require any configuration.

[[ActiveJPA]] and [[Java Object Oriented Querying|jOOQ (for Java Object Oriented Querying)]] implements the Active record pattern, combining active records with [[Automatic programming|source code generation]] and a querying DSL similar to [[SQL]] allowing for retrieving active records using complex SQL statements.

The [[Play framework]] is a Java web framework which implements the Active Record pattern, using ideas from [[Ruby on Rails]].

=== Dart ===
The [[Dart (programming language)|Dart]] language implements the Active Record pattern via the [https://github.com/HannesRammer/Dartabase/tree/master/dartabase_migration Dartabase Migration] and [https://github.com/HannesRammer/Dartabase/tree/master/dartabase_model Dartabase Model] packages

[https://github.com/HannesRammer/Dartabase/tree/master/dartabase_migration Dartabase Migration]
Serverside Database Object Models for simple data manipulation currently supporting MySQL/PGSQL inspired by Ruby on Rails Migration

[https://github.com/HannesRammer/Dartabase/tree/master/dartabase_model Dartabase Model]
Serverside Database migration for simple version controlled database structure manipulation currently supporting MySQL/PGSQL inspired by Ruby on Rails Model
now console and GUI (build in Polymer 1.0)

=== Other languages ===
There are several open-source implementations of the Active Record pattern in other languages, including [[JavaScript]] (e.g., [[ActiveJS]]'s Active Record<ref name="ActiveJS">{{cite web|url=http://www.activejs.org/activerecord/ActiveRecord/index.html|title=ActiveJS Active Record|accessdate=2011-07-28|archive-url=https://web.archive.org/web/20110930024525/http://activejs.org/activerecord/ActiveRecord/index.html|archive-date=2011-09-30|dead-url=yes|df=}}</ref>), [[Perl]] ([[DBIx::Class]]), [[ActionScript]], [[Haxe]] (SPOD<ref name="HaxeSPOD">{{cite web|url=http://haxe.org/manual/spod|title=SPOD Macros|accessdate=2013-01-09|archive-url=https://web.archive.org/web/20121130055044/http://haxe.org/manual/spod|archive-date=2012-11-30|dead-url=yes|df=}}</ref>), [[C Sharp (programming language)|C#]],<ref name="CastleActiveRecord">{{cite web|title=Castle ActiveRecord|url=http://www.castleproject.org/projects/activerecord/|accessdate=14 January 2014}}</ref> [[Objective-C]]<ref>{{cite web|title=Objective Record|url=https://github.com/mneorr/Objective-Record|accessdate=14 January 2013}}</ref> and [[Scala (programming language)|Scala]].<ref>{{cite web|title=Scala Active Record|url=https://github.com/aselab/scala-activerecord|accessdate=4 December 2013}}</ref>


== Criticism ==
== Criticism ==


=== Testability ===
=== Large 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 ===
=== 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
Another critique of the active record pattern is that, 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. 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. 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 ( eg offline ). i.e. 2 updates both may have 1 field that is correct but only one of the 2 records can win.{{Clarify|date=August 2018}}


== See also ==
== See also ==
* [[Business object]]
* [[Create, read, update and delete|CRUD]]
* [[Data mapper pattern]]
* [[Object-relational mapping]]


* {{Annotated link |Business object}}
== References ==
* {{Annotated link |Create, read, update and delete|CRUD}}
{{Reflist|colwidth=35em}}
* {{Annotated link |Data mapper pattern}}
* {{Annotated link |Object–relational mapping}}


== External links ==
== References ==
{{Reflist}}
* [https://github.com/rails/rails/tree/master/activerecord Ruby implementation]
* [https://metacpan.org/module/DBIx::Class DBIx::Class Perl Implementation]
* [https://web.archive.org/web/20100208131627/http://www.phpactiverecord.org/ PHP implementation]
* [http://www.castleproject.org/projects/activerecord/ .NET implementation]
* [https://code.google.com/p/air-activerecord/ AIR Active Record project home]
* [https://github.com/scandio/troba/blob/master/doc/active-record-extension.md troba Active record extension]


{{Design Patterns Patterns}}
{{Design Patterns Patterns}}
Line 101: Line 52:
{{DEFAULTSORT:Active Record Pattern}}
{{DEFAULTSORT:Active Record Pattern}}
[[Category:Architectural pattern (computer science)]]
[[Category:Architectural pattern (computer science)]]
[[Category:Software design patterns]]

Latest revision as of 23:18, 31 October 2023

In software engineering, the active record pattern is an architectural pattern. It is 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][2] 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

[edit]

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

[edit]

Large files

[edit]

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

[edit]

Another critique of the active record pattern is that, 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][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.

See also

[edit]

References

[edit]
  1. ^ P of EAA Catalog - Active Record
  2. ^ Fowler, Martin (2003). Patterns of enterprise application architecture. Addison-Wesley. ISBN 978-0-321-12742-6.