/
Repository.cs
96 lines (80 loc) · 2.04 KB
/
Repository.cs
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
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Linq.Expressions;
namespace Naimad.Infrastructure.Db
{
public class Repository<T> : IRepository<T> where T : IAggregateRoot
{
private IDataSource<T> dataSource;
protected Repository() { }
public Repository(IUnitOfWork unitOfWork)
{
if (unitOfWork == null)
throw new ArgumentNullException("unitOfWork");
dataSource = unitOfWork.GetDataSource<T>();
UnitOfWork = unitOfWork;
}
public T Add(T entity)
{
return dataSource.Add(entity);
}
public void Update(T entity)
{
dataSource.Update(entity);
}
public void Delete(T entity)
{
dataSource.Delete(entity);
}
public void DeleteAll()
{
foreach (var e in this)
dataSource.Delete(e);
}
public void DeleteById(Guid id)
{
dataSource.DeleteById(id);
}
public T GetById(Guid id)
{
return dataSource.Single(x => x.Id == id);
}
public T GetByIdOrDefault(Guid id)
{
return dataSource.SingleOrDefault(x => x.Id == id);
}
public IEnumerator<T> GetEnumerator()
{
return dataSource.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public Type ElementType
{
get
{
return dataSource.ElementType;
}
}
public Expression Expression
{
get
{
return dataSource.Expression;
}
}
public IQueryProvider Provider
{
get
{
return dataSource.Provider;
}
}
public IUnitOfWork UnitOfWork { get; private set; }
}
}