|
using System;
using NHibernate;
namespace Hik.PhoneBook.Data.Repositories.Nh
{
/// <summary>
/// Implements Unit of work for NHibernate.
/// </summary>
public class NhUnitOfWork : IUnitOfWork
{
/// <summary>
/// Gets current instance of the NhUnitOfWork.
/// It gets the right instance that is related to current thread.
/// </summary>
public static NhUnitOfWork Current
{
get { return _current; }
set { _current = value; }
}
[ThreadStatic]
private static NhUnitOfWork _current;
/// <summary>
/// Gets Nhibernate session object to perform queries.
/// </summary>
public ISession Session { get; private set; }
/// <summary>
/// Reference to the session factory.
/// </summary>
private readonly ISessionFactory _sessionFactory;
/// <summary>
/// Reference to the currently running transcation.
/// </summary>
private ITransaction _transaction;
/// <summary>
/// Creates a new instance of NhUnitOfWork.
/// </summary>
/// <param name="sessionFactory"></param>
public NhUnitOfWork(ISessionFactory sessionFactory)
{
_sessionFactory = sessionFactory;
}
/// <summary>
/// Opens database connection and begins transaction.
/// </summary>
public void BeginTransaction()
{
Session = _sessionFactory.OpenSession();
_transaction = Session.BeginTransaction();
}
/// <summary>
/// Commits transaction and closes database connection.
/// </summary>
public void Commit()
{
try
{
_transaction.Commit();
}
finally
{
Session.Close();
}
}
/// <summary>
/// Rollbacks transaction and closes database connection.
/// </summary>
public void Rollback()
{
try
{
_transaction.Rollback();
}
finally
{
Session.Close();
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have started programming at 14 years old using Pascal as hobby. Then I interested in web development (HTML, JavaScript, ASP...) before university.
I graduated from Sakarya University Computer Engineering. At university, I learned C++, Visual Basic.NET, C#, ASP.NET and Java. I partly implemented ARP, IP and TCP protocols in Java as my final term project.
Now, I am working on Windows and web based software development mostly using Microsoft technologies in my own company.
My open source projects:
* ABP Framework:
https://abp.io
* jTable:
http://jtable.org
* Others:
https://github.com/hikalkan
My personal web site:
https://halilibrahimkalkan.com