-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEfCoreRepository.cs
168 lines (147 loc) · 5.5 KB
/
EfCoreRepository.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
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
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using Nerosoft.Euonia.Domain;
namespace Nerosoft.Euonia.Repository.EfCore;
/// <inheritdoc />
public class EfCoreRepository<TContext, TEntity, TKey> : Repository<TContext, TEntity, TKey>
where TKey : IEquatable<TKey>
where TEntity : class, IEntity<TKey>
where TContext : DbContext, IRepositoryContext
{
/// <summary>
/// Initializes a new instance of the <see cref="EfCoreRepository{TContext, TEntity, TKey}"/> class.
/// </summary>
/// <param name="provider">The repository context.</param>
public EfCoreRepository(IContextProvider provider)
: base(provider)
{
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
//Context?.Dispose();
}
/// <inheritdoc />
public override IQueryable<TEntity> Queryable()
{
var query = Context.Set<TEntity>().AsQueryable();
if (Actions.Count > 0)
{
query = Actions.Aggregate(query, (current, action) => action(current));
}
return query;
}
/// <inheritdoc />
public override IQueryable<TEntity> BuildQuery(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IQueryable<TEntity>> handle)
{
ArgumentNullException.ThrowIfNull(predicate);
var query = Context.Set<TEntity>().AsQueryable();
if (handle != null)
{
query = handle(query);
}
return query.Where(predicate);
}
/// <inheritdoc />
public override async Task<TEntity> GetAsync(TKey key, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(key);
return await Context.FindAsync<TEntity>(key);
}
/// <inheritdoc />
public override Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IQueryable<TEntity>> handle, CancellationToken cancellationToken = default)
{
return BuildQuery(predicate, handle).FirstOrDefaultAsync(predicate, cancellationToken);
}
/// <inheritdoc />
public override Task<List<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IQueryable<TEntity>> handle, CancellationToken cancellationToken = default)
{
return BuildQuery(predicate, handle).ToListAsync(cancellationToken);
}
/// <inheritdoc />
public override Task<List<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IQueryable<TEntity>> handle, int offset, int count, CancellationToken cancellationToken = default)
{
return BuildQuery(predicate, handle).Skip(offset).Take(count).ToListAsync(cancellationToken);
}
/// <inheritdoc />
public override Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IQueryable<TEntity>> handle, CancellationToken cancellationToken = default)
{
return BuildQuery(predicate, handle).CountAsync(predicate, cancellationToken);
}
/// <inheritdoc />
public override Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IQueryable<TEntity>> handle, CancellationToken cancellationToken = default)
{
return BuildQuery(predicate, handle).LongCountAsync(predicate, cancellationToken);
}
/// <inheritdoc />
public override Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IQueryable<TEntity>> handle, CancellationToken cancellationToken = default)
{
return BuildQuery(predicate, handle).AnyAsync(predicate, cancellationToken);
}
/// <inheritdoc />
public override Task<bool> AllAsync(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IQueryable<TEntity>> handle, CancellationToken cancellationToken = default)
{
return BuildQuery(predicate, handle).AllAsync(predicate, cancellationToken);
}
/// <inheritdoc />
public override async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = true, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(entity);
var entry = await Context.AddAsync(entity, cancellationToken);
if (autoSave)
{
await SaveChangesAsync(cancellationToken);
}
return entry.Entity;
}
/// <inheritdoc />
public override async Task InsertAsync(IEnumerable<TEntity> entities, bool autoSave = true, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(entities);
await Context.AddRangeAsync(entities, cancellationToken);
if (autoSave)
{
await SaveChangesAsync(cancellationToken);
}
}
/// <inheritdoc />
public override async Task UpdateAsync(TEntity entity, bool autoSave = true, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(entity);
var _ = Context.Update(entity);
if (autoSave)
{
await SaveChangesAsync(cancellationToken);
}
}
/// <inheritdoc />
public override async Task UpdateAsync(IEnumerable<TEntity> entities, bool autoSave = true, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(entities);
Context.UpdateRange(entities);
if (autoSave)
{
await SaveChangesAsync(cancellationToken);
}
}
/// <inheritdoc />
public override async Task DeleteAsync(TEntity entity, bool autoSave = true, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(entity);
var _ = Context.Remove(entity);
if (autoSave)
{
await SaveChangesAsync(cancellationToken);
}
}
/// <inheritdoc />
public override async Task DeleteAsync(IEnumerable<TEntity> entities, bool autoSave = true, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(entities);
Context.RemoveRange(entities);
if (autoSave)
{
await SaveChangesAsync(cancellationToken);
}
}
}