The document discusses the history and concepts behind object-relational mapping (ORM) and ORMs. It begins by describing the mismatch between the object-oriented and relational models. It then covers early work on object-relational mapping to bridge this gap. This led to the development of ORM tools that allow defining object-relational mappings and querying object graphs that are persisted to databases. Key concepts discussed include object identity, object shadows, query objects, repositories, partitioning query results, the unit of work pattern, and identity maps. Code examples demonstrate these concepts using NHibernate and LINQ.
1. From relational data to
ObjectSpaces
(…and then O/RMs)
Andrea Saltarello
Solution Architect @ Managed Designs
andreas@manageddesigns.it
http://twitter.com/andysal74
http://blogs.ugidotnet.org/mrbrightside
http://creativecommons.org/licenses/by-nc-nd/2.5/
2. Agenda
Scenario
Model vs. Model
Object-relational mapping
Object/Relational Mappers
3. The Scenario
Face it: most software does access databases
As of today, most of the time dabatase==RDBMS
During the ’90s object based|oriented gained
momentum
Gentlemen prefer Blondes, and Developers
prefer Objects
4. OO Recap
Object: an identifiable, encapsulated entity that
is interacted with by sending messages. Objects
have behavior, state and identity
Type: specifies the public behavior and
conceptual grouping for objects that are
members of the type
Identity: the ability to tell an object apart from
another object independently of whether their
type and state is equal
5. Relational Model Recap
Relation: a truth predicate. It defines what attributes
are involved in the predicate and what the meaning
of the predicate is.
Person: {SSN#, Name, City} There exists a person with social
security number SSN#, who has the name Name and lives in
a city named City
Attribute: identifies a name that participates in the
relation and specifies the domain (type) from which
values of the attribute must come
Person: {SSN#: SSN, Name: string, City: string}
Tuple: a truth statement in the context of a relation.
Tuples are values and two tuples are identical if their
relation and atribute values are equal.
Person SSN#=‘123-456-78’ Name=‘Mario Rossi’ City=‘Milan’
6. Relational Model Recap
Relation Value: the composition of a relation (the
heading) and a set of tuples (the body). All the tuples
must have the same relation as the heading. The
body is unordered and does not contain duplicates
Relation Variable: holds onto a single Relation Value
at any point in time, but can change value at any
point in time. Relvars are typed to a particular
relation, so they will always hold relation values that
have a heading with that relation.
Database: a collection of relvars. It can change state
by changing relvars and can answer questions about
its particular state
7. Impedance mismatch
Tuples have neither identity nor encapsulation
Tuple attribute values are pure values, so they
have no identity
Relational modeling seems to have no way of
representing any of the object modeling
properties nicely (inheritance, composition, M:N
associations, …)
8. Object-Relational mapping
In 1997 Mark Fussels wrote a document whose aim
was to integrate objects into the relational model
by means of a ‘’mapping’’ process:
object-relational mapping is the process of
transforming between object and relational
modeling approaches and between the systems
that support these approaches [FORM, 5]
9. Object-Relational mapping
ObjectShadow: the informations needed to see that
an object exists without any true representation of
the real object [FORM, 21]
ObjectShadows are composed by:
IdentityKeys: the value that represents the object’s
identity
Distinguishers: a value that represents the type of the
object
Objects are built from tuples by means of
ObjectShadows
10. Here comes the O/RM
ObjectSpace: a closed, self-contained collection of
objects based on a single (possibly very large)
schema
Fussels own work inspired the inception of O/RM
toolkits which:
Allow to define an O/R mapping
Allow to query an ObjectSpace
Allow an ObjectSpace to be persisted onto a
database
12. The ObjectSpace is the Database
To make an ObjectSpace look as a ‘’DBMS’’, an
O/RM should at least:
Make it queryable, with support for partitioning
of fetched data
Support transactions
13. Query Object [P of EAA, 316]
A Query Object is an interpreter
[Gang of Four], that is, a
structure of objects that can
form itself into a SQL query.
You can create this query by
referring to classes and fields
rather than tables and
columns. In this way those
who write the queries can do
so independently of the
database schema and
changes to the schema can
be localized in a single place.
14. Query Object @ .NET
Every O/RM ships its own query object (es:
NHibernate’s Criteria API)
Starting from v 3.5, .NET provides an idiomatic
query object: LINQ
18. Partitioning
Do you remember those old days when we run
queries like:
SELECT FirstName, LastName AS FamilyName
FROM Persons
In order to fetch only the data we’re going to use?
20. Unit of Work [P of EAA, 184]
When you're pulling data in and
out of a database, it's important to
keep track of what you've
changed. Similarly you have to
insert new objects you create and
remove any objects you delete.
A Unit of Work keeps track of
everything you do during a
business transaction that can
affect the database.
22. Identity Map [P of EAA, 195]
Ensures that each object gets loaded only once by
keeping every loaded object in a map. Looks up objects
using the map when referring to them
24. O/RM «Magic»: Object Tracking
object tracking allows an O/RM to know which
quota of a graph has been modified so to
dynamically generate optimized SQL code
26. Books & Publications
[FORM] Foundations of Object-Relational
Mapping, Mark L. Fussell
[P of EAA] Pattern of Enterprise Application
Architecture, Martin Fowler, Addison-Wesley
[NSK] NSK, http://nsk.codeplex.com