22

An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Embed Size (px)

Citation preview

Page 1: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain
Page 2: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

• An “Object/Relational Mapping” tool• Free and open source• Simplifies storage of object data in a

relational database• Removes the need to write and

maintain SQL queries

Page 3: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

A little history…

Page 4: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Origins of Databases and Objects

• DBMS technology invented late 1960’s• OO technology invented late 1960’s• Independent evolution for over 20 years• Different audiences

– DBMS: corporate information systems– OO: academic and scientific

Page 5: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

The Rise of Client/Server

• Corporate IT in early ’90s• High cost of mainframe hardware• Low cost of the IBM PC-based

platform• Desire to move away from COBOL• The “hot” languages were OO

– Smalltalk– C++

Page 6: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Object Meets Relational

• Corporate IT already deeply invested in RDBMS

• “We’ll just connect up our new client server applications to these databases!”

• Efforts to marry the two technologies• Discovery of the “O/R Impedance

Mismatch”

Page 7: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

The object/relational conflict

Page 8: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

DBMS Representation

• Based on the concept of a Relation– A set of data values that “go together”– Represented as a Row in a Table

• No two rows should be duplicates– Useless to state the same fact twice

• Very useful for flexible set manipulation– SQL queries

Page 9: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Object-Oriented Representation

• An object is a model of something in the real world

• Public interfaces– How an object interacts with the rest of

the world

• Private members– How an object behaves internally

Page 10: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Object-Oriented Representation

• Merges data and behavior– Allows an object to initiate interactions

and respond flexibly to them– Important for modeling the real world

• Effective OO models allow for very high reuse– Parcel program logic and data into self-

defined and self-consistent bundles

Page 11: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Object-Oriented Representation

• Each object (instance) is unique– even if all data members are equal

• Object types inherit from other types– allows for the creation of rich, powerful

models– allows opportunities for reuse

Page 12: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

O/R “Impedance Mismatch”

• Impedance matching– from electronics theory– matching a power source with a power

load

• Mismatch between object and relational technologies– Different theoretical foundations

• Relations are facts about data relatedness• Objects are models of reality

Page 13: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

O/R “Impedance Mismatch”

• Mismatched uniqueness rules– Rows containing identical data are “the same”

• Considered undesirable

– Objects containing identical data are different• Common and useful• “equal”, but not “==“

• Mismatched data model topologies– Relational tables are flat– Object types are hierarchical

Page 14: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

O/R Mapping Tools

• Invented in early 1990’s• One of the first came from Cleveland

– Raleigh Systems’ ObjectPM

• Shield the OO application from the database paradigm

• Mappings to tables and columns are localized in configuration files

Page 15: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

[Enter: Hibernate]

• Initially developed for Java– created in late 2001 by Gavin King– absorbed by the JBoss Group / Red Hat

• Ported to .NET 1.1 and 2.0– Resulting product called “NHibernate”

• All popular databases supported• XML-based configuration files

Page 16: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

What NHibernate Does

• Takes as input– a C# class with properties that need to be

stored in a database and retrieved later– an XML file containing mapping information

• Outputs automatically generated SQL – when you tell NHibernate to “save” an object– when you ask NHibernate to search/load

objects– can also create all database tables if desired

Page 17: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

How to Use NHibernate

• Step 1: create an object oriented business model– Start simple: one or two small classes– Note which properties will save in DBMS

• Implement the model using C#– Best kept in a separate C# project

Page 18: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Step 2: The Mapping File

• One per business model class• Named classname.hbm.xml• Tells NHibernate where data is to reside

– Name of table to store data for this class– Name of a column for each property in class

• Also describes relationships between classes– One-to-many, many-to-one, many-to-many

• Generated SQL is based on this information

Page 19: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Step 3: Configuration File

• XML-based file for global configuration• Usually named “hibernate.cfg.xml”• Application-wide settings• Database connection string• Database “dialect”

– Specifies database product and version– Allows NHibernate to use non-standard

features where appropriate

• Logging options

Page 20: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Step 4: User Interface Tasks

• Create business model objects• Create an NHibernate session object• Insert/Update to DBMS

– Session.save(object)

• Retrieve business model data from the DBMS as business model objects – Session.load(type, id)– Session.find(query)

Page 21: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Step 5: Unit Test and Debug

• Unit tests can be created for each persistent class if desired– VSTS unit testing support– NUnit

• Run, test and debug in the usual fashion

• Sit back and relax….• You’re done!

Page 22: An Object/Relational Mapping tool Free and open source Simplifies storage of object data in a relational database Removes the need to write and maintain

Questions