|
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.EnterpriseServices;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
namespace ComPlusNHibernateTest
{
/// <summary>
/// Controls the use of <see cref="Table1"/> and <see cref="Table2"/>.
/// </summary>
[Transaction(TransactionOption.Supported)]
public class OrmManager : ServicedComponent
{
private ISessionFactory _SessionFactory;
/// <summary>
/// Gets the session factory for this assembly.
/// </summary>
private ISessionFactory SessionFactory
{
get
{
if (_SessionFactory == null)
{
Configuration cfg = new Configuration();
cfg.Properties = GetConfigProps();
cfg.AddAssembly(this.GetType().Assembly);
_SessionFactory = cfg.BuildSessionFactory();
}
return _SessionFactory;
}
}
public OrmManager()
{
}
private Hashtable GetConfigProps()
{
Hashtable props = new Hashtable();
props.Add("hibernate.connection.provider", "NHibernate.Connection.DriverConnectionProvider");
props.Add("hibernate.dialect", "NHibernate.Dialect.MsSql2000Dialect");
props.Add("hibernate.connection.driver_class", "NHibernate.Driver.SqlClientDriver");
props.Add("hibernate.connection.connection_string", ConfigurationSettings.AppSettings["ConnectionString"]);
return props;
}
/// <summary>
/// This method uses reflection to enlist in the distributed transaction.
/// </summary>
/// <param name="conn"></param>
private static void EnlistIfPossible(System.Data.IDbConnection conn)
{
if (ContextUtil.IsInTransaction)
{
MethodInfo mi = conn.GetType().GetMethod("EnlistDistributedTransaction", BindingFlags.Public | BindingFlags.Instance);
if (mi != null)
{
mi.Invoke(conn, new object[] { (System.EnterpriseServices.ITransaction)ContextUtil.Transaction });
}
}
}
public void Save(object obj)
{
ISession session = SessionFactory.OpenSession();
try
{
EnlistIfPossible(session.Connection);
session.SaveOrUpdate(obj);
session.Flush();
if (ContextUtil.IsInTransaction)
ContextUtil.MyTransactionVote = TransactionVote.Commit;
}
catch
{
if (ContextUtil.IsInTransaction)
ContextUtil.MyTransactionVote = TransactionVote.Abort;
throw;
}
finally
{
if (session != null && session.IsOpen)
session.Close();
}
}
public void Delete(object obj)
{
ISession session = SessionFactory.OpenSession();
try
{
EnlistIfPossible(session.Connection);
session.Delete(obj);
session.Flush();
if (ContextUtil.IsInTransaction)
ContextUtil.MyTransactionVote = TransactionVote.Commit;
}
catch
{
if (ContextUtil.IsInTransaction)
ContextUtil.MyTransactionVote = TransactionVote.Abort;
throw;
}
finally
{
if (session != null && session.IsOpen)
session.Close();
}
}
public IList GetAll(Type t)
{
ISession session = SessionFactory.OpenSession();
try
{
IList lst = session.CreateCriteria(t).List();
return lst;
}
finally
{
if (session != null && session.IsOpen)
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.