NHibernate for .NET

6,456 views
6,238 views

Published on

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,456
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
269
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

NHibernate for .NET

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

×