Share on Facebook Share on Twitter Email
Answers.com

Active record pattern

 
Wikipedia: Active record pattern

In software engineering, the active record pattern is a design pattern found in software that stores its data in relational databases. It was named by Martin Fowler in his book Patterns of Enterprise Application Architecture. The interface to such an object 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. Typically foreign key relationships will be exposed as an object instance of the appropriate type via a property.

Contents

Implementations

Implementations of the concept can be found in various frameworks for many programming environments. For example, if in a database there is a table parts with columns name (string type) and price (number type), and the Active Record pattern is implemented in the class Part, the following 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 create 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

or

SELECT * FROM parts WHERE name = 'gearbox' AND rownum < 2; -- Oracle

or

SELECT TOP 1 * FROM parts WHERE name = 'gearbox'; -- Microsoft SQL Server

Rails

A Ruby library is available which implements the object-relational mapping (ORM) design pattern. It creates a persistable domain model from business objects and database tables, where logic and data are presented as a unified package. ActiveRecord adds inheritance and 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 SingleTableInheritance 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.[1]

PHP

PHP ActiveRecord is an open source ORM library based on the ActiveRecord pattern. It was inspired by Ruby on Rails implementation and therefore borrows many of its conventions and ideas. The library is licensed under the MIT License and is providing a development environment hosted on Github. Additional OOP constructs have been supported by Active Record since the release of PHP 5.3[2] [3]

This implementation includes support for Finder, Dynamic finder, and Writer methods, Relationships, Validations, Callbacks, Serialization and support for multiple adapters.

Microsoft .NET

An implementation for the Microsoft .NET framework is available in the Castle Project . It represents a row in the database with an ActiveRecord instance, and the static methods act on all rows. It is free, open source software that is distributed under the Apache 2.0 License. It uses NHibernate, but you do not need to write XML mapping.

The ADO.NET Entity Framework is an addition to the .NET framework in version 3.5, providing an ORM/Active Record pattern to the core of the framework.

The nHydrate ORM tool also implements the Active Record pattern in its data access layer (DAL). Thought the framework provides much more functionality and other implemented patterns, Active Record is the heart of its DAL. It is also open source and has the added advantage of requiring no complex configuration.

See also

References

External links


Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
 
 

 

Copyrights:

Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Active record pattern" Read more