The Basics Of NHibernate Object Relational Mapper For 'Net PowerPoint PPT Presentation

presentation player overlay
1 / 15
About This Presentation
Transcript and Presenter's Notes

Title: The Basics Of NHibernate Object Relational Mapper For 'Net


1
The Basics Of NHibernateObject Relational Mapper
For .Net
  • Oren Eini
  • Orene_at_we-can.co.il

2
What Is Object Relational Mapping?
3
NHibernate Overview
  • NHibernate is a mature ORM with its roots in
    Javas Hibernate.
  • Uses XML to map database structure to object
    structure.
  • Very flexible and extensible.
  • Active Community and Projects around it.
  • Developed Supported by JBoss (www.jboss.org)

4
The Unit Of Work Pattern
  • "Maintains a list of objects affected by a
    business transaction and coordinates the writing
    out of changes and the resolution of concurrency
    problems." Martin Fowler,http//www.shrinkster.
    com/eha
  • Manages a set of changes to business objects. All
    objects that were changed during the Unit Of Work
    are saved when the Unit Of Work is concluded.
  • NHibernate works out of the box using Unit Of
    Work. An NHibernate Session has 11 match to a
    Unit Of Work.

5
Key Concepts
  • Entities
  • Value Types
  • The Session Factory
  • The Session
  • Mapping (hbm.xml)
  • Advanced Topics
  • Components
  • User Types

6
The Session Factory
  • Contains all the information needed to talk to a
    database
  • Connection String
  • Object Mapping
  • Database dialect
  • Thread Safe
  • Highly Concurrent
  • Expensive to create
  • Exception Safe (doesnt mean that it wont
    throw!)

7
The Session
  • Main point to interact with the database
  • Responsible for objects persistence and loading.
  • Not thread safe
  • Not exception safe
  • Cheap to create

8
Common Usages Patterns
  • Session Per Request
  • NHibernate Http Module
  • Session open on Request_Start
  • Session disposed on Request_End
  • Session Per Conversation
  • Single Session (often disconnected)
  • Multiply interactions with the user
  • All changes are persisted in the end of the
    conversation.

9
Getting To Know The Session
  • Find()
  • Load()
  • Get()
  • Save()
  • Update()
  • BeginTransaction()
  • Flush()

10
Xml Mapping
  • lt?xml version'1.0' encoding'utf-16'?gt
  • lthibernate-mapping
  • xmlnsxsi'http//www.w3.org/2001/XMLSchema-inst
    ance'
  • xmlnsxsd'http//www.w3.org/2001/XMLSchema'
    xmlns'urnnhibernate-mapping-2.0'gt
  • ltclass name'NHibernate.Demos.ToDoItem,
    NHibernate.Demos'
  • table'ToDo'gt
  • ltid name'Id'gt
  • ltgenerator class'identity' /gt
  • lt/idgt
  • ltproperty name'DueDate' /gt
  • ltproperty name'Title' /gt
  • ltproperty name'Description' /gt
  • ltproperty name'PrecentageDone' /gt
  • lt/classgt
  • lt/hibernate-mappinggt

11
Configuration
  • lt?xml version"1.0" encoding"utf-8" ?gt
  • lthibernate-configuration xmlns"urnnhibernate-con
    figuration-2.0" gt
  • ltsession-factorygt
  • ltproperty name"connection.provider"gt
    NHibernate.Connection.DriverConnectionProviderlt/pr
    opertygt
  • ltproperty name"connection.driver_class"gt
    NHibernate.Driver.SqlClientDriverlt/propertygt
  • ltproperty name"connection.connection_string"gt
    Data SourcelocalhostInitial
    CatalogTestIntegrated SecurityTruelt/propertygt
  • ltproperty name"dialect"gt NHibernate.Dialect.M
    sSql2000Dialectlt/propertygt
  • ltmapping assembly"NHibernate.Demos"/gt
  • lt/session-factorygt
  • lt/hibernate-configurationgt

12
Logging
  • NHibernate offers a wealth of information that
    can be turned on / off at runtime using log4nets
    capabilities.
  • ltfilter type"log4net.Filter.LoggerMatchFilter"gt
  • ltloggerToMatch value"NHibernate" /gt
  • ltacceptOnMatch value"true" /gt
  • lt/filtergt
  • ltfilter type"log4net.Filter.DenyAllFilter" /gt

13
What it is not meant for
  • Bulk data manipulations Use SPs or SqlBulkCopy
    for this.
  • Free developers from understanding the database /
    NHibernate.
  • Let you forget the profiler.

14
Resources
  • Forumshttp//shrinkster.com/egu
  • Mailing Listshttp//shrinkster.com/egv
  • Hibernate In Action (highly recommended)http//sh
    rinkster.com/egw 
  • How to ask questionsPost with relevant code and
    mapping, include what you expected to happen and
    what happened. If an exception occurred, include
    the exception message and full stack.

15
Questions Comments
Orene_at_we-can.co.il
Write a Comment
User Comments (0)
About PowerShow.com