Introduction to NHibernate By Andrew Smith
The Basics <ul><li>Object Relation Mapper </li></ul><ul><li>Maps POCOs to database tables </li></ul><ul><li>Based on Java ...
Entity definitions <ul><li>“ Persistence ignorance” </li></ul><ul><ul><li>No need for special base class </li></ul></ul><u...
Class Diagram
Configuration <ul><li>Multiple options – Xml, ActiveRecord, Fluent NH </li></ul><ul><li>Xml: </li></ul><ul><li>1 overall c...
Sessions <ul><li>Very quick to create session </li></ul><ul><li>Disposable (may hold a DB connection open) </li></ul><ul><...
Mappings: Primary keys <ul><li>Entities must be identifiable </li></ul><ul><li>Multiple strategies for PK </li></ul><ul><l...
Mappings: Properties <ul><li>Maps database columns to properties/fields on entity </li></ul><ul><li>Can map columns to nes...
Demo: The basics <ul><li>Fetching single entity by ID </li></ul><ul><li>Updating </li></ul><ul><li>Inserting </li></ul>
Mappings: Relations <ul><li>Many types. Most common: bag and set </li></ul><ul><li>Assuming bi-directional relations and s...
Mappings: 1-* Relations <ul><li>Professor (1) – Class (*) </li></ul><ul><li>Bi-directional: <bag> and < many-to-one > </li...
Mappings: *-* Relations <ul><li>Student (*) – Class (*) </li></ul><ul><li>Bi-directional: 2 <bag> elements with link table...
Cascades <ul><li>Don’t have to explicitly call save / update / delete on related entities </li></ul><ul><li>Cascade types:...
Proxies <ul><li>Enables lazy loading - Just in time loading of data </li></ul><ul><ul><li>Lazy loaded collections </li></u...
Querying <ul><li>Lots of options: </li></ul><ul><ul><li>Criteria </li></ul></ul><ul><ul><li>DetachedCriteria </li></ul></u...
Querying: ICriteria <ul><li>Out-of-the-box method for building up a query in code </li></ul><ul><li>Requires an active ses...
Querying: Projections <ul><li>By default NH selects all mapped properties of entity </li></ul><ul><li>Projections allow co...
Querying: Aliases / Subcriteria <ul><li>Alias relations to refer to them later </li></ul><ul><li>Need to use them to refer...
Querying: DetachedCriteria <ul><li>Same capabilities as ICriteria </li></ul><ul><li>Does not require active session </li><...
Querying: MultiCriteria <ul><li>Enables multiple criteria to be evaluated in the one round trip to DB </li></ul><ul><li>Ve...
Querying: HQL <ul><li>Similar to SQL </li></ul><ul><li>Allows querying over domain entities </li></ul><ul><li>“ HQL is ful...
Querying: Linq <ul><li>NHibernate Linq 1.0 released </li></ul><ul><li>Supports just about anything you can do with criteri...
Caching: 1 st  level cache <ul><li>Every ISession has an in-built cache called 1 st  level cache </li></ul><ul><li>Stores ...
Caching: 2 nd  level cache <ul><li>Cache shared between sessions </li></ul><ul><li>Out of the box support for: </li></ul><...
Querying: Future Queries <ul><li>Executes a batch of queries in one round trip to DB </li></ul>
Why use it? <ul><li>Reduces repetitive, error prone data access code </li></ul><ul><li>Enables use of OOP </li></ul><ul><l...
Resources <ul><li>Book: NHibernate In Action (v 1.2) </li></ul><ul><li>NHForge.org </li></ul><ul><ul><li>NHibernate blog <...
Upcoming SlideShare
Loading in …5
×

Introduction to NHibernate

2,915 views

Published on

Published in: Technology, Spiritual
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
2,915
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
96
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to NHibernate

  1. 1. Introduction to NHibernate By Andrew Smith
  2. 2. The Basics <ul><li>Object Relation Mapper </li></ul><ul><li>Maps POCOs to database tables </li></ul><ul><li>Based on Java Hibernate. V stable </li></ul><ul><li>Generates SQL at run time </li></ul><ul><li>Database agnostic </li></ul>
  3. 3. Entity definitions <ul><li>“ Persistence ignorance” </li></ul><ul><ul><li>No need for special base class </li></ul></ul><ul><ul><li>No need to implement special interfaces </li></ul></ul><ul><li>Default constructor </li></ul><ul><li>Identity property (Primary key) </li></ul><ul><li>To support lazy loading, public properties and methods must be virtual </li></ul><ul><li>Collection properties must be declared as an interface type </li></ul>
  4. 4. Class Diagram
  5. 5. Configuration <ul><li>Multiple options – Xml, ActiveRecord, Fluent NH </li></ul><ul><li>Xml: </li></ul><ul><li>1 overall configuration section </li></ul><ul><li>Multiple “<entityname>.hbm.xml” embedded resource files </li></ul><ul><li>Intellisense: Place .xsd files in “Microsoft Visual Studio 9.0XmlSchemas” </li></ul>
  6. 6. Sessions <ul><li>Very quick to create session </li></ul><ul><li>Disposable (may hold a DB connection open) </li></ul><ul><li>Web: session per request. (Also, ‘long conversations’ - check out NHibernate Burrow) </li></ul>
  7. 7. Mappings: Primary keys <ul><li>Entities must be identifiable </li></ul><ul><li>Multiple strategies for PK </li></ul><ul><li>Use ‘native’ as best-guess </li></ul><ul><li>Recommended way is “HiLo” to save round-tripping to DB </li></ul>
  8. 8. Mappings: Properties <ul><li>Maps database columns to properties/fields on entity </li></ul><ul><li>Can map columns to nested types </li></ul><ul><li>Control insert/update per column </li></ul><ul><li>Default is to insert/update all columns, but can be dynamic </li></ul>
  9. 9. Demo: The basics <ul><li>Fetching single entity by ID </li></ul><ul><li>Updating </li></ul><ul><li>Inserting </li></ul>
  10. 10. Mappings: Relations <ul><li>Many types. Most common: bag and set </li></ul><ul><li>Assuming bi-directional relations and simple bag collections </li></ul><ul><li>In a bi-directional association, only one end can be the ‘owner’ </li></ul><ul><li>Identify the ‘non-owner’ end of collection by setting Inverse=“true” </li></ul>
  11. 11. Mappings: 1-* Relations <ul><li>Professor (1) – Class (*) </li></ul><ul><li>Bi-directional: <bag> and < many-to-one > </li></ul><ul><li>Declare the many-valued end inverse=&quot;true“ </li></ul><ul><li>Use ‘ not-found =ignore’ for bad, legacy data </li></ul>
  12. 12. Mappings: *-* Relations <ul><li>Student (*) – Class (*) </li></ul><ul><li>Bi-directional: 2 <bag> elements with link table name </li></ul><ul><li>Arbitrarily choose 1 end to set as Inverse=“true” </li></ul><ul><li>Demo </li></ul>
  13. 13. Cascades <ul><li>Don’t have to explicitly call save / update / delete on related entities </li></ul><ul><li>Cascade types: none, save-update, delete, all, all-delete-orphan </li></ul><ul><li>Can define a default cascade </li></ul><ul><li>Q’s? </li></ul>
  14. 14. Proxies <ul><li>Enables lazy loading - Just in time loading of data </li></ul><ul><ul><li>Lazy loaded collections </li></ul></ul><ul><ul><li>Lazy loaded entities (the single-end of a relation) </li></ul></ul><ul><ul><li>Controlled via mappings or code </li></ul></ul><ul><li>Watch for N+1 issue </li></ul>
  15. 15. Querying <ul><li>Lots of options: </li></ul><ul><ul><li>Criteria </li></ul></ul><ul><ul><li>DetachedCriteria </li></ul></ul><ul><ul><li>HQL </li></ul></ul><ul><ul><li>Linq </li></ul></ul><ul><ul><li>SQL </li></ul></ul>
  16. 16. Querying: ICriteria <ul><li>Out-of-the-box method for building up a query in code </li></ul><ul><li>Requires an active session </li></ul><ul><li>Weakly typed </li></ul><ul><li>nhlambdaextensions project adds typesafe lambda expressions </li></ul>
  17. 17. Querying: Projections <ul><li>By default NH selects all mapped properties of entity </li></ul><ul><li>Projections allow control over the ‘select’ part of generated SQL </li></ul>
  18. 18. Querying: Aliases / Subcriteria <ul><li>Alias relations to refer to them later </li></ul><ul><li>Need to use them to reference multi-level relations. E.g. OrderItem.OrderHeader.Customer.Name </li></ul>
  19. 19. Querying: DetachedCriteria <ul><li>Same capabilities as ICriteria </li></ul><ul><li>Does not require active session </li></ul><ul><li>Can be ‘attached’ to any session at any time to execute query </li></ul>
  20. 20. Querying: MultiCriteria <ul><li>Enables multiple criteria to be evaluated in the one round trip to DB </li></ul><ul><li>Very useful for paging </li></ul>
  21. 21. Querying: HQL <ul><li>Similar to SQL </li></ul><ul><li>Allows querying over domain entities </li></ul><ul><li>“ HQL is fully object-oriented, understanding notions like inheritance, polymorphism and associations” </li></ul>
  22. 22. Querying: Linq <ul><li>NHibernate Linq 1.0 released </li></ul><ul><li>Supports just about anything you can do with criteria API </li></ul>
  23. 23. Caching: 1 st level cache <ul><li>Every ISession has an in-built cache called 1 st level cache </li></ul><ul><li>Stores all the loaded entities for current unit of work </li></ul><ul><li>Prevents needless round-tripping to DB </li></ul>
  24. 24. Caching: 2 nd level cache <ul><li>Cache shared between sessions </li></ul><ul><li>Out of the box support for: </li></ul><ul><ul><li>NHibernate.Caches.Prevalence </li></ul></ul><ul><ul><li>NHibernate.Caches.SysCache (ASP.Net cache) </li></ul></ul><ul><ul><li>NHibernate.Caches.SysCache2 (SQL dependency-based expiration) </li></ul></ul><ul><ul><li>NHibernate.Caches.MemCache </li></ul></ul><ul><li>Does all the hard work for you! </li></ul>
  25. 25. Querying: Future Queries <ul><li>Executes a batch of queries in one round trip to DB </li></ul>
  26. 26. Why use it? <ul><li>Reduces repetitive, error prone data access code </li></ul><ul><li>Enables use of OOP </li></ul><ul><li>Very flexible </li></ul><ul><li>Stable, widely used </li></ul><ul><li>Speed up development time </li></ul><ul><li>Single place to add behaviour such as auditing / INotifyPropertyChanged / Filters etc </li></ul><ul><li>Testing: Easier integration tests </li></ul><ul><li>Security: Uses parameterised queries by default </li></ul>
  27. 27. Resources <ul><li>Book: NHibernate In Action (v 1.2) </li></ul><ul><li>NHForge.org </li></ul><ul><ul><li>NHibernate blog </li></ul></ul><ul><li>Nhibernate FAQ blog </li></ul><ul><li>Very active users group (nhusers) </li></ul><ul><li>ayende.com </li></ul><ul><li>Dimecast and “Summer of NHibernate” videos </li></ul>

×