Last active
December 10, 2015 12:38
-
-
Save Nosfheratu/4435428 to your computer and use it in GitHub Desktop.
Generic repository pattern implementation for NHibernate
This file contains 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 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