Upload
alexa-knight
View
221
Download
0
Tags:
Embed Size (px)
Citation preview
• 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
A little history…
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
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++
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”
The object/relational conflict
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
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
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
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
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
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
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
[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
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
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
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
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
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)
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!
Questions