-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRepository.Generic.cs.pp
More file actions
147 lines (121 loc) · 3.72 KB
/
Repository.Generic.cs.pp
File metadata and controls
147 lines (121 loc) · 3.72 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
namespace $rootnamespace$.Data
{
/// <summary>
/// Base class for POCO repository instances.
/// </summary>
/// <typeparam name="T">Type of class to persist or query</typeparam>
public class Repository<T> where T : class
{
protected readonly IDbSet<T> Set;
private readonly IDataContext _dataContext;
public Repository(IDataContext dataContext)
{
_dataContext = dataContext;
Set = _dataContext.GetDbSet<T>();
}
public IQueryable<T> All()
{
return Set.AsQueryable();
}
public IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
{
var query = All();
return includeProperties.Aggregate(query, (current, includeProperty) => current.Include(includeProperty));
}
public T FirstOrDefault()
{
return All().FirstOrDefault();
}
public T FirstOrDefault(Expression<Func<T, bool>> predicate)
{
return All().FirstOrDefault(predicate);
}
public Task<T> FirstOrDefaultAsync()
{
return All().FirstAsync();
}
public Task<T> FirstOrDefaultAsync(CancellationToken cancellationToken)
{
return All().FirstAsync(cancellationToken);
}
public Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate)
{
return All().FirstAsync(predicate);
}
public Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken)
{
return All().FirstAsync(predicate, cancellationToken);
}
public T FindById(int id)
{
return Set.Find(id);
}
public void Attach(T entity)
{
Set.Attach(entity);
_dataContext.GetEntry(entity).State = EntityState.Modified;
}
public void Detatch(T entity)
{
_dataContext.GetEntry(entity).State = EntityState.Detached;
}
public void Add(T entity)
{
Set.Add(entity);
}
public void Delete(T entity)
{
Set.Remove(entity);
}
public void Delete(Expression<Func<T, bool>> criteria)
{
var entities = Where(criteria).ToList();
foreach (var entity in entities)
{
Delete(entity);
}
}
public void Delete(IEnumerable<T> entities)
{
foreach (var entity in entities.ToList())
{
Delete(entity);
}
}
public IQueryable<T> Where(Expression<Func<T, bool>> criteria)
{
return Set.Where(criteria);
}
public int Count()
{
return Set.Count();
}
public int Count(Expression<Func<T, bool>> criteria)
{
return Set.Count(criteria);
}
public Task<int> CountAsync()
{
return Set.CountAsync();
}
public Task<int> CountAsync(CancellationToken cancellationToken)
{
return Set.CountAsync(cancellationToken);
}
public Task<int> CountAsync(Expression<Func<T, bool>> criteria)
{
return Set.CountAsync(criteria);
}
public Task<int> CountAsync(Expression<Func<T, bool>> criteria, CancellationToken cancellationToken)
{
return Set.CountAsync(criteria, cancellationToken);
}
}
}