NHibernate - PowerPoint PPT Presentation

About This Presentation
Title:

NHibernate

Description:

Intorduction to NHibernate – PowerPoint PPT presentation

Number of Views:730
Slides: 22
Provided by: mehrline

less

Transcript and Presenter's Notes

Title: NHibernate


1
nhibernate
  • Meysam Roostaee
  • Mohammad Ali Bahrami

2
Agenda
  • Introduction to ORM
  • Introduction to Nhibernate
  • Nhibernate in Action

3
Introduction to ORM
  • What is ORM?
  • ORM Benefits
  • ORM and Architacture

4
What is ORM?
  • 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

5
Orm benefits
  • Productivity
  • Eliminates lots of repetitive code focus on
    business logic
  • Maintainability
  • Fewer lines of code easier to understand
  • Easier to manage change in the object model
  • Performance
  • Lazy loading associations are fetched when
    needed
  • Caching
  • Database vendor independence
  • The underlying database is abstracted away
  • Can be configured outside the application

6
ORM and Architecture
Call data access layer
MySQl, Oracle, SQL Server, etc.
7
Introduction to Nhibernate
  • Key features
  • High level architecture
  • Very important concept
  • Access persistent object
  • Nhibernate in a nutshell
  • Hibernate-config.xml

8
Key features
  • NHibernate is a port of Hibernate Core for Java
    to the .NET Framework
  • Natural programming model
  • NHibernate supports natural OO idiom
    inheritance, polymorphism, specialization and the
    .NET collections framework
  • Support for fine-grained object models
  • A rich variety of mappings for collections and
    dependent objects

9
Key features
  • The query options
  • NHibernate addresses both sides of the problem
    not only how to get objects into the database,
    but also how to get them out again.
  • Query API with HQL (Hibernate query language)
  • HQL is fully object-oriented, understanding
    notions like inheritence, polymorphism and
    joins.
  • example
  • select cat.Mate from Eg.Cat as cat
  • -The query will select Mates of other Cats.
    Actually, you may express this query by join
  • select mate from Eg.Cat as cat inner join
    cat.Mate as mate

10
Key features
  • Native SQL
  • Execution of native SQL queries is
    controlled via ISQLQuery  interface,

    which is obtained by calling
    ISession.CreateSQLQuery().
  • example 1 Scalar queries
  • sess.CreateSQLQuery("SELECT FROM
    CATS") .AddScalar("ID", NHibernateUtil.Int64)
    .AddScalar("NAME", NHibernateUtil.String) .AddSc
    alar("BIRTHDATE", NHibernateUtil.Date)
  • the columns and types to return.
  • example 2 Entity queries
  • Query query sess.CreateSQLQuery("SELECT ID,
    NAME, BIRTHDATE FROM
    CATS ").AddEntity(typeof(Cat))
  • List result query.list()
  • the entity returned by the query.
  • Assuming that Cat is mapped as a class with the
    columns ID, NAME and BIRTHDATE the above queries
    will both return an IList where each element is
    a Cat entity.

11
Key features
  • Custom SQL
  • Include stored procedures are supported on
    Microsoft SQL Server
  • All popular databases supported
  • Oracle, SQL Server, DB2, SQLite, PostgreSQL,
    MySQL, Sybase, etc.
  • XML-based configuration files
  • Good community support (http//www.nhforge.org)
  • Free/open source
  • NHibernate is licensed under the LGPL (Lesser GNU
    Public License)

12
High level architecture
13
Very important concept
14
Access persistent object
  • Configuration
  • Used to configure Nhibernate
  • Specify the location of mapping documents
  • ISessionFactory
  • The application obtains ISession instances from
    an ISessionFactory
  • One per database (or application)
  • The SessionFactory caches generated SQL
    statements and other mapping metadata that
    NHibernate uses at runtime

15
Access persistent object
  • ISession
  • The primary interface used by NHibernate
    applications
  • Finding, saving,retrieving,updating, and deleting
    objects
  • Call the ISession a persistence manager
  • Itransaction
  • Encapsulates database transactions (abstracts
    application code from the underlying transaction
    implementation)
  • All communication with a database has to occur
    inside a transaction Should end either with a
    commit or a rollback
  • IQUERY
  • Gives you powerful ways to perform queries
    against the database while also controlling how
    the query is executed.
  • Basic interface used for fetching data using
    NHibernate.
  • Queries are written in HQL or in your databases
    native SQL dialect

16
Nhibernate in a nutshell
  • Configuration Class builds Session Factory
  • Session Factory builds Session
  • Session is used as Unit Of Work to interaction
    with objects
  • --------------------------------------------------
    --------------------------------
  • Configuration config new Configuration()
    .Configure()
  • var sessionFactorynew config.BuildSessionFactory
    ()
  • using (var sessionsessionFactory.openSession())
  • var customernew Customer()
  • session.SaveOrUpdate(customer)
  • session.Flush()

17
Hibernate-config.xml
18
(No Transcript)
19
To be continued
20
(No Transcript)
21
References
  • http//www.nhforge.org/doc/nh/en/index.html
  • http//fluentnhibernate.org
  • http//en.wikipedia.org/wiki/Nhibernate
  • http//www.dotnetspider.com
  • http//www.summerofnhibernate.com/
  • http//en.wikipedia.org/wiki/Object-Relational_Map
    ping
  • NHibernate in Action --PIERRE HENRI KUATÉ, TOBIN
    HARRIS.
  • NHibernate explained by example -- Dragos Nuta,
    2005 .
Write a Comment
User Comments (0)
About PowerShow.com