NHibernate for .NETAlbert Kuo1
Introduction to ORMIntroduction to NHibernateScenarioNHibernate DemoDemo ProcessHibernate-config.xmlMapping files & classesSessionManagerDAO (Data Access Object)Create ASPX to do testingReference2Agenda
Introduction to ORM3
Object-relational mapping (aka ORM, O/RM, and O/R mapping) is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages (Wikipedia)Objects are hierarchicalDatabases are relationalWhat is ORM?ORMobjectsrelational4
ProductivityEliminates lots of repetitive code – focus on business logicDatabase schema is generated automaticallyMaintainabilityFewer lines of code – easier to understandEasier to manage change in the object modelPerformanceLazy loading – associations are fetched when neededCachingDatabase vendor independenceThe underlying database is abstracted awayCan be configured outside the applicationORM Benefits5
6ORM and ArchitactureOracle, MS SQL Server, DB2, MySQL, Sybase, etc.
Introduction to NHibernate7
Initially developed for Javacreated in late 2001 by Gavin Kingabsorbed by the JBossGroup / Red HatPorted to .NET 1.1, 2.0, 3.5Resulting product called “NHibernate”All popular databases supportedOracle, SQL Server, DB2, SQLite, PostgreSQL, MySQL, Sybase, Firebird, …XML-based configuration filesGood community supportFree/open source -NHibernateis licensed under the LGPL (Lesser GNU Public License)Introduction to NHibernate8
9High-level overview of the Nhibernate API
NHibernate managing database access10
11Access Persistent Object
ISessionFactoryOne per database (or application)Expensive to createReads configurationISessionPortal to the databaseSaves, retrievesITransactionEncapsulates database transactionsNhibernate in a Nutshell12
Scenario13
One people may have more than one contactOne contact belongs to one people14Database schema
15Use Case Diagram
16Sequence diagram – create
17Sequence diagram – Read
18Sequence diagram – Update
19Sequence diagram – Delete
20Project DirectoriesStored persistence tier-related codeStored presentation tier-related codeData access objectsValue objects and mapping files
NHibernate21
LibrariesRequired libraryRequired for lazy loadingAntlr3.Runtime.dllIesi.Collections.dlllog4net.dllNHibernate.dllCastle.Core.dllCastle.DynamicProxy2.dllNHibernate.ByteCode.Castle.dll22
23Demo Process
24
25Hibernate-config.xml
26Hibernate-config.xml
27
<class> declare a persistent class<id> defines the mapping from that property to the primary key columnSpecifies strategy<property> declares a persistent property of the class<component> maps properties of a child object to columns of the table of a parent class.AssociationsOne-to-ManyMany-to-OneMany-to-ManyOne-to-One (uncommon)28Mapping Concepts
29Mapping Collections
30Mapping files & classesORMobjectsrelational
31Mapping files & classes – cont.
32Mapping files & classes – cont.
33Mapping files & classes – cont.
34Session Manager[ISession]Obtained from a SessionFactory instance
Responsible for storing and retrieving objects
Think of it as a collection of loaded objects related to a single unit of work35DAO (Data Access Object)
36DAO (Data Access Object) – cont.
37DAO (Data Access Object) – cont.
38DAO (Data Access Object) – cont.
39
Transaction: A set of database operations which must be executed in entirety or not at allShould end either with a commit or a rollbackAll communication with a database has to occur inside a transaction!40Transactions
41Transactions – cont.
Create a new record
43Read data by criteria
Update record
45Delete record

NHibernate for .NET