Active Record

from Wikipedia, the free encyclopedia

In software development , Active Record is a design pattern for object-oriented software that stores data in a relational database .

Mode of action

Example of a person's active record

It was named by Martin Fowler in his book Patterns of Enterprise Application Architecture . An object usually provides interfaces for inserting, changing and deleting according to this pattern. These then refer directly to the underlying table (s).

Active Record is an approach to access data that is in a database . A corresponding class is created for a database table. An object or an instance of this class then corresponds to a row in the table. The creation of a new object leads to a new line in the table. Every object that is loaded gets its information from the database. If an object is changed, the corresponding line in the table is also changed. The class implements access methods for each attribute or each column of the table.

ActiveRecord is therefore an object as Adapter ( English wrapper ) to a row in a database table or database view (Engl. View ) is used. The adapter contains the database access and business logic for the data. It is basically a row data gateway that is extended by the business logic and therefore implements both data (properties) and behavior (methods).

Implementations

There are numerous software frameworks that implement the active record pattern. For example, if there is a database table produktewith columns nameand preisand the active record pattern is Produktimplemented as a class , then the following pseudo-code creates a new line in the table produktewith the given attributes.

produkt = new Produkt
produkt.name = 'Beispielprodukt'
produkt.preis = 1.99
produkt.save

This roughly corresponds to the SQL command:

 INSERT INTO produkte(name, preis) VALUES ('Beispielprodukt', 1.99);

Conversely, the class can Produktalso be used to load a product:

produkt = Produkt.find_by(name: 'Beispielprodukt')

This creates a new Produktobject that produktecorresponds to the first product from the table, which is named "Example Product ". The corresponding SQL could look like this, for example:

 SELECT * FROM produkte WHERE name = 'Beispielprodukt' LIMIT 1; -- MySQL oder PostgreSQL

Web links

Individual evidence

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