nhibernatethe orm for net platform 1226744632929962 8

32
Samnang Chhun ([email protected] ) Software Engineer, http://tech.wowkhmer.com

Upload: nicolas-thon

Post on 22-Jun-2015

756 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Nhibernatethe Orm For Net Platform 1226744632929962 8

Samnang Chhun ([email protected])Software Engineer, http://tech.wowkhmer.com

Page 2: Nhibernatethe Orm For Net Platform 1226744632929962 8

Introduction to Object-Relational MappingIntroduction to NHibernateNHibernate BasicsMapping Inheritance hierarchiesAdvanced QueryingAdditional Reading

2

Page 3: Nhibernatethe Orm For Net Platform 1226744632929962 8

3

Page 4: Nhibernatethe Orm For Net Platform 1226744632929962 8

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 relational

4

Objects Relational

ORM

Page 5: Nhibernatethe Orm For Net Platform 1226744632929962 8

Performance or ScalabilityProductivity: less code to write/maintainAbstraction: transient to different DB technologiesSimplification and ConsistencyQuality: depending on the product

5

Page 6: Nhibernatethe Orm For Net Platform 1226744632929962 8

ADO.NET Entity Framework (released with .NET 3.5 SP1)Business Logic Toolkit for .NETCastle ActiveRecordIBatis.NetLightSpeedLinq (Language Integrated Query)LLBLGenLLBLGen ProNHibernateNeo …etc

6

Page 7: Nhibernatethe Orm For Net Platform 1226744632929962 8

7

Page 8: Nhibernatethe Orm For Net Platform 1226744632929962 8

Initially developed for Javacreated in late 2001 by Gavin Kingabsorbed by the JBoss Group / Red Hat

Ported 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 - NHibernate is licensed under the LGPL (Lesser GNU Public License)

8

Page 9: Nhibernatethe Orm For Net Platform 1226744632929962 8

9

RDBMS

Page 10: Nhibernatethe Orm For Net Platform 1226744632929962 8

ISessionFactoryOne per database (or application)Expensive to create

Reads configurationISession

Portal to the databaseSaves, retrieves

ITransactionEncapsulates database transactions

10

SessionFactory Session Transaction

Page 11: Nhibernatethe Orm For Net Platform 1226744632929962 8

11

Page 12: Nhibernatethe Orm For Net Platform 1226744632929962 8

12

Page 13: Nhibernatethe Orm For Net Platform 1226744632929962 8

<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-ManyMany-to-OneMany-to-ManyOne-to-One (uncommon)

13

Page 14: Nhibernatethe Orm For Net Platform 1226744632929962 8

14

Element Description .NET Type<set> An unordered collection

that does not allow duplicates.

Iesi.Collections.ISetIesi.Collections.Generic.ISet<T>

<list> An ordered collection that allows duplicates

System.Collections.IListSystem.Collections.Generic.IList<T>

<bag> An unordered collection that allow duplicatd

System.Collections.IListSystem.Collections.Generic.IList<T>

Page 15: Nhibernatethe Orm For Net Platform 1226744632929962 8

15

Page 16: Nhibernatethe Orm For Net Platform 1226744632929962 8

<class name="Animal"><id name="Id">

<generator class="native" /></id><discriminator column="AnimalType" length="5" /><property name="Name" />

<subclass name="Dog" discriminator-value="Dog"><property name="Breed" />

</subclass><subclass name=“Frog" discriminator-value=“Frog">

<property name=“TongueLength" /></subclass>

</class>

16

Page 17: Nhibernatethe Orm For Net Platform 1226744632929962 8

ProsSimple approachEasy to add new classes, you just need to add new columns for the additional dataData access is fast because the data is in one tableAd-hoc reporting is very easy because all of the data is found in one table.

ConsCoupling within the class hierarchy is increased because all classes are directly coupled to the same table. A change in one class can affect the table which can then affect the other classes in the hierarchySpace potentially wasted in the databaseTable can grow quickly for large hierarchies. 17

Page 18: Nhibernatethe Orm For Net Platform 1226744632929962 8

<class name="Animal"><id name="Id">

<generator class="native" /></id><property name="Name" /><joined-subclass name="Dog">

<key column="Id" /><property name="Breed" />

</joined-subclass><joined-subclass name="Frog">

<key column="Id" /><property name="TongueLength" />

</joined-subclass></class>

18

Page 19: Nhibernatethe Orm For Net Platform 1226744632929962 8

ProsEasy to understand because of the one-to-one mappingVery easy to modify superclasses and add new subclasses as you merely need to modify/add one tableData size grows in direct proportion to growth in the number of objects.

ConsThere are many tables in the database, one for every class (plus tables to maintain relationships)Potentially takes longer to read and write data using this technique because you need to access multiple tablesAd-hoc reporting on your database is difficult, unless you add views to simulate the desired tables. 19

Page 20: Nhibernatethe Orm For Net Platform 1226744632929962 8

<class name="Frog"><id name="Id">

<generator class="native" /></id><property name="Name" /><property name="TongueLength" />

</class>

<class name="Dog"><id name="Id">

<generator class="native" /></id><property name="Name" /><property name="Breed" />

</class>

20

Page 21: Nhibernatethe Orm For Net Platform 1226744632929962 8

ProsEasy to do ad-hoc reporting as all the data you need about a single class is stored in only one tableGood performance to access a single object’s data.

ConsWhen you modify a class you need to modify its table and the table of any of its subclasses.

21

Page 22: Nhibernatethe Orm For Net Platform 1226744632929962 8

22

Page 23: Nhibernatethe Orm For Net Platform 1226744632929962 8

23

Page 24: Nhibernatethe Orm For Net Platform 1226744632929962 8

Object oriented queryingIncrease compile-time syntax-checkingEasy to writeHard to read

24

ICriteria crit = sess.CreateCriteria(typeof(Cat)); crit.SetMaxResults(50); List topCats = crit.List();

IList cats = sess.CreateCriteria(typeof(Cat)) .Add( Restrictions.Like("Name", "Fritz%")) .Add( Restrictions.Between("Weight", minWeight,

maxWeight)).List();

Page 25: Nhibernatethe Orm For Net Platform 1226744632929962 8

String based queryingObject-Oriented SQLSimilar to SQLSpeak in terms of objectsCase sensitiveVery flexibleZero compile-time syntax-checking

25

• from Customer c where c.Name like :name

• select count(*) from Customer c

Page 26: Nhibernatethe Orm For Net Platform 1226744632929962 8

Powerful way to (simply) return a group of like objects from the DB.

Wonderfully simple to work withGreat way to quickly process a “…where A=<something> and B=<something> and C=<something>…”

26

Cat cat = new Cat(); cat.Sex = 'F'; cat.Color = Color.Black; List results = session.CreateCriteria(typeof(Cat))

.Add( Example.Create(cat) )

.List();

Page 27: Nhibernatethe Orm For Net Platform 1226744632929962 8

You can submit SQL statements to NHibernate if the other methods of querying a database do not fit your needsutilize database specific features

27

• sess.CreateSQLQuery("SELECT * FROM CATS") .AddScalar("ID", NHibernateUtil.Int32) .AddScalar("NAME", NHibernateUtil.String) .AddScalar("BIRTHDATE", NHibernateUtil.Date);

• sess.CreateSQLQuery("SELECT * FROM CATS") .AddEntity(typeof(Cat));

Page 28: Nhibernatethe Orm For Net Platform 1226744632929962 8

28

Page 29: Nhibernatethe Orm For Net Platform 1226744632929962 8

29

Page 30: Nhibernatethe Orm For Net Platform 1226744632929962 8

Nhibernate.ContribMapping.AttributesCacheSearchValidatorBurrowLINQ to NHibernateShards

Fluent Interface to NHibernate

30

Page 31: Nhibernatethe Orm For Net Platform 1226744632929962 8

http://en.wikipedia.org/wiki/Object-relational_mappingNHibernate in ActionNHibernate Reference Documentation 1.2.0http://code.google.com/p/sharp-architecture/http://www.codeproject.com/KB/database/Nhibernate_Made_Simple.aspxhttp://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspxwww.hibernate.orgNHibernate FAQSummer of NHibernate

31

Page 32: Nhibernatethe Orm For Net Platform 1226744632929962 8