Monday, September 27, 2010

nHibernate fundamentals

1) nHibernate it is a OR/M tool. Hibernate is very popular for Java programmers. Same thing is now available to .NET developers too.

2) ISession class is a unit of work (transactions)

3) ISession is like a ADO.NET dataset

4) session.Add()
Session.Delete()
Session.Flush()

5) ISessionFactory to build a session class.

6) Configuration Class. IsessionFactory uses configuration class to build it.
ISessionFactory sessionFactory = new
Configuration().Configure().buildSessionFactory()

7) In summary.
Configuration Class -> Session Factory -> Session

8) there are two important xsd files exist.
nHibernate-configuration.xsd
nHibernate-mapping.xsd

9) copy the above two files into the below folder inorder to get the intellisense in the web.config file.
c:\program files\microsoft visual studio 9.0\xml\schemas

10) create a DTO library project and create DTO classes for each table

11) create a mapping file. ex:- Customer.hbm.xml this convension is optional. but recommended

12)











Note: 1) column="xyz" is optional.
2) .hbm.xml file has a property.
"Build action" - embedded resources
"copy to output directory"
13)
creat a data access layer project and reference to the following dll's
1) nHibernate.dll
2) log4net.dll
3) isi.collections.dll
4) castle.dynamicProxy.dll

14)

public DataTransfer.Customer GetCustomerById(int customerid)
{
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
config.Configure();
NHibernate.ISessionFactory sessionFactory = config.BuildSessionFactory();
NHibernate.ISession session = sessionFactory.OpenSession();
return (DataTransfer.Customer)session.Get(typeof(DataTransfer.Customer), customerId);
}

15)

return session.Get(customerId);

16)

public DataTransfer.Customer GetCustomerById(int customerid)
{
ISessionFactory sessionFactory = (new Configuration()).configure().BuildSessionFactory();
ISession session = sessionFactory.OpenSession();
return session.Get(customerId);
}

17)

creat a test project. and add a configuration file.
filename is hibernate.cfg.xml



nHibernate.connection.DriverconnectionProvider


false
nHibernate.Dialect.MsSql2005Dialect





18)
SQL statement is shown.
parameterized sql is a protection against SQL injection problem.
SQL injection problem is taken care.
execution plan is maintained due to parameterized sql.
speedy response. SQL Server execution caching techniques.

19) Query choices

1) HQL - hibernate query language
2) Criteria API
3) QBE
4) T-SQL / PL-SQL

20) Object oriented SQL. instead of referring to tables, columns we refer to objects, properties.

select c.Firstname from Customer c.

21)

public IList GetCustomersByFirstname(string firstname)
{
ISession session = GetSession();
return session.CreateQuery("select from Customer c where c.Firstname='" + firstname + "'").List();

return session.CreateQuery("select from customer c where c.Firstname = :fn")
.SetString("fn",firstname)
.List();
}

22)

public IList GetCustomersByFirstnameLastname(string firstname, string lastname)
{
ISession session = GetSession();

return session.CreateQuery("select from customer c where c.Firstname = :fn and c.Lastname = :ln")
.SetString("fn",firstname)
.SetString("ln",lastname)
.List();
}

23)

public IList GetCustomersWithIdGreaterThan(int CustomerId)
{
ISession session = GetSession();

return session.CreateQuery("select from customer c where c.CustomerId > :cid")
.SetInt32("cid",customerid)
.List();
}


24)

public IList CRIT_GetCustomersByFirstname(string firstname)
{
ISession session = GetSession();

return session.CreateCriteria(typeof(Customer))
.Add(new NHibernate.Expression.EqExpression("Firstname",firstname))
.List();
}


25)

public IList CRIT_GetCustomersByFirstnameLastname(string firstname, string lastname)
{
ISession session = GetSession();

return session.CreateCriteria(typeof(Customer))
.Add(new NHibernate.Expression.EqExpression("Firstname",firstname))
.Add(new NHibernate.Expression.EqExpression("Lastname",lastname))
.List();
}

26)

public IList CRIT_GetCustomersWithIdGreaterThan(int CustomerId)
{
ISession session = GetSession();

return session.CreateCriteria(typeof(Customer))
.Add(new NHibernate.Expression.GtExpression("CustomerId",customerid))
.List();
}

No comments:

Post a Comment