an object/relational mapping tool free and open source simplifies storage of object data in a...
TRANSCRIPT
![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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/1.jpg)
![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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/2.jpg)
• 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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/15.jpg)
[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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062404/551512e855034673228b4a8c/html5/thumbnails/22.jpg)
Questions