137 lines
3.4 KiB
C#
137 lines
3.4 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq.Expressions;
|
|||
|
using NHibernate;
|
|||
|
|
|||
|
namespace Tanshu.Accounts.Repository
|
|||
|
{
|
|||
|
public class FluentBase<T> : IDisposable, IRepository<T> where T : class
|
|||
|
{
|
|||
|
protected readonly bool DisposeSession;
|
|||
|
protected readonly ISession Session;
|
|||
|
protected readonly ITransaction Transaction;
|
|||
|
|
|||
|
public FluentBase()
|
|||
|
{
|
|||
|
Session = SessionManager.Session;
|
|||
|
DisposeSession = true;
|
|||
|
Transaction = Session.BeginTransaction();
|
|||
|
|
|||
|
}
|
|||
|
public FluentBase(bool beginTransaction)
|
|||
|
{
|
|||
|
Session = SessionManager.Session;
|
|||
|
DisposeSession = true;
|
|||
|
if (beginTransaction)
|
|||
|
Transaction = Session.BeginTransaction();
|
|||
|
}
|
|||
|
public FluentBase(ISession session)
|
|||
|
{
|
|||
|
this.Session = session;
|
|||
|
DisposeSession = false;
|
|||
|
}
|
|||
|
public FluentBase(ISession session, bool beginTransaction)
|
|||
|
{
|
|||
|
this.Session = session;
|
|||
|
DisposeSession = false;
|
|||
|
if (beginTransaction)
|
|||
|
Transaction = Session.BeginTransaction();
|
|||
|
}
|
|||
|
|
|||
|
#region IDisposable Members
|
|||
|
public void Dispose()
|
|||
|
{
|
|||
|
if (Transaction != null)
|
|||
|
{
|
|||
|
Transaction.Commit();
|
|||
|
Transaction.Dispose();
|
|||
|
}
|
|||
|
if (DisposeSession && Session != null)
|
|||
|
Session.Dispose();
|
|||
|
}
|
|||
|
//public void Dispose()
|
|||
|
//{
|
|||
|
// Dispose(true);
|
|||
|
// GC.SuppressFinalize(this);
|
|||
|
//}
|
|||
|
|
|||
|
//protected virtual void Dispose(bool disposing)
|
|||
|
//{
|
|||
|
// if (Transaction != null)
|
|||
|
// {
|
|||
|
// Transaction.Commit();
|
|||
|
// Transaction.Dispose();
|
|||
|
// }
|
|||
|
// if (DisposeSession && Session != null)
|
|||
|
// Session.Dispose();
|
|||
|
|
|||
|
// if (disposing)
|
|||
|
// {
|
|||
|
// // get rid of managed resources
|
|||
|
// }
|
|||
|
// // get rid of unmanaged resources
|
|||
|
//}
|
|||
|
//~FluentBase()
|
|||
|
//{
|
|||
|
// Dispose(false);
|
|||
|
//}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region IRepository<T> Members
|
|||
|
|
|||
|
public void Insert(T item)
|
|||
|
{
|
|||
|
Session.Save(item);
|
|||
|
}
|
|||
|
|
|||
|
public void Update(T item)
|
|||
|
{
|
|||
|
Session.Update(item);
|
|||
|
}
|
|||
|
|
|||
|
public void Delete(T item)
|
|||
|
{
|
|||
|
Session.Delete(item);
|
|||
|
}
|
|||
|
|
|||
|
public void Delete(Expression<Func<T, bool>> query)
|
|||
|
{
|
|||
|
Delete(Get(query));
|
|||
|
}
|
|||
|
|
|||
|
public void DeleteList(Expression<Func<T, bool>> query)
|
|||
|
{
|
|||
|
foreach (T item in List(query))
|
|||
|
Delete(item);
|
|||
|
}
|
|||
|
|
|||
|
public T Get(Expression<Func<T, bool>> query)
|
|||
|
{
|
|||
|
return Session.QueryOver<T>()
|
|||
|
.Where(query)
|
|||
|
.SingleOrDefault();
|
|||
|
}
|
|||
|
|
|||
|
public IList<T> List()
|
|||
|
{
|
|||
|
return Session.CreateCriteria<T>()
|
|||
|
.List<T>();
|
|||
|
}
|
|||
|
|
|||
|
public IList<T> List(Expression<Func<T, bool>> query)
|
|||
|
{
|
|||
|
return Session.QueryOver<T>()
|
|||
|
.Where(query)
|
|||
|
.List();
|
|||
|
}
|
|||
|
|
|||
|
public void DeleteList(IList<T> list)
|
|||
|
{
|
|||
|
foreach (T item in list)
|
|||
|
Delete(item);
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|