from relational data to object spaces

26
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/

Upload: andrea-saltarello

Post on 07-Dec-2014

1.989 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: From relational data to object spaces

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/

Page 2: From relational data to object spaces

Agenda

Scenario

Model vs. Model

Object-relational mapping

Object/Relational Mappers

Page 3: From relational data to object spaces

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

Page 4: From relational data to object spaces

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

Page 5: From relational data to object spaces

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’

Page 6: From relational data to object spaces

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

Page 7: From relational data to object spaces

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, …)

Page 8: From relational data to object spaces

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]

Page 9: From relational data to object spaces

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

Page 10: From relational data to object spaces

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

Page 11: From relational data to object spaces

Demo Demo O/RM overview

Page 12: From relational data to object spaces

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

Page 13: From relational data to object spaces

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.

Page 14: From relational data to object spaces

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

Page 15: From relational data to object spaces

Demo Demo Query (Object): NH & LINQ

Page 16: From relational data to object spaces

Repository [P of EAA, 322]

Mediates between the domain and data mapping layers

using a collection-like interface for accessing domain

objects.

Page 17: From relational data to object spaces

Demo Demo Repository: NSK

Page 18: From relational data to object spaces

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?

Page 19: From relational data to object spaces

Demo Demo Partitioning: NHibernate’s Projections & «LINQ feat. Anonymous Types»

Page 20: From relational data to object spaces

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.

Page 21: From relational data to object spaces

Demo Demo ISession

Page 22: From relational data to object spaces

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

Page 23: From relational data to object spaces

Demo Demo Identity Map

Page 24: From relational data to object spaces

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

Page 25: From relational data to object spaces

Demo Demo Persistence

Page 26: From relational data to object spaces

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