-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathRepository.cs
More file actions
172 lines (141 loc) · 5.83 KB
/
Copy pathRepository.cs
File metadata and controls
172 lines (141 loc) · 5.83 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using LinkDotNet.Blog.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
namespace LinkDotNet.Blog.Infrastructure.Persistence.Sql;
public sealed partial class Repository<TEntity> : IRepository<TEntity>
where TEntity : Entity
{
private readonly IDbContextFactory<BlogDbContext> dbContextFactory;
private readonly ILogger<Repository<TEntity>> logger;
public Repository(IDbContextFactory<BlogDbContext> dbContextFactory, ILogger<Repository<TEntity>> logger)
{
this.dbContextFactory = dbContextFactory;
this.logger = logger;
}
public async ValueTask<HealthCheckResult> PerformHealthCheckAsync()
{
try
{
await using var db = await dbContextFactory.CreateDbContextAsync();
await db.Database.ExecuteSqlRawAsync("SELECT 1");
return HealthCheckResult.Healthy();
}
catch (Exception exc)
{
return HealthCheckResult.Unhealthy(exception: exc);
}
}
public async ValueTask<TEntity?> GetByIdAsync(string id)
{
await using var blogDbContext = await dbContextFactory.CreateDbContextAsync();
return await blogDbContext.Set<TEntity>().FirstOrDefaultAsync(b => b.Id == id);
}
public ValueTask<IPagedList<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue) =>
GetAllByProjectionAsync(s => s, filter, orderBy, descending, page, pageSize);
public async ValueTask<IPagedList<TProjection>> GetAllByProjectionAsync<TProjection>(
Expression<Func<TEntity, TProjection>> selector,
Expression<Func<TEntity, bool>>? filter = null,
Expression<Func<TEntity, object>>? orderBy = null,
bool descending = true,
int page = 1,
int pageSize = int.MaxValue)
{
ArgumentNullException.ThrowIfNull(selector);
await using var blogDbContext = await dbContextFactory.CreateDbContextAsync();
var entity = blogDbContext.Set<TEntity>().AsNoTracking().AsQueryable();
if (filter is not null)
{
entity = entity.Where(filter);
}
if (orderBy is not null)
{
entity = descending
? entity.OrderByDescending(orderBy)
: entity.OrderBy(orderBy);
}
return await entity.Select(selector).ToPagedListAsync(page, pageSize);
}
public async ValueTask StoreAsync(TEntity entity)
{
ArgumentNullException.ThrowIfNull(entity);
await using var blogDbContext = await dbContextFactory.CreateDbContextAsync();
if (string.IsNullOrEmpty(entity.Id))
{
await blogDbContext.Set<TEntity>().AddAsync(entity);
}
else
{
blogDbContext.Entry(entity).State = EntityState.Modified;
}
await blogDbContext.SaveChangesAsync();
}
public async ValueTask DeleteAsync(string id)
{
var entityToDelete = await GetByIdAsync(id);
if (entityToDelete is not null)
{
await using var blogDbContext = await dbContextFactory.CreateDbContextAsync();
blogDbContext.Remove(entityToDelete);
await blogDbContext.SaveChangesAsync();
}
}
public async ValueTask DeleteBulkAsync(IReadOnlyCollection<string> ids)
{
await using var blogDbContext = await dbContextFactory.CreateDbContextAsync();
var strategy = blogDbContext.Database.CreateExecutionStrategy();
await strategy.ExecuteAsync(DeleteBulkAsyncInBatchesAsync);
async Task DeleteBulkAsyncInBatchesAsync()
{
await using var trx = await blogDbContext.Database.BeginTransactionAsync();
var idList = ids.ToList();
const int batchSize = 1000;
var totalBatches = (int)Math.Ceiling((double)idList.Count / batchSize);
for (var batch = 0; batch < totalBatches; batch++)
{
var currentBatchIds = idList.Skip(batch * batchSize).Take(batchSize).ToList();
await blogDbContext.Set<TEntity>()
.Where(s => currentBatchIds.Contains(s.Id))
.ExecuteDeleteAsync();
LogDeleteBatch(batch + 1, (batch + 1) * batchSize);
}
await trx.CommitAsync();
}
}
public async ValueTask StoreBulkAsync(IReadOnlyCollection<TEntity> records)
{
ArgumentNullException.ThrowIfNull(records);
await using var blogDbContext = await dbContextFactory.CreateDbContextAsync();
var strategy = blogDbContext.Database.CreateExecutionStrategy();
await strategy.ExecuteAsync(StoreBulkAsyncInBatchesAsync);
async Task StoreBulkAsyncInBatchesAsync()
{
await using var trx = await blogDbContext.Database.BeginTransactionAsync();
var count = 0;
foreach (var record in records)
{
await blogDbContext.Set<TEntity>().AddAsync(record);
if (++count % 1000 == 0)
{
LogBatch(count);
await blogDbContext.SaveChangesAsync();
}
}
await blogDbContext.SaveChangesAsync();
await trx.CommitAsync();
}
}
[LoggerMessage(LogLevel.Debug, "Saving Batch. In total {Count} elements saved")]
private partial void LogBatch(int count);
[LoggerMessage(LogLevel.Debug, "Deleted Batch {BatchNumber}. In total {TotalDeleted} elements deleted")]
private partial void LogDeleteBatch(int batchNumber, int totalDeleted);
}