CSC/ECE 517 Fall 2009/wiki3 12 obj to relational: Difference between revisions
Line 44: | Line 44: | ||
[[Image:Persistence Class Diagram.png|700 px|Persistence Class Diagram.]] [12] | [[Image:Persistence Class Diagram.png|700 px|Persistence Class Diagram.]] [12] | ||
AbtObservableObject subclass: PersistentObject | AbtObservableObject subclass: PersistentObject | ||
instanceVariableNames: 'objectIdentifier� isPersisted isChanged owningObject ' | instanceVariableNames: 'objectIdentifier� isPersisted isChanged owningObject ' | ||
classVariableNames: '' | classVariableNames: '' | ||
poolDictionaries: '' | poolDictionaries: '' | ||
buildSqlStatement: aString | buildSqlStatement: aString | ||
"Answer that each subclass must� implement a queryStream method." | "Answer that each subclass must� implement a queryStream method." | ||
^self subclassResponsibility | ^self subclassResponsibility | ||
executeSql: aSqlStatement | executeSql: aSqlStatement | ||
"Execute a sql statement using an custom error block in runtime or use the default debugger during development." | "Execute a sql statement using an custom error block in runtime or use the default debugger | ||
during development." | |||
| aQuery | | | aQuery | | ||
aQuery:=AbtQuerySpec new statement:� aSqlStatement. | aQuery:=AbtQuerySpec new statement:� aSqlStatement. |
Revision as of 02:57, 18 November 2009
Patterns for mapping objects to relational databases
The Object-Relational Mismatch
Object-orientation and relational model are different paradigms of programming.[1] The object paradigm is based on building applications out of objects that have both data and behavior, whereas the relational paradigm is based on storing data. With the object paradigm, one traverses objects via their relationships whereas with the relational paradigm one duplicates data to join the rows in tables.[2] The difference between how object models work and relational databases is known as the “Object-Relational Impedance Mismatch”. This is just nerd-speak for “they are different”. In analogy, we could consider the example of the ball and shape game. It's the same thing; you can’t fit a square in a triangle hole.[10]
One way to avoid the impedance mismatch between objects and relations is to use an object-oriented database. However, systems often need to store objects in a relational database. Sometimes a system needs relational calculus or the maturity of a relational database. Other times the corporate policy is to use a relational database rather than an object-oriented database. Whatever the reason, a system that stores objects in a relational database needs to provide a design that reduces the impedance mismatch.[5] One of the secrets of success for mapping objects to relational databases is to understand both paradigms, and their differences, and then make intelligent trade offs based on that knowledge.[2] An application that maps between the two paradigms needs to be designed with respect to performance, maintainability and cost to name just a few requirements.[4]
Persistence frameworks were designed to reduce the amount of work needed to develop an object-oriented system that stores data in a relational database. For this reason, this framework area is often also referred to as object-relational mapping or ORM (see the below Figure). The goal of a persistence framework is to automatically map objects to and from a database seamlessly so that information can be quickly and easily retrieved in object format, and then subsequently stored in its relational structure. This aids in system development and maintenance greatly. The mundane details of writing one's own create, read, update, and delete functions are taken care of, so one can concentrate on designing and building the application.[11]
Patterns
This page describes patterns for mapping objects to relations.
Persistence Layer
Motivation:
If one builds a large object-oriented business system that stores objects in a relational database, one can spend a lot of time dealing with the problems of making one's objects persistent. Also, every programmer working on the system has to know SQL and the code can become tied to the database. It can be a lot of work to convert the system from using Microsoft Access to using DB2, or even adding a few variables to an object. One needs to separate one's domain knowledge from knowledge of how objects are stored in a database to protect developers from these types of changes. The problem is how to save objects in a non object-oriented storage mechanism such as a relational database with the developers not having to know the exact implementation.
Solution:
The solution is to provide a Persistence Layer that can populate objects from a data storage source and save their data back to the data storage source. This layer should hide the developer from the details of storing objects. All persistent objects use the standard interface of the Persistence Layer. If the data storage mechanism changes, only the Persistence Layer needs to be changed. For example, the corporate direction may be to start using Oracle rather than DB2 and then switch in midstream. The system needs to know how to store each object and to load it. Sometimes an object is stored in multiple databases over multiple media. An object that is part of a more complex object needs to keep track of which object it is a part; this is called the owning object. This owning object idea makes it easier to write complex queries. Therefore it is important for the Persistence Layer to provide a means to uniquely identify each object and its parent.
Example Implementation:
The figure shown below is a UML class diagram for an implementation of Persistent Layer which map domain objects to a relational database. Note that in this example, domain objects that need to be persisted are subclasses of the PersistentObject. The PersistentObject provides for the interface to the Persistence Layer. The PersistentObject interacts with the Table Manager, the Connection Manger and the OID Manager. Thus, the PersistentObject is the central hub for any information the domain object requires but does not contain in the instance.
AbtObservableObject subclass: PersistentObject instanceVariableNames: 'objectIdentifier� isPersisted isChanged owningObject ' classVariableNames: poolDictionaries: buildSqlStatement: aString
"Answer that each subclass must� implement a queryStream method." ^self subclassResponsibility
executeSql: aSqlStatement
"Execute a sql statement using an custom error block in runtime or use the default debugger
during development."
| aQuery | aQuery:=AbtQuerySpec new statement:� aSqlStatement. (System startUpClass isRuntime) ifTrue:[self databaseConnection�
executeQuerySpec: aQuery� ifError:[:error| ErrorCollector� dbmError: nil ]]
ifFalse:[self databaseConnection�
executeQuerySpec: aQuery].
Conclusion
The concurrency patterns discussed above involve coordinating concurrent operations. They address two types of problems:[5]
Shared resources - When concurrent operations access the same data or another type of shared resource, operations may interfere with each other if they access the resource at the same time. To ensure that operations on shared resources execute correctly, the operations must be sufficiently constrained to access their shared resource one at a time. However, if the operations are overly constrained, then they may deadlock and not be able to finish executing.
Sequence of operations - If operations are constrained to access a shared resource one at a time, it may be necessary to ensure that they access the shared resource in a particular order. For example, an object cannot be removed from a data structure before it is added to the data structure.
See Also
- Monitor Object
- Active Object
- Thread pool pattern
- Reactor patter
- Double Buffering
- Scheduler
- Asynchronous Processing
References
[1] Wolfgang Keller, Mapping Objects to Tables - A Pattern Language http://www.objectarchitects.de/ObjectArchitects/papers/Published/ZippedPapers/mappings04.pdf
[2] Scott W. Ambler, President, Ronin International, Mapping Objects To Relational Databases http://www.crionics.com/products/opensource/faq/docs/mappingObjects.pdf
[3] Scott W. Ambler, Mapping Objects to Relational Databases: O/R Mapping In Detail http://www.agiledata.org/essays/mappingObjects.html
[4] Wolfgang Keller, Object/Relational Access Layers - A Roadmap, Missing Links and More Patterns http://citeseer.ist.psu.edu/4328.html
[5] Joseph W. Yoder, Ralph E. Johnson, Quince D. Wilson, Connecting Business Objects to Relational Databases http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.34.7703&rep=rep1&type=pdf
[6] Michael R. Blaha, William J. Premerlani and James E. Rumbaugh, Relational Database Design using an Object-Oriented Methodology http://www.sims.monash.edu.au/subjects/ims2501/seminars/oomodelling.pdf
[7] http://www.cetus-links.org/oo_db_systems_3.html
[8] http://www.ibm.com/developerworks/library/ws-mapping-to-rdb/
[10] http://cantgrokwontgrok.blogspot.com/2009/03/tech-day-1-nhibernate.html
[11] http://www.adobe.com/newsletters/edge/october2008/articles/article2/index.html?trackingid=DWZST
[12] Joseph W. Yoder, Patterns for Making your Business Objects Persistent in a Relational Database http://www.joeyoder.com/Research/objectmappings/mappingobjectstordbms/ppframe.htm