Skip to content

Instantly share code, notes, and snippets.

@Nosfheratu
Last active December 10, 2015 12:38
Show Gist options
  • Save Nosfheratu/4435428 to your computer and use it in GitHub Desktop.
Save Nosfheratu/4435428 to your computer and use it in GitHub Desktop.
Generic repository pattern implementation for NHibernate
public interface IRepository<T>
{
IEnumerable<T> GetAll();
T GetByID(int id);
T GetByID(Guid key);
void Save(T entity);
void Delete(T entity);
}
public class Repository<T> : IRepository<T>
{
protected readonly ISession Session;
public Repository(ISession session)
{
Session = session;
}
public IEnumerable<T> GetAll()
{
return Session.Query<T>();
}
public T GetByID(int id)
{
return Session.Get<T>(id);
}
public T GetByID(Guid key)
{
return Session.Get<T>(key);
}
public void Save(T entity)
{
Session.Save(entity);
Session.Flush();
}
public void Delete(T entity)
{
Session.Delete(entity);
Session.Flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment