using
NHibernate.Cfg;
using
NHibernate;
using
System;
using
System.Reflection;
using
System.Collections;
namespace
nhiber
...
{
/**//// <summary>
/// 持久化类
/// </summary>
public class BizObject 
...{
public BizObject() ...{ }
public BizObject( object existingId ) 
...{
ObjectBroker.Load( this, existingId );
}
public virtual void Create() 
...{
ObjectBroker.Create( this );
}
public virtual void Update() 
...{
ObjectBroker.Update( this );
}
public virtual void Delete() 
...{
ObjectBroker.Delete( this );
}
public virtual IList List()
...{
return ObjectBroker.Lists(this);
}
}
public class ObjectBroker 
...{
private ObjectBroker() ...{ }
public static void Load( object obj, object id )
...{
ISession s = Sessions.GetSession();
try 
...{
s.Load( obj, id );
}
finally 
...{
s.Close();
}
}
public static void Create( object obj ) 
...{
ISession s = Sessions.GetSession();
ITransaction trans = null;
try 
...{
trans = s.BeginTransaction();
s.Save( obj );
trans.Commit();
}
finally 
...{
s.Close();
}
}
public static void Update( object obj ) 
...{
ISession s = Sessions.GetSession();
ITransaction trans = null;
try 
...{
trans = s.BeginTransaction();
s.Update( obj );
trans.Commit();
}
finally 
...{
s.Close();
}
}
public static void Delete( object obj ) 
...{
ISession s = Sessions.GetSession();
ITransaction trans = null;
try 
...{
trans = s.BeginTransaction();
s.Delete( obj );
trans.Commit();
}
finally 
...{
s.Close();
}
}
public static IList Lists(object obj)
...{
IList lst;
ISession session = Sessions.GetSession();
ITransaction transaction = session.BeginTransaction();
ICriteria crit=session.CreateCriteria(obj.GetType());
lst=crit.List();
transaction.Commit();
session.Close();
return lst;
}
}
public class Sessions
...{
private static readonly object lockObj = new object();
private static ISessionFactory m_factory;
public Sessions()
...{
}
public static ISessionFactory Factory 
...{

get 
...{
if ( m_factory == null ) 
...{
lock ( lockObj ) 
...{
if ( m_factory == null ) 
...{
Configuration cfg = new Configuration();
cfg.AddAssembly( Assembly.GetExecutingAssembly() );
m_factory = cfg.BuildSessionFactory();
}
}
}
return m_factory;
}
}
public static ISession GetSession() 
...{
return Factory.OpenSession();
}
}
}