Object Relational Mapping (ORM)

22
Object Relational Mapping (ORM)

description

Object Relational Mapping (ORM). Persistence. Almost all applications require some form of persistence. Almost all persistence is done using relational databases. Usually the business classes are the ones that are persisted. Actual Example. - PowerPoint PPT Presentation

Transcript of Object Relational Mapping (ORM)

Page 1: Object Relational Mapping (ORM)

Object Relational Mapping(ORM)

Page 2: Object Relational Mapping (ORM)

Persistence

• Almost all applications require some form of persistence.• Almost all persistence is done using relational

databases.• Usually the business classes are the ones that are

persisted.

Page 3: Object Relational Mapping (ORM)

Actual ExamplelsSqlStatement = "INSERT INTO tblClaimsHistory " +

"(fldBatchLet, fldBatchNum, fldBatchFy,fldBatchReqSeq, fldPatientSSN,fldProvTaxId,fldProvSuff,fldProvType,"+

"fldDiag1,fldDiag2,fldDiag3,fldDiag4, " + "fldPatientLastName, fldPatientFirstName, fldProviderName ," +

"fldTotalCharged,fldTotalToPay,fldTotalDue,fldTotalCalcAmount,fldTotalPaid, "+"fldRecDate, fldClaimBeginDate,

fldClaimEndDate,fldCurrDocNum, fldMailCode, MessageIdentifier ) " + "VALUES ('"+lsBatchNum.Substring(0,1)+"',"+lsBatchNum.Substring(1)+","+lsBatchFy+",'"+ lsBatchReqSeq + "', '" +lsSSN+"','"+lsFedTaxID+"','"+lsProvTaxSfx+"','"+ lsProviderType +"','"+ fldDiag1+"','"+fldDiag2+"','"+fldDiag3+"','"+fldDiag4 +fldPatientLastName+"','"+fldPatientFirstName+"','" +lsProviderName+"',"+

fldTotalCharged+","+fldTotalToPay+","+fldTotalDue+","+fldTotalCalcAmount+","+fldTotalAmtPaid +",'"+ ClaimRecievedDate + "','" + fldClaimBeginDate + "','" + fldClaimEndDate + "','"+fldCurrDocNum+"','"+fldMailCode+"','"+MessageIdentifier+"') " +"SELECT @@IDENTITY AS 'Identity'";

Page 4: Object Relational Mapping (ORM)

And as preparation we need:

lsBeginDate = lsBeginDate.Replace("-", "/");

lsEndDate = lsEndDate.Replace("-", "/");

OR

Replace("'","''");

Page 5: Object Relational Mapping (ORM)

Usually we have

• A business Object • & A database Table

-ID-Name-Description-Address

CustomerCustomer

ID Name Desciption Addresss

Page 6: Object Relational Mapping (ORM)

Traditional way to handle persistence

• Using a N-Tier design– UI Tier– Business Tier– Data Tier– Database

UI tier

Data Tier

Business tier

Dat

aba

se

Page 7: Object Relational Mapping (ORM)

This approach requires

• Long and tedious work to:– Build SQL statements for Insert, update, Delete,

select– Send the Objects’ properties to the data layer as

parameters– Account for different types of databases/data fields

(ex: ‘ ‘ for strings and dates, format dates, handling Null values,..

– Handle IDs, Keys,..

Page 8: Object Relational Mapping (ORM)

This work is usually..

• Time Consuming• Boring• Error Prone• And most importantly:

– Doesn’t require much thinking– Shifts focus from main target ( handle business case)

Page 9: Object Relational Mapping (ORM)

ORM

• ORM is about Mapping an Object to one or more database tables.

• It eliminates the need to create a data layer tier ( data layer is implicit)

• “In a nutshell, object/relational mapping is the automated (and transparent) persistence of objects in an application to the tables in a relational database, using metadata that describes the mapping between the objects and the database.”

• In short: ORM saves you from writing boring and error prone code thus saving time and getting better quality.

Page 10: Object Relational Mapping (ORM)

Main Advantages of ORM

• Better productivity ( no more tedious coding)• Better performance• DB independence• Less error prone

Page 11: Object Relational Mapping (ORM)

Many ORM tools, Many Approaches

• There are different Tools for implementing ORM using different approach– Using Attributes in the classes (GENTLE.NET)– Using XML mapping Files (NHIBERNATE)– Using Visual Mapping,– … etc.

Page 12: Object Relational Mapping (ORM)

Introduction to GENTLE.NET

• Open source, free tool.• Supports many DB types.• Uses attributes .. No complex configuration files• Queries generated at runtime using features in the .NET

framework (class attributes)• Handles “impedance mismatch” (Null values, auto

generated keys,..)• Handles transactions• Supports caching• Supports validations on the object level

Page 13: Object Relational Mapping (ORM)

Example Classpublic class User{

private int userId;private string userName;public User( int userId, string userName

){

this.userId = userId;this.userName = userName;

}public int Id{

get{ return userId; }set{ userId = value; }

}public string Name{

get{ return userName; }set{ userName = value; }

}}

• To be saved in

User

ID Name

Page 14: Object Relational Mapping (ORM)

All we need to do is

1. Add reference to 3 DLLs

2. Add 3 “using..” lines

3. Add appropriate attributes to object propertied

Page 15: Object Relational Mapping (ORM)

Class Becomesusing Gentle.Framework;[TableName("Users")]public class User : Persistent{

private int userId;private string userName;// this is used by clients to construct new userspublic User( string userName ) : this( 0, userName ) {}// this is used by Gentle to reconstruct objects read from the databasepublic User( int userId, string userName ){

this.userId = userId;this.userName = userName;

}// this is used by client to fetch users from the databasestatic public User Retrieve( int userId ){

Key key = new Key( typeof(User), true, "Id", userId );return Broker.RetrieveInstance( typeof(User), key ) as User;

}[TableColumn("UserId"), PrimaryKey(AutoGenerated=true)]public int Id{

get{ return userId; }set{ userId = value; }

}[TableColumn(NotNull=true)]public string Name{

get{ return userName; }set{ userName = value; }

}}

Page 16: Object Relational Mapping (ORM)

• That was all the “SQL” we need to write.• Also we don’t need to “create” an object

from a row in DB. It is done implicitly• To save an object all we need to do is:

User ford = new User( "Ford Prefect" );ford.Persist();

• To Load an object all we need to do is:User prefect = User.Retrieve( ford.Id );Using Id ( or any other Key)

BINGO !!!

Page 17: Object Relational Mapping (ORM)

Demo

Page 18: Object Relational Mapping (ORM)

AdditivesUsing SQL builder

static public IList ListByNameStartsWith( string partialName ){SqlBuilder sb = new SqlBuilder( StatementType.Select, typeof(User) );// note: the partialName parameter must also contain the %'s for the LIKE query!

sb.AddConstraint( Operator.Like, "Name", partialName );// passing true indicates that we'd like a list of elements, i.e. that no primary key// constraints from the type being retrieved should be added to the statement

SqlStatement stmt = sb.GetStatement( true );// execute the statement/query and create a collection of User instances from the

result setreturn ObjectFactory.GetCollection( typeof(User), stmt.Execute() );}

Page 19: Object Relational Mapping (ORM)

Additives

• Retrieving Lists of objects

• IList is directly bindable with data grids.

GentleList list = new GentleList( typeof(User), parentInstance );

Page 20: Object Relational Mapping (ORM)

Additives

• Generating business class using MyGeneration– Demo

Page 21: Object Relational Mapping (ORM)

References

• http://www.mertner.com/projects/gentle

• http://www.mygenerationsoftware.com

Page 22: Object Relational Mapping (ORM)

Thank You!

Emad Magdy