from relational data to object spaces
DESCRIPTION
TRANSCRIPT
From relational data to ObjectSpaces (…and then O/RMs)
Andrea Saltarello Solution Architect @ Managed Designs [email protected] http://twitter.com/andysal74 http://blogs.ugidotnet.org/mrbrightside
http://creativecommons.org/licenses/by-nc-nd/2.5/
Agenda
Scenario
Model vs. Model
Object-relational mapping
Object/Relational Mappers
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
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
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’
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
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, …)
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]
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
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
Demo Demo O/RM overview
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
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.
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
Demo Demo Query (Object): NH & LINQ
Repository [P of EAA, 322]
Mediates between the domain and data mapping layers
using a collection-like interface for accessing domain
objects.
Demo Demo Repository: NSK
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?
Demo Demo Partitioning: NHibernate’s Projections & «LINQ feat. Anonymous Types»
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.
Demo Demo ISession
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
Demo Demo Identity Map
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
Demo Demo Persistence
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