summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Generic/GenericRepository_T_.cs
blob: f0c15d7537d67fed63a20afaa130e84fc5c4336d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Gestor.Infrastructure.Entities.Generic;
using NHibernate;
using NHibernate.Linq;
using NHibernate.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Gestor.Infrastructure.Repository.Generic
{
	public class GenericRepository<T> : IGenericRepository<T>
	where T : class
	{
		private readonly ISession _currentSession;

		public GenericRepository(ISession currentSession)
		{
			this._currentSession = currentSession;
		}

		public virtual void Add(T entity)
		{
			this._currentSession.Save(entity);
		}

		public void AddRange(IEnumerable<T> entities)
		{
			if (entities != null)
			{
				entities.ForEach<T>((T entity) => this._currentSession.Save(entity));
			}
		}

		public IQueryable<T> All()
		{
			return this._currentSession.Query<T>();
		}

		public void Delete(T entity)
		{
			this._currentSession.Delete(entity);
		}

		public void DeleteRange(IEnumerable<T> entities)
		{
			if (entities != null)
			{
				entities.ForEach<T>((T entity) => this._currentSession.Delete(entity));
			}
		}

		public void Evict<TEntity>(TEntity entity)
		where TEntity : EntityBase
		{
			this._currentSession.Evict(entity);
		}

		public T FindEntityById(long id)
		{
			return this._currentSession.Get<T>(id);
		}

		protected void Load<TEntity>(TEntity entity)
		where TEntity : EntityBase
		{
			EntityBase entityBase = entity;
			if (entityBase == null)
			{
				return;
			}
			this._currentSession.Load(entity, entityBase.Id);
		}

		public void Merge(T entity)
		{
			this._currentSession.Merge<T>(entity);
		}

		public virtual void SaveOrUpdate(T entity)
		{
			this._currentSession.SaveOrUpdate(entity);
		}

		public void Update(T entity)
		{
			this._currentSession.Update(entity);
		}
	}
}