1: public class RepositoryBase<TEntity> : ICRUDRepository<TEntity>
2: where TEntity : EntityBase, new() {
3:
4: protected readonly ISessionBuilder _sessionBuilder;
5:
6: public RepositoryBase(ISessionBuilder sessionFactory) {
7: _sessionBuilder = sessionFactory;
8: }
9:
10: #region ICRUDRepository<TEntity> Members
11:
12: public void Create(TEntity entity) {
13: ISession session = GetSession();
14: using (ITransaction transaction = session.BeginTransaction()) {
15: session.Save(entity);
16:
17: transaction.Commit();
18: }
19: }
20:
21: public TEntity Retrieve(Guid entityId) {
22: ISession session = GetSession();
23: ICriteria criteria = session.CreateCriteria(typeof(TEntity));
24: criteria.Add(Expression.Eq("Id", entityId));
25:
26: return criteria.UniqueResult<TEntity>();
27: }
28:
29: public IList<TEntity> RetrieveAll() {
30: ISession session = GetSession();
31: ICriteria targetObjects = session.CreateCriteria(typeof(TEntity));
32:
33: return targetObjects.List<TEntity>();
34: }
35:
36: public void Update(TEntity entity) {
37: ISession session = GetSession();
38:
39: using (ITransaction transaction = session.BeginTransaction()) {
40: session.Update(entity);
41:
42: transaction.Commit();
43: }
44: }
45:
46: public void Delete(TEntity entity) {
47: ISession session = GetSession();
48: using (ITransaction transaction = session.BeginTransaction()) {
49: session.Delete(entity);
50:
51:
52: transaction.Commit();
53: }
54: }
55:
56: #endregion
57:
58: protected ISession GetSession() {
59: return _sessionBuilder.CurrentSession;
60: }
61: }