NHibernate (The ORM For .NET Platform)

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

2 comments

Comments 1 - 2 of 2 previous next Post a comment

  • + samnang.chhun Samnang Chhun 1 month ago
    @csokun,
    Personality, I teat it both ways.
  • + csokun csokun 1 month ago
    So you treat AR as and ORM rather a pattern
Post a comment
Embed Video
Edit your comment Cancel

3 Favorites

NHibernate (The ORM For .NET Platform) - Presentation Transcript

  1. Samnang Chhun (samnang@wowkhmer.com) Software Engineer, http://tech.wowkhmer.com
  2. Introduction to Object-Relational Mapping Introduction to NHibernate NHibernate Basics Mapping Inheritance hierarchies Advanced Querying Additional Reading 2
  3. 3
  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 ORM Objects Relational 4
  5. Performance or Scalability Productivity: less code to write/maintain Abstraction: transient to different DB technologies Simplification and Consistency Quality: depending on the product 5
  6. ADO.NET Entity Framework (released with .NET 3.5 SP1) Business Logic Toolkit for .NET Castle ActiveRecord IBatis.Net LightSpeed Linq (Language Integrated Query) LLBLGen LLBLGen Pro NHibernate Neo …etc 6
  7. 7
  8. Initially developed for Java created in late 2001 by Gavin King absorbed by the JBoss Group / 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 - NHibernate is licensed under the LGPL (Lesser GNU Public License) 8
  9. RDBMS 9
  10. ISessionFactory One per database (or application) Expensive to create Reads configuration ISession Portal to the database Saves, retrieves ITransaction Encapsulates database transactions SessionFactory Session Transaction 10
  11. 11
  12. 12
  13. <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) 13
  14. Element Description .NET Type <set> An unordered collection Iesi.Collections.ISet that does not allow Iesi.Collections.Generic.ISet<T> duplicates. <list> An ordered collection that System.Collections.IList allows duplicates System.Collections.Generic.IList<T> <bag> An unordered collection System.Collections.IList that allow duplicatd System.Collections.Generic.IList<T> 14
  15. 15
  16. <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
  17. Pros Simple approach Easy to add new classes, you just need to add new columns for the additional data Data access is fast because the data is in one table Ad-hoc reporting is very easy because all of the data is found in one table. Cons Coupling 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 hierarchy Space potentially wasted in the database Table can grow quickly for large hierarchies. 17
  18. <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
  19. Pros Easy to understand because of the one-to-one mapping Very easy to modify superclasses and add new subclasses as you merely need to modify/add one table Data size grows in direct proportion to growth in the number of objects. Cons There 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 tables Ad-hoc reporting on your database is difficult, unless you add views to simulate the desired tables. 19
  20. <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
  21. Pros Easy to do ad-hoc reporting as all the data you need about a single class is stored in only one table Good performance to access a single object’s data. Cons When you modify a class you need to modify its table and the table of any of its subclasses. 21
  22. 22
  23. 23
  24. Object oriented querying Increase compile-time syntax-checking Easy to write Hard to read 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(); 24
  25. String based querying Object-Oriented SQL Similar to SQL Speak in terms of objects Case sensitive Very flexible Zero compile-time syntax-checking • from Customer c where c.Name like :name • select count(*) from Customer c 25
  26. Powerful way to (simply) return a group of like objects from the DB. Wonderfully simple to work with Great way to quickly process a “…where A=<something> and B=<something> and C=<something>…” Cat cat = new Cat(); cat.Sex = 'F'; cat.Color = Color.Black; List results = session.CreateCriteria(typeof(Cat)) .Add( Example.Create(cat) ) .List(); 26
  27. You can submit SQL statements to NHibernate if the other methods of querying a database do not fit your needs utilize database specific features • 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)); 27
  28. 28
  29. 29
  30. Nhibernate.Contrib Mapping.Attributes Cache Search Validator Burrow LINQ to NHibernate Shards Fluent Interface to NHibernate 30
  31. http://en.wikipedia.org/wiki/Object-relational_mapping NHibernate in Action NHibernate Reference Documentation 1.2.0 http://code.google.com/p/sharp-architecture/ http://www.codeproject.com/KB/database/Nhibernate_M ade_Simple.aspx http://www.codeproject.com/KB/architecture/NHibernate BestPractices.aspx www.hibernate.org NHibernate FAQ Summer of NHibernate 31

+ Samnang ChhunSamnang Chhun, 11 months ago

custom

2976 views, 3 favs, 3 embeds more stats

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 2976
    • 2967 on SlideShare
    • 9 from embeds
  • Comments 2
  • Favorites 3
  • Downloads 111
Most viewed embeds
  • 7 views on http://tech.wowkhmer.com
  • 1 views on http://192.168.10.100
  • 1 views on http://feeds.feedburner.com

more

All embeds
  • 7 views on http://tech.wowkhmer.com
  • 1 views on http://192.168.10.100
  • 1 views on http://feeds.feedburner.com

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories