Friday, October 08, 2010

NHibernate


What is NHibernate?

NHibernate is an ORM (Object Relational Mapping) system. Providing an xml file which maps table schema to the class members, NHibernate is capable of managing a persistent data objects for respective mapped classes. As we already realised there is a gap between object oriented entity modal and relational data modal. NHibernate fills this gap providing HQL (Hibernate Query Language) to query objects rather than relational queries.

How it works?

Key object is ISession. ISession holds a persistent pool of data objects. ISessionFactory is the origin of ISession. ISessionFactory uses XML mappings (as a first level cache) and connection provider to create ISession. Connection provider uses IDriver to create connections. IDriver uses NHibernate configuration (XML file containing database query string and NHibernate settings) to create client specific connection string and manage optimistic behaviour based on the client.



  1. ISessionFactory:
    1. Factory for Session
    2. Client of IConnection Provider
    3. Has immutable cache compiled mappings (level1) derived from XML mappings
    4. Optional second level cache of data which might be useful between transactions.
  2. ISession:
    1. Short lived objects between application and data store.
    2. Factory for transaction.
    3. First level cache of persistent objects
    4. Used in object graphs and navigating
    5. Can expand across multiple transactions
    6. In the session all the persistent objects get associated with a CLR identity in the object map which is equivalent to the persistent identity. 
  3. ITransaction:
    1. A short lived object that perform unit of work
    2. In meaning of it terms data update/Inset or delete
    3. While transaction is on progress all the underline relations are read-only
    4. Get generated from ISession
    5. ISession might span several ITransactions
  4. ITransactionFactory:
    1. Not exposed to the application but can be extended by the developer
    2. IConnectionProvider:
    3. Factory for ADO.NET connections/commands
    4. Abstracts application from vender specific implementation of IDbConnection/IDbCommand.
      Can be extended by developer
  5. IDriver:
    1. Abstracts capabilities of different Ado.NET providers
    2. Handles parameter naming / type conversions and other supported Ado.NET features.
Configuration

NHibernate settings and appropriate connection string to the data store. Includes driver classes, dialect

Example:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string_name">SqlServer</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

Mappings

In a formal notation, mappings are the relationship between relation structure and class structure. In simple terms mappings are the relationship between class name and table name plus table columns and class properties.

Example:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"                    
                   assembly="ActiveTest"                    
                   namespace="ActiveTest">
  <class name="Product">
    <id name="Id" />
    <property name="Name" />
    <property name="Description" />    
  </class>
</hibernate-mapping>

Table:


Objects and collection states:
  1. Transient: - The transient has not and never been associated with a NHibernate session (persistent context). It has no identity (primary key value)
  2. Persistent: - Data objects that are currently associated with NHibernate session (persistent context) called as persistent objects. They are associated with primary key value. Object may represent a row of a relation. Single primary key value can represent one and only one object in the persistent context. For a persistent object NHibernate guarantees that the persistent identity is equivalent to the CLR identity.
  3. Detached: - Objects been associated with persistent context but now the context is closed. For detached objects NHibernate makes no guarantees about the association between persistent context identity and the CLR identity.
References:
  1. NHibernate Architecture
  2. NHibernate Architecture

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...