Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created November 24, 2009 17:15
Show Gist options
  • Save jfromaniello/242033 to your computer and use it in GitHub Desktop.
Save jfromaniello/242033 to your computer and use it in GitHub Desktop.
public class Dao<T> : IDao<T>
{
private readonly ISessionFactory _factory;
public Dao(ISessionFactory factory)
{
_factory = factory;
}
protected ISession Session
{
get { return _factory.GetCurrentSession(); }
}
#region IDao<T> Members
public T Get(object id)
{
return Session.Get<T>(id);
}
public T GetProxy(object id)
{
return Session.Load<T>(id);
}
public T MakePersistent(T entity)
{
Session.Save(entity);
return entity;
}
public void Refresh(T entity)
{
var entityAsEntity = entity as Entity;
if (entityAsEntity != null && entityAsEntity.Id == 0)
{
return;
}
object id = Session.GetIdentifier(entity);
Session.Evict(entity);
Session.Load(entity, id);
}
public IEnumerable<T> RetrieveAll()
{
return Session.Linq<T>();
}
public IEnumerable<T> RetrieveAll(params string[] expandProperties)
{
INHibernateQueryable<T> queryable = GetLinq();
foreach (var property in expandProperties)
{
queryable.Expand(property);
}
return queryable;
}
public virtual IEnumerable<T> Retrieve(Expression<Func<T, bool>> predicate)
{
return GetLinq().Where(predicate);
}
public IEnumerable<T> Retrieve(Expression<Func<T, bool>> predicate, params string[] expandProperties)
{
INHibernateQueryable<T> queryable = GetLinq();
foreach (var property in expandProperties)
{
queryable.Expand(property);
}
return queryable.Where(predicate);
}
public int Count(Expression<Func<T, bool>> predicate)
{
return GetLinq().Where(predicate).Count();
}
public void MakeTransient(T entity)
{
Session.Delete(entity);
}
#endregion
private INHibernateQueryable<T> GetLinq()
{
return Session.Linq<T>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment