-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathRepository.cs
More file actions
216 lines (180 loc) · 9.71 KB
/
Repository.cs
File metadata and controls
216 lines (180 loc) · 9.71 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic.Core;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Dotnet5.GraphQL3.Domain.Abstractions.Entities;
using Dotnet5.GraphQL3.Repositories.Abstractions.Pages;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
namespace Dotnet5.GraphQL3.Repositories.Abstractions
{
public abstract class Repository<TEntity, TId> : IRepository<TEntity, TId>
where TEntity : Entity<TId>
where TId : struct
{
private const string SelectorTemplate = "new({0})";
private readonly IConfigurationProvider _configuration;
private readonly DbSet<TEntity> _dbSet;
protected Repository(DbContext dbDbContext, IConfigurationProvider configuration)
{
_configuration = configuration;
_dbSet = dbDbContext.Set<TEntity>();
}
public virtual void Delete(TId id)
{
var entity = GetById(id);
if (entity is null) return;
_dbSet.Remove(entity);
}
public virtual void Delete(TEntity entity)
{
if (entity is null) return;
_dbSet.Remove(entity);
}
public virtual async Task DeleteAsync(TId id, CancellationToken cancellationToken)
{
var entity = await GetByIdAsync(id, cancellationToken);
if (entity is null) return;
_dbSet.Remove(entity);
}
public virtual bool Exists(TId id)
=> _dbSet.Any(x => Equals(x.Id, id));
public virtual Task<bool> ExistsAsync(TId id, CancellationToken cancellationToken)
=> _dbSet.AnyAsync(x => Equals(x.Id, id), cancellationToken);
public virtual TEntity Add(TEntity entity)
{
if (Exists(entity.Id)) return entity;
_dbSet.Add(entity);
return entity;
}
public virtual async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken)
{
if (await ExistsAsync(entity.Id, cancellationToken)) return entity;
await _dbSet.AddAsync(entity, cancellationToken);
return entity;
}
public TEntity GetById(TId id, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default, bool asTracking = default)
{
if (Equals(id, default(TId))) return default;
if (include is null && asTracking) return _dbSet.Find(id);
return include is null
? _dbSet.AsNoTracking().SingleOrDefault(x => Equals(x.Id, id))
: include(_dbSet).AsNoTracking().SingleOrDefault(x => Equals(x.Id, id));
}
public async Task<TEntity> GetByIdAsync(TId id, CancellationToken cancellationToken, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default, bool asTracking = default)
{
if (Equals(id, default(TId))) return default;
if (include is null && asTracking) return await _dbSet.FindAsync(new object[] {id}, cancellationToken);
return include is null
? await _dbSet.AsNoTracking().SingleOrDefaultAsync(x => Equals(x.Id, id), cancellationToken)
: await include(_dbSet).AsNoTracking().SingleOrDefaultAsync(x => Equals(x.Id, id), cancellationToken);
}
public virtual void Update(TEntity entity)
{
if (Exists(entity.Id) is false) return;
_dbSet.Update(entity);
}
public virtual async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken)
{
if (await ExistsAsync(entity.Id, cancellationToken) is false) return;
_dbSet.Update(entity);
}
public PaginatedResult<TEntity> GetAll(
PageParams pageParams,
Expression<Func<TEntity, bool>> predicate = default,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = default,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
bool asTracking = default)
{
var query = asTracking ? _dbSet.AsTracking() : _dbSet.AsNoTrackingWithIdentityResolution();
query = include is null ? query : include(query);
query = predicate is null ? query : query.Where(predicate);
query = orderBy is null ? query : orderBy(query);
return PaginatedResult<TEntity>.Create(query, pageParams);
}
public PaginatedResult<TResult> GetAllProjections<TResult>(
PageParams pageParams,
Expression<Func<TEntity, TResult>> selector = default,
Expression<Func<TEntity, bool>> predicate = default,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = default,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
bool asTracking = default)
{
var query = asTracking ? _dbSet.AsTracking() : _dbSet.AsNoTrackingWithIdentityResolution();
query = include is null ? query : include(query);
query = predicate is null ? query : query.Where(predicate);
query = orderBy is null ? query : orderBy(query);
return selector is null
? PaginatedResult<TResult>.Create(_dbSet.ProjectTo<TResult>(_configuration), pageParams)
: PaginatedResult<TResult>.Create(query.Select(selector), pageParams);
}
public PaginatedResult<TResult> GetAllDynamically<TResult>(
PageParams pageParams,
IEnumerable<string> selected,
Func<List<object>, List<TResult>> mapping,
Expression<Func<TEntity, bool>> predicate = default,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = default,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
bool asTracking = default)
{
var query = asTracking ? _dbSet.AsTracking() : _dbSet.AsNoTrackingWithIdentityResolution();
query = include is null ? query : include(query);
query = predicate is null ? query : query.Where(predicate);
query = orderBy is null ? query : orderBy(query);
var selector = string.Format(SelectorTemplate, string.Join(',', selected));
return PaginatedResult<TResult>.CreateDynamically(query.Select(selector), pageParams, mapping);
}
public Task<PaginatedResult<TEntity>> GetAllAsync(
PageParams pageParams,
CancellationToken cancellationToken,
Expression<Func<TEntity, bool>> predicate = default,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = default,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
bool asTracking = default)
{
var query = asTracking ? _dbSet.AsTracking() : _dbSet.AsNoTrackingWithIdentityResolution();
query = include is null ? query : include(query);
query = predicate is null ? query : query.Where(predicate);
query = orderBy is null ? query : orderBy(query);
return PaginatedResult<TEntity>.CreateAsync(query, pageParams, cancellationToken);
}
public async Task<PaginatedResult<TResult>> GetAllDynamicallyAsync<TResult>(
PageParams pageParams,
IEnumerable<string> selected,
Func<List<object>, List<TResult>> mapping,
Expression<Func<TEntity, bool>> predicate = default,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = default,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
bool asTracking = default)
{
var query = asTracking ? _dbSet.AsTracking() : _dbSet.AsNoTrackingWithIdentityResolution();
query = include is null ? query : include(query);
query = predicate is null ? query : query.Where(predicate);
query = orderBy is null ? query : orderBy(query);
var selector = string.Format(SelectorTemplate, string.Join(',', selected));
return await PaginatedResult<TResult>.CreateDynamicallyAsync(query.Select(selector), pageParams, mapping);
}
public Task<PaginatedResult<TResult>> GetAllProjectionsAsync<TResult>(
PageParams pageParams,
CancellationToken cancellationToken,
Expression<Func<TEntity, TResult>> selector = default,
Expression<Func<TEntity, bool>> predicate = default,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = default,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
bool asTracking = default)
{
var query = asTracking ? _dbSet.AsTracking() : _dbSet.AsNoTrackingWithIdentityResolution();
query = include is null ? query : include(query);
query = predicate is null ? query : query.Where(predicate);
query = orderBy is null ? query : orderBy(query);
return selector is null
? PaginatedResult<TResult>.CreateAsync(_dbSet.ProjectTo<TResult>(_configuration), pageParams, cancellationToken)
: PaginatedResult<TResult>.CreateAsync(query.Select(selector), pageParams, cancellationToken);
}
}
}