59909a5ee7
Must use the Repositories with Using or else bad things will happen.
141 lines
3.5 KiB
C#
141 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq.Expressions;
|
|
using NHibernate;
|
|
|
|
namespace Tanshu.Accounts.Repository
|
|
{
|
|
public class FluentGenericBase<T> : IDisposable, IRepository<T> where T : class
|
|
{
|
|
protected readonly bool DisposeSession;
|
|
protected readonly ISession Session;
|
|
protected readonly ITransaction Transaction;
|
|
|
|
public FluentGenericBase()
|
|
{
|
|
Session = SessionManager.Session;
|
|
DisposeSession = true;
|
|
Transaction = Session.BeginTransaction();
|
|
|
|
}
|
|
public FluentGenericBase(bool beginTransaction)
|
|
{
|
|
Session = SessionManager.Session;
|
|
DisposeSession = true;
|
|
if (beginTransaction)
|
|
Transaction = Session.BeginTransaction();
|
|
}
|
|
public FluentGenericBase(ISession session)
|
|
{
|
|
this.Session = session;
|
|
DisposeSession = false;
|
|
}
|
|
public FluentGenericBase(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
|
|
//}
|
|
//~FluentGenericBase()
|
|
//{
|
|
// 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);
|
|
}
|
|
|
|
public IQueryOver<T, T> Query()
|
|
{
|
|
return Session.QueryOver<T>();
|
|
}
|
|
#endregion
|
|
}
|
|
}
|