-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathRepository.cs
153 lines (120 loc) · 4.9 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using N8T.Core.Domain;
using N8T.Core.Repository;
using N8T.Core.Specification;
namespace N8T.Infrastructure.EfCore
{
public class RepositoryBase<TDbContext, TEntity> : IRepository<TEntity>, IGridRepository<TEntity>
where TEntity : EntityBase, IAggregateRoot
where TDbContext : DbContext
{
private readonly TDbContext _dbContext;
protected RepositoryBase(TDbContext dbContext)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
public TEntity FindById(Guid id)
{
return _dbContext.Set<TEntity>().SingleOrDefault(e => e.Id == id);
}
public async Task<TEntity> FindOneAsync(ISpecification<TEntity> spec)
{
var specificationResult = GetQuery(_dbContext.Set<TEntity>(), spec);
return await specificationResult.FirstOrDefaultAsync();
}
public async Task<List<TEntity>> FindAsync(ISpecification<TEntity> spec)
{
var specificationResult = GetQuery(_dbContext.Set<TEntity>(), spec);
return await specificationResult.ToListAsync();
}
public async ValueTask<long> CountAsync(IGridSpecification<TEntity> spec)
{
spec.IsPagingEnabled = false;
var specificationResult = GetQuery(_dbContext.Set<TEntity>(), spec);
return await ValueTask.FromResult(specificationResult.LongCount());
}
public async Task<List<TEntity>> FindAsync(IGridSpecification<TEntity> spec)
{
var specificationResult = GetQuery(_dbContext.Set<TEntity>(), spec);
return await specificationResult.ToListAsync();
}
public async Task<TEntity> AddAsync(TEntity entity)
{
await _dbContext.Set<TEntity>().AddAsync(entity);
await _dbContext.SaveChangesAsync();
return entity;
}
public async Task RemoveAsync(TEntity entity)
{
_dbContext.Set<TEntity>().Remove(entity);
await _dbContext.SaveChangesAsync();
}
private static IQueryable<TEntity> GetQuery(IQueryable<TEntity> inputQuery,
ISpecification<TEntity> specification)
{
var query = inputQuery;
if (specification.Criteria is not null)
{
query = query.Where(specification.Criteria);
}
query = specification.Includes.Aggregate(query, (current, include) => current.Include(include));
query = specification.IncludeStrings.Aggregate(query, (current, include) => current.Include(include));
if (specification.OrderBy is not null)
{
query = query.OrderBy(specification.OrderBy);
}
else if (specification.OrderByDescending is not null)
{
query = query.OrderByDescending(specification.OrderByDescending);
}
if (specification.GroupBy is not null)
{
query = query.GroupBy(specification.GroupBy).SelectMany(x => x);
}
if (specification.IsPagingEnabled)
{
query = query.Skip(specification.Skip - 1)
.Take(specification.Take);
}
return query;
}
private static IQueryable<TEntity> GetQuery(IQueryable<TEntity> inputQuery,
IGridSpecification<TEntity> specification)
{
var query = inputQuery;
if (specification.Criterias is not null && specification.Criterias.Count > 0)
{
var expr = specification.Criterias.First();
for (var i = 1; i < specification.Criterias.Count; i++)
{
expr = expr.And(specification.Criterias[i]);
}
query = query.Where(expr);
}
query = specification.Includes.Aggregate(query, (current, include) => current.Include(include));
query = specification.IncludeStrings.Aggregate(query, (current, include) => current.Include(include));
if (specification.OrderBy is not null)
{
query = query.OrderBy(specification.OrderBy);
}
else if (specification.OrderByDescending is not null)
{
query = query.OrderByDescending(specification.OrderByDescending);
}
if (specification.GroupBy is not null)
{
query = query.GroupBy(specification.GroupBy).SelectMany(x => x);
}
if (specification.IsPagingEnabled)
{
query = query.Skip(specification.Skip - 1)
.Take(specification.Take);
}
return query;
}
}
}