Created
November 24, 2009 17:15
-
-
Save jfromaniello/242033 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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