Object Relational Mapping In Real World Applications

Post on 22-May-2015

2.115 views 2 download

Tags:

description

Object Relational Mapping in real-world applications with Oliver Sturm

Transcript of Object Relational Mapping In Real World Applications

Oliver Sturm oliver@sturmnet.orgolivers@devexpress.comhttp://www.sturmnet.org/blog

Oliver Sturm

Object/Relational Mapping in real-world applications

Person

XPObject

Class

Fields

name

Properties

Name

Numbers

Methods

OnDeleting

Person

PhoneNumber

XPObject

Class

Fields

number

person

Properties

Number

Person

Methods

PhoneNumber (+ 1 overload)

XPObject

XPCustomObject

Abstract Class

Properties

Oid

Methods

XPObject (+ 1 overload)

XPCustomObject

XPBaseObject

Abstract Class

Properties

IsDeleted

Methods

XPCustomObject (+ 1 overload)

What is Object/Relational Mapping?

Object/Relational Mapping (ORM) is a technique that allows programmers to map fields and properties of classes to fields in relational database tables, and vice versa, in order to store object data in relational databases.

Why is ORM being used?

• ORM leverages existing knowledge and skills related to OO programming and unifies the programming model

• ORM can provide a level of abstraction towards the database

• ORM saves programmer time!

Important differences

Several areas:

1. Data Binding2. Business Logic3. Multi-Tier

Data Binding ADO.NET

Name City

Mike Reading

Ruth Liverpool

Sue London

Brad Birmingham

Jim Newcastle

Data

Bindin

g

DataTables, DataSets – query results

AD

O.N

ET

ORM

Data Binding?

Data Binding ORM

Name City

Mike Reading

Ruth Liverpool

Sue London

Brad Birmingham

Jim NewcastleAD

O.N

ET

Fetch data

IListIBindingListITypedList...

ORM

Fetch data

Business Logic ADO.NET

SQL

Stored procedures (T-SQL, ...)Trigger code...

ADO.NET, SQL code

Functionality

Functionality

Functionality must be implemented in different models, using different programming languages.

Business Logic ORM

SQL

Stored procedures (T-SQL, ...)Trigger code ...

ORM

Functionality on the serverimplemented in .NET

ORM leverages existing knowledge and skills related to OO programming and unifies the programming model

ORM can provide a level of abstraction towards the database

Functionality

Functionality

Functionality is implemented in the same model, using the same programming language.

Demo

Differences between ADO.NET and ORM based applications – Data

Binding and Business Logic

Where does my Business Logic live?

Several possibilities:

1.In the persistent classes themselves2.In separate classes – separate BL layer3.In the process implementations

When is Business Logic executed?

More things to consider:

1.Automatically, e.g. when objects are created or saved.2.“Manually”, i.e. from a process implementation

Important for Business Logic!

Expectations regarding data consistency could be different when ORM is being used: suddenly data has to be consistent even if it lives “only” in RAM!Whether or not this is relevant for you depends on your use case.

Important differences

Several areas:

1. Data Binding2. Business Logic3. Multi-Tier

Multi-Tier with ADO.NET

Name City

Mike Reading

Ruth Liverpool

Sue London

Brad Birmingham

Jim Newcastle

Network – XML

WebServices, WCF, ...

Name City

Mike Reading

Ruth Liverpool

Sue London

Brad Birmingham

Jim Newcastle

SerializationDeserialization

Result:Identical data!

Multi-Tier with ORM

• Objects contain more than just data• The ORM system does a lot of

management work for us, and so it has certain expectations about status.Consequence: Communication must be designed to consider ORM requirements

Multi-Tier with ORM

Dynamic SQL

Network

Metadata

Transport format

Transport format / SQL

Persistent Classes / Query classes / Transport format

Persistent Classes

Layer Data used

ORM

Optional!

Application

The application works with persistent classes and calls functionality of the ORM system.On this layer, the ORM system provides most of its services – Identity Map, Transaction handling, ...

The transport format defines Data Transfer Objects (DTOs). This layer is optional – it’s used when “Dynamic SQL” is a separate tier.

Finally the ORM system dynamically generates SQL code.

Demo

Tunnelling through .NET Remoting

Tunnelling is not specific to Remoting

• .NET Remoting is really easy to use, as it maps all .NET data types directly

• I have examples for tunnelling through XML Web Services, WCF and RemObjects.

Further information

• My blog: http://www.sturmnet.org/blog

• My email: oliver@sturmnet.org

• Commercial: http://www.devexpress.com/xpo