Subsecciones
In software engineering, the active record pattern is an architectural
pattern found in software that stores its data in relational databases. It
was named by Martin Fowler in his 2003 book Patterns of Enterprise
Application Architecture.
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.
Active record 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.
Las gemas activerecord
y DataMapper siguen el patrón Active Record.
Martin Fowler
(Catalog of Patterns of Enterprise Application Architecture):
- Objects and relational databases have different mechanisms for structuring
data.
- Many parts of an object, such as collections and inheritance,
aren't present in relational databases.
- When you build an
object model with a lot of business logic it's valuable to use these
mechanisms
to better organize the data and the behavior that goes with
it.
- Doing so leads to variant schemas; that is, the object
schema and the relational schema don't match up.
- You still need to transfer data between the two schemas, and this data
transfer becomes a complexity in its own right.
- If the in-memory objects
know about the relational database structure, changes in
one tend to ripple to the other.
- The Data Mapper is a layer of software that separates the in-memory
objects from the database.
- Its responsibility is to transfer data between
the two and also to isolate them from each other
- With
Data Mapper
- the in-memory objects needn't know even that there's a
database present;
- they need no SQL interface code,
- and certainly no
knowledge of the database schema.
- (The database schema is always
ignorant of the objects that use it.)
The gem perpetuity
implements the DataMapper pattern.
Casiano Rodríguez León
2015-01-25