- infocenter.zip
- InfoCenter
- ConfigSamples
- InfoCenter.Agent
- InfoCenter.Logic.Tests
- InfoCenter.Logic
- Agent
- Entities
- Helpers
- bin
- Debug
- Antlr3.Runtime.dll
- hibernate.cfg.xml
- Iesi.Collections.dll
- InfoCenter.Logic.dll
- InfoCenter.Logic.dll.mdb
- InfoCenter.Logic.pdb
- InfoCenter.Persistence.dll
- InfoCenter.Persistence.dll.mdb
- InfoCenter.Persistence.pdb
- log4net.dll
- Release
- InfoCenter.Logic.csproj
- InfoCenter.Logic.pidb
- obj
- Debug
- PacketParser.cs
- Properties
- InfoCenter.Persistence.Test
- InfoCenter.Persistence
- InfoCenter.sln
- InfoCenter.userprefs
- InfoCenter.WebConsole
- SharedLibs
- Antlr3.Runtime.dll
- Castle.Core.dll
- Castle.DynamicProxy2.dll
- Iesi.Collections.dll
- log4net.dll
- MySql.Data.dll
- NHibernate.ByteCode.Castle.dll
- NHibernate.dll
- nunit.framework.dll
- test-results
|
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
//using NHibernate.Linq;
using NHibernate.Criterion;
namespace InfoCenter.Persistence.Core
{
public class Repository<T> : IRepository<T>
{
private ISessionFactory _sessionFactory;
private ISession Session
{
get
{
return _sessionFactory.OpenSession();
}
}
public Repository(ISessionFactory sessionFactory)
{
_sessionFactory = sessionFactory;
}
/*public IQueryable<T> FindAll(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
return from item in Session.Linq<T>().Where(predicate)
select item;
}
public IQueryable<T> FindAll(System.Linq.Expressions.Expression<Func<T, bool>> predicate, int pageIndex, int pageSize)
{
return (from item in Session.Linq<T>().Where(predicate)
select item).Skip<T>((pageIndex-1)*pageSize).Take<T>(pageSize);
}*/
/// <summary>
/// Add employee
/// </summary>
/// <param name="employee"></param>
public void Add(T entity)
{
using (ISession session = _sessionFactory.OpenSession())
using (session.BeginTransaction())
{
session.Save(entity);
session.Transaction.Commit();
}
}
/// <summary>
/// Update employee
/// </summary>
/// <param name="employee"></param>
public void Update(T entity)
{
using (ISession session = _sessionFactory.OpenSession())
using (session.BeginTransaction())
{
session.SaveOrUpdate(entity);
session.Transaction.Commit();
}
}
public void Remove(T entity)
{
using (ISession session = _sessionFactory.OpenSession())
using (session.BeginTransaction())
{
session.Delete(entity);
session.Transaction.Commit();
}
}
/* public void Remove(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
foreach (T entity in Session.Linq<T>().Where(predicate).ToList<T>())
Remove(entity);
}*/
/// <summary>
/// Deletes every entity that matches the given criteria
/// </summary>
/// <param name="criteria"></param>
public void Remove(DetachedCriteria criteria)
{
foreach (T entity in FindAll(criteria))
Remove(entity);
}
public ICollection<T> FindAll(DetachedCriteria criteria)
{
return criteria.GetExecutableCriteria(Session).List<T>();
}
/// <summary>
/// Returns each entity that maches the given criteria, and orders the results
/// according to the given Orders
/// </summary>
/// <param name="criteria"></param>
/// <param name="orders"></param>
/// <returns></returns>
public ICollection<T> FindAll(DetachedCriteria criteria, params Order[] orders)
{
if (orders != null)
foreach (Order order in orders)
criteria.AddOrder(order);
return FindAll(criteria);
}
/// <summary>
/// Returns each entity that matches the given criteria, with support for paging,
/// and orders the results according to the given Orders
/// </summary>
/// <param name="criteria"></param>
/// <param name="firstResult"></param>
/// <param name="numberOfResults"></param>
/// <param name="orders"></param>
/// <returns></returns>
public ICollection<T> FindAll(DetachedCriteria criteria, int firstResult, int numberOfResults, params Order[] orders)
{
criteria.SetFirstResult(firstResult).SetMaxResults(numberOfResults);
return FindAll(criteria, orders);
}
/// <summary>
/// Returns the one entity that matches the given criteria. Throws an exception if
/// more than one entity matches the criteria
/// </summary>
/// <param name="criteria"></param>
/// <returns></returns>
public T FindOne(DetachedCriteria criteria)
{
return criteria.GetExecutableCriteria(Session).UniqueResult<T>();
}
/*public T FindOne(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
return Session.Linq<T>().Where(predicate).FirstOrDefault<T>();//criteria.GetExecutableCriteria(Session).UniqueResult<T>();
}*/
/// <summary>
/// Returns the first entity to match the given criteria
/// </summary>
/// <param name="criteria"></param>
/// <returns></returns>
public T FindFirst(DetachedCriteria criteria)
{
IList<T> results = criteria.SetFirstResult(0)
.SetMaxResults(1)
.GetExecutableCriteria(Session).List<T>();
if (results.Count > 0)
return results[0];
return default(T);
}
/// <summary>
/// Returns the first entity to match the given criteria, ordered by the given order
/// </summary>
/// <param name="criteria"></param>
/// <param name="order"></param>
/// <returns></returns>
public T FindFirst(DetachedCriteria criteria, Order order)
{
return FindFirst(criteria.AddOrder(order));
}
/// <summary>
/// Returns the total number of entities that match the given criteria
/// </summary>
/// <param name="criteria"></param>
/// <returns></returns>
public long Count(DetachedCriteria criteria)
{
return Convert.ToInt64(criteria
.GetExecutableCriteria(Session)
.SetProjection(Projections.RowCountInt64()).UniqueResult());
}
/// <summary>
/// Returns the total number of entities that match the given expression
/// </summary>
/// <param name="criteria"></param>
/// <returns></returns>
/* public long Count(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
return Convert.ToInt64(Session.Linq<T>().Count(predicate));
}*/
/// <summary>
/// Returns true if at least one entity exists that matches the given criteria
/// </summary>
/// <param name="criteria"></param>
/// <returns></returns>
public bool Exists(DetachedCriteria criteria)
{
return Count(criteria) > 0;
}
/*public bool Exists(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
return Count(predicate) > 0;
}*/
}
}
|
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.
Interested in design/development of framework functionality using the best patterns and practices.