CSC/ECE 517 Fall 2007/wiki2 2 d4: Difference between revisions
Line 74: | Line 74: | ||
#[http://www.martinfowler.com/eaaCatalog/index.html Active Record was mentioned in this book] | #[http://www.martinfowler.com/eaaCatalog/index.html Active Record was mentioned in this book] | ||
#[http://www.patchedsoftware.com/RailsEnvy-ActiveRecord.mov A video from RailsEnvy giving a quick and clean introduction to Active Record ] | #[http://www.patchedsoftware.com/RailsEnvy-ActiveRecord.mov A video from RailsEnvy giving a quick and clean introduction to Active Record ] | ||
#[http://datamapper.org/why.html A comparison of | #[http://datamapper.org/why.html A comparison of Data Mapper and Active Record implementations in Ruby] | ||
#[http://www.dev411.com/blog/2006/06/04/activerecord-achilles-heel-of-ruby-on-rails A blog entry by Dev411 on limitations of Active Record] | #[http://www.dev411.com/blog/2006/06/04/activerecord-achilles-heel-of-ruby-on-rails A blog entry by Dev411 on limitations of Active Record] | ||
#[http://www.objectarchitects.de/ObjectArchitects/papers/index.htm?Published/index.htm Mapping Objects to Tables, Proceedings EuroPLoP, 1997 (2004 update)] | #[http://www.objectarchitects.de/ObjectArchitects/papers/index.htm?Published/index.htm Mapping Objects to Tables, Proceedings EuroPLoP, 1997 (2004 update)] | ||
#[http://www.theserverside.com/tt/articles/content/RailsHibernate/article.html Hybernate vs. Rails: The Persistence Showdown] | #[http://www.theserverside.com/tt/articles/content/RailsHibernate/article.html Hybernate vs. Rails: The Persistence Showdown] |
Revision as of 02:59, 25 October 2007
Topic
Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights)
Object Relational Mapping (ORM)
Object-relational mapping (ORM) is the technique used to marshal database records into classes of object-oriented (o-o) languages and vice-versa. The technique is usually developed into a tool that can be used by the developer to alleviate some of the complexity in this mapping. The complexity derives from the inherently different data types that are used by relational databases and o-o languages. Database data types are typically scalar in nature while classes are more typically composed of a mix of non-scalar and scalar types.
Figure 1. Position of ORM Layer in Database Architecture
In addition to mapping data structures in ORM, o-o concepts often need to “bridge the chasm” between the object layer and the database layer. These concepts include: inheritance, aggregation, polymorphism, class associations, and data encapsulation6. This conceptual difference between the database structure and o-o data storage is known as the impedance mismatch,which can create a fragile bridge across the chasm. A solution to this problem would be to create o-o databases, which has been done, but these databases do not have the structural stability or mathematical foundation that relational databases have. Even if o-o databases were to catch on, the ORM problem would still exist for years to come with the many legacy relational databases.
There have been many ORM models developed to bridge the chasm, and we will compare and contrast a handful of them in terms of their utility for developers. Some issues that should be considered when comparing ORM models are the degree of coupling between the layers, performance issues, storage space usage, and maybe even concurrency and synchronization issues. Often improving one issue will come at the expense of another issue, e.g., decoupling the layers, maybe by separating their physical locations, can increase response time thus degrading performance6.
Approaches/Patterns
Crossing the Chasm Pattern Language
One of the first ORMs, the name implies the difficulty involved in mapping between the two disjoint data structures. The Chasm Pattern Language is a collection of patterns developed by Kyle Brown and Bruce Whitenack used to solve the ORM problem as it relates to “marrying relational databases and Smalltalk”. The patterns of Crossing Chasms are grouped into “static” patterns that deal with setting up the database tables and objects, “dynamic” patterns that relate to the runtime issues involved in the ORM model, and architectural patterns that are broad patterns that model entire systems.
Representing Objects as Tables with Crossing Chasms
The following patterns combined can be used to represent class objects as tables in a relational database.
- Object Identifier - This is a pattern that assigns a unique identifier to each persistent object usually in the for
- Foreign Key - A pattern that handles references to other class types by taking advantage of instances of the other class’s object identifier.
- Representing Collections - Use a relationship table for each collection that maps the object IDs of the containing objects to the contained objects.
Table Data Gateway
This pattern has a simple structure. This class will have methods that take the parameters required in SQL as function arguments and function returns the SQL values. Typical methods would be find, update, insert, delete. This approach is the first step in abstracting the database and calls to SQL. A simple usage of this can be seen in Perl's DBI module. Though a most of the features of the SQL are implemented in this module, the idea is the same.
Dealing the Return type is not straightforward. Most of the times SQL queries return multiple values but programming languages methods typically return only one value. We can use either Map approach or a RecordSet(JDBC, .NET) approach both of which have some compromises.
Row Data Gateway
In this pattern every row of the record structure is treated like an instance of an object. There will be two objects one finder and a gateway. The finder object has methods like find and the gateway objects has methods like insert, update. The reason for separating the finder class is to have polymorphisms of finds from separate data stores. This pattern is almost similar to the active record pattern but with the exception that active record also have the domain logic in the class.
Active Record
Limitations
- No Foreign Key Support: Although RoR lets you define has_many relationships, it makes no effort to create foreign key constraints in the underlying database to ensure relational integrity.
- No Multi-column Primary Key Support: Multi-column primary keys are a staple of relational database schema definition.
Data Mapper
Popular Products
These patterns in practice can be found in:
Hibernate
- Uses the Data Mapper, Identity Map, and Unit of Work patterns
Oslick
Rails
- Uses the Active Record pattern
Lafcadio
- Another ORM for Ruby that currently only supports MySQL databases
- Treats each table row as a class object
- see http://lafcadio.rubyforge.org/ and http://www.zenspider.com/dl/rubyconf2003/lafcadio.pdf
Perl's DBIx::Class
References
- ORM articles
- Active Record was mentioned in this book
- A video from RailsEnvy giving a quick and clean introduction to Active Record
- A comparison of Data Mapper and Active Record implementations in Ruby
- A blog entry by Dev411 on limitations of Active Record
- Mapping Objects to Tables, Proceedings EuroPLoP, 1997 (2004 update)
- Hybernate vs. Rails: The Persistence Showdown