nhibernate for .net
DESCRIPTION
TRANSCRIPT
![Page 1: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/1.jpg)
1
NHibernate for .NETAlbert Kuo
![Page 2: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/2.jpg)
2
Introduction to ORM Introduction to NHibernate Scenario NHibernate Demo◦Demo Process
Hibernate-config.xml Mapping files & classes SessionManager DAO (Data Access Object) Create ASPX to do testing
Reference
Agenda
![Page 3: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/3.jpg)
3
Introduction to ORM
![Page 4: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/4.jpg)
4
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 hierarchical◦Databases are relational
What is ORM?
ORM
objects relational
![Page 5: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/5.jpg)
5
Productivity◦ Eliminates lots of repetitive code – focus on business logic◦ Database schema is generated automatically
Maintainability◦ Fewer lines of code – easier to understand◦ Easier to manage change in the object model
Performance◦ Lazy loading – associations are fetched when needed◦ Caching
Database vendor independence◦ The underlying database is abstracted away◦ Can be configured outside the application
ORM Benefits
![Page 6: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/6.jpg)
6
ORM and Architacture
Oracle, MS SQL Server, DB2, MySQL, Sybase, etc.
![Page 7: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/7.jpg)
7
Introduction to NHibernate
![Page 8: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/8.jpg)
8
Initially developed for Java◦ created in late 2001 by Gavin King◦ absorbed by the JBossGroup / Red Hat
Ported to .NET 1.1, 2.0, 3.5◦ Resulting product called “NHibernate”
All popular databases supported◦ Oracle, SQL Server, DB2, SQLite, PostgreSQL, MySQL,
Sybase, Firebird, … XML-based configuration files Good community support Free/open source -NHibernateis licensed under the
LGPL (Lesser GNU Public License)
Introduction to NHibernate
![Page 9: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/9.jpg)
9
High-level overview of the Nhibernate API
![Page 10: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/10.jpg)
10
NHibernate managing database access
![Page 11: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/11.jpg)
11
Access Persistent Object
![Page 12: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/12.jpg)
12
ISessionFactory◦One per database (or application)◦Expensive to create
Reads configuration ISession◦Portal to the database◦Saves, retrieves
ITransaction◦Encapsulates database transactions
Nhibernate in a Nutshell
![Page 13: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/13.jpg)
13
Scenario
![Page 14: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/14.jpg)
14
One people may have more than one contact One contact belongs to one people
Database schema
![Page 15: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/15.jpg)
15
Use Case Diagram
![Page 16: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/16.jpg)
16
Sequence diagram – create
![Page 17: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/17.jpg)
17
Sequence diagram – Read
![Page 18: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/18.jpg)
18
Sequence diagram – Update
![Page 19: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/19.jpg)
19
Sequence diagram – Delete
![Page 20: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/20.jpg)
20
Project Directories
Stored persistence tier-related code Stored presentation tier-related code
Data access objects
Value objects and mapping files
![Page 21: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/21.jpg)
21
NHibernate
![Page 22: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/22.jpg)
22
Libraries
Required library Required for lazy loading
Antlr3.Runtime.dll Iesi.Collections.dll log4net.dll NHibernate.dll
Castle.Core.dll Castle.DynamicProxy2.dll NHibernate.ByteCode.Ca
stle.dll
![Page 23: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/23.jpg)
23
Hibernate-config.xml
Mapping files & classes SessionManager
DAO (Data Access Object)
Create ASPX to do testing
Demo Process
![Page 24: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/24.jpg)
24
![Page 25: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/25.jpg)
25
Hibernate-config.xml
![Page 26: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/26.jpg)
26
Hibernate-config.xml
![Page 27: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/27.jpg)
27
![Page 28: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/28.jpg)
28
<class> declare a persistent class <id> defines the mapping from that property to the
primary key column◦ Specifies 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. Associations◦ One-to-Many◦ Many-to-One◦ Many-to-Many◦ One-to-One (uncommon)
Mapping Concepts
![Page 29: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/29.jpg)
29
Mapping Collections
![Page 30: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/30.jpg)
30
Mapping files & classes
ORM
objects relational
![Page 31: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/31.jpg)
31
Mapping files & classes – cont.
![Page 32: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/32.jpg)
32
Mapping files & classes – cont.
![Page 33: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/33.jpg)
33
Mapping files & classes – cont.
![Page 34: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/34.jpg)
34
Session 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 work
![Page 35: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/35.jpg)
35
DAO (Data Access Object)
![Page 36: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/36.jpg)
36
DAO (Data Access Object) – cont.
![Page 37: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/37.jpg)
37
DAO (Data Access Object) – cont.
![Page 38: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/38.jpg)
38
DAO (Data Access Object) – cont.
![Page 39: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/39.jpg)
39
![Page 40: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/40.jpg)
40
Transaction: A set of database operations which must be executed in entirety or not at all
Should end either with a commit or a rollback All communication with a database has to occur
inside a transaction!
Transactions
![Page 41: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/41.jpg)
41
Transactions – cont.
![Page 42: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/42.jpg)
Create a new record
![Page 43: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/43.jpg)
43
Read data by criteria
![Page 44: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/44.jpg)
Update record
![Page 45: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/45.jpg)
45
Delete record
![Page 46: NHibernate for .NET](https://reader033.vdocument.in/reader033/viewer/2022061120/546d0ca8af79595e658b82a1/html5/thumbnails/46.jpg)
46
O/R Mapping◦ http://en.wikipedia.org/wiki/Object-relational_mapping
Official site◦www.hibernate.org
NHibernate in Action NHibernate Made Simple◦ http://www.codeproject.com/KB/database/Nhibernate_Ma
de_Simple.aspx
NHibernate Best Practices ◦ http://www.codeproject.com/KB/architecture/NHibernateB
estPractices.aspx
Reference