| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Elsa.Common.Entities; |
| | | 3 | | using Elsa.Common.Models; |
| | | 4 | | using Elsa.Common.Multitenancy; |
| | | 5 | | using Elsa.Persistence.EFCore.Extensions; |
| | | 6 | | using Elsa.Extensions; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | using Microsoft.EntityFrameworkCore; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection; |
| | | 10 | | using Open.Linq.AsyncExtensions; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Persistence.EFCore; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// A generic repository class around EF Core for accessing entities. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <typeparam name="TDbContext">The type of the database context.</typeparam> |
| | | 18 | | /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| | | 19 | | [PublicAPI] |
| | 4390 | 20 | | public class Store<TDbContext, TEntity>(IDbContextFactory<TDbContext> dbContextFactory, IServiceProvider serviceProvider |
| | | 21 | | { |
| | | 22 | | private const int SqlServerBulkWriteMaxRetryCount = 3; |
| | 6 | 23 | | private static readonly TimeSpan SqlServerBulkWriteBaseDelay = TimeSpan.FromMilliseconds(50); |
| | | 24 | | |
| | | 25 | | // ReSharper disable once StaticMemberInGenericType |
| | | 26 | | // Justification: This is a static member that is used to ensure that only one thread can access the database for TE |
| | 6 | 27 | | private static readonly SemaphoreSlim Semaphore = new(1, 1); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Creates a new instance of the database context. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 33 | | /// <returns>The database context.</returns> |
| | 22819 | 34 | | public async Task<TDbContext> CreateDbContextAsync(CancellationToken cancellationToken = default) => await dbContext |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Adds the specified entity. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="entity">The entity to add.</param> |
| | | 40 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 41 | | public async Task AddAsync(TEntity entity, CancellationToken cancellationToken = default) |
| | | 42 | | { |
| | 0 | 43 | | await AddAsync(entity, null, cancellationToken); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Adds the specified entity. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="entity">The entity to add.</param> |
| | | 50 | | /// <param name="onAdding">The callback to invoke before adding the entity.</param> |
| | | 51 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 52 | | public async Task AddAsync(TEntity entity, Func<TDbContext, TEntity, CancellationToken, ValueTask>? onAdding, Cancel |
| | | 53 | | { |
| | 51 | 54 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | | 55 | | |
| | 51 | 56 | | if (onAdding != null) |
| | 51 | 57 | | await onAdding(dbContext, entity, cancellationToken); |
| | | 58 | | |
| | 51 | 59 | | var set = dbContext.Set<TEntity>(); |
| | 51 | 60 | | await set.AddAsync(entity, cancellationToken); |
| | 51 | 61 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 51 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Adds the specified entities. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="entities">The entities to save.</param> |
| | | 68 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 69 | | public async Task AddManyAsync( |
| | | 70 | | IEnumerable<TEntity> entities, |
| | | 71 | | CancellationToken cancellationToken = default) |
| | | 72 | | { |
| | 0 | 73 | | await AddManyAsync(entities, null, cancellationToken); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Adds the specified entities. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="entities">The entities to save.</param> |
| | | 80 | | /// <param name="onSaving">The callback to invoke before saving the entity.</param> |
| | | 81 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 82 | | public async Task AddManyAsync( |
| | | 83 | | IEnumerable<TEntity> entities, |
| | | 84 | | Func<TDbContext, TEntity, CancellationToken, ValueTask>? onSaving = null, |
| | | 85 | | CancellationToken cancellationToken = default) |
| | | 86 | | { |
| | 378 | 87 | | await Semaphore.WaitAsync(cancellationToken); |
| | | 88 | | |
| | | 89 | | try |
| | | 90 | | { |
| | 378 | 91 | | var entityList = entities.ToList(); |
| | | 92 | | |
| | 378 | 93 | | if (entityList.Count == 0) |
| | 0 | 94 | | return; |
| | | 95 | | |
| | 378 | 96 | | await ExecuteBulkWriteWithSqlServerRetryAsync(async (dbContext, ct) => |
| | 378 | 97 | | { |
| | 378 | 98 | | if (onSaving != null) |
| | 378 | 99 | | { |
| | 5608 | 100 | | var savingTasks = entityList.Select(entity => onSaving(dbContext, entity, ct).AsTask()).ToList(); |
| | 378 | 101 | | await Task.WhenAll(savingTasks); |
| | 378 | 102 | | } |
| | 378 | 103 | | |
| | 378 | 104 | | await dbContext.BulkInsertAsync(entityList, ct); |
| | 756 | 105 | | }, cancellationToken); |
| | 378 | 106 | | } |
| | 0 | 107 | | catch (Exception ex) |
| | | 108 | | { |
| | 0 | 109 | | await HandleDbExceptionAsync(ex, cancellationToken); |
| | 0 | 110 | | throw; |
| | | 111 | | } |
| | | 112 | | finally |
| | | 113 | | { |
| | 378 | 114 | | Semaphore.Release(); |
| | | 115 | | } |
| | 378 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Saves the entity. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <param name="entity">The entity to save.</param> |
| | | 122 | | /// <param name="keySelector">The key selector to get the primary key property.</param> |
| | | 123 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 0 | 124 | | public async Task SaveAsync(TEntity entity, Expression<Func<TEntity, string>> keySelector, CancellationToken cancell |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Saves the entity. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="entity">The entity to save.</param> |
| | | 130 | | /// <param name="keySelector">The key selector to get the primary key property.</param> |
| | | 131 | | /// <param name="onSaving">The callback to invoke before saving the entity.</param> |
| | | 132 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 133 | | public async Task SaveAsync(TEntity entity, Expression<Func<TEntity, string>> keySelector, Func<TDbContext, TEntity, |
| | | 134 | | { |
| | 473 | 135 | | await Semaphore.WaitAsync(cancellationToken); // Asynchronous wait |
| | | 136 | | |
| | | 137 | | try |
| | | 138 | | { |
| | 473 | 139 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | | 140 | | |
| | 473 | 141 | | if (onSaving != null) |
| | 473 | 142 | | await onSaving(dbContext, entity, cancellationToken); |
| | | 143 | | |
| | 473 | 144 | | var set = dbContext.Set<TEntity>(); |
| | 473 | 145 | | var lambda = keySelector.BuildEqualsExpression(entity); |
| | 473 | 146 | | var exists = await set.AnyAsync(lambda, cancellationToken); |
| | 473 | 147 | | set.Entry(entity).State = exists ? EntityState.Modified : EntityState.Added; |
| | 473 | 148 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 472 | 149 | | } |
| | 1 | 150 | | catch (Exception ex) |
| | | 151 | | { |
| | 1 | 152 | | await HandleDbExceptionAsync(ex, cancellationToken); |
| | 1 | 153 | | throw; |
| | | 154 | | } |
| | | 155 | | finally |
| | | 156 | | { |
| | 473 | 157 | | Semaphore.Release(); |
| | | 158 | | } |
| | 472 | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Saves the specified entities. |
| | | 163 | | /// </summary> |
| | | 164 | | /// <param name="entities">The entities to save.</param> |
| | | 165 | | /// <param name="keySelector">The key selector to get the primary key property.</param> |
| | | 166 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 0 | 167 | | public async Task SaveManyAsync(IEnumerable<TEntity> entities, Expression<Func<TEntity, string>> keySelector, Cancel |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Saves the specified entities. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <param name="entities">The entities to save.</param> |
| | | 173 | | /// <param name="keySelector">The key selector to get the primary key property.</param> |
| | | 174 | | /// <param name="onSaving">The callback to invoke before saving the entity.</param> |
| | | 175 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 176 | | public async Task SaveManyAsync( |
| | | 177 | | IEnumerable<TEntity> entities, |
| | | 178 | | Expression<Func<TEntity, string>> keySelector, |
| | | 179 | | Func<TDbContext, TEntity, CancellationToken, ValueTask>? onSaving = null, |
| | | 180 | | CancellationToken cancellationToken = default) |
| | | 181 | | { |
| | 3186 | 182 | | await Semaphore.WaitAsync(cancellationToken); |
| | | 183 | | |
| | | 184 | | try |
| | | 185 | | { |
| | 3186 | 186 | | var entityList = entities.ToList(); |
| | | 187 | | |
| | 3186 | 188 | | if (entityList.Count == 0) |
| | 0 | 189 | | return; |
| | | 190 | | |
| | 3186 | 191 | | var tenantId = serviceProvider.GetRequiredService<ITenantAccessor>().TenantId; |
| | | 192 | | |
| | 3186 | 193 | | await ExecuteBulkWriteWithSqlServerRetryAsync(async (dbContext, ct) => |
| | 3186 | 194 | | { |
| | 3186 | 195 | | if (onSaving != null) |
| | 3186 | 196 | | { |
| | 8734 | 197 | | var savingTasks = entityList.Select(entity => onSaving(dbContext, entity, ct).AsTask()).ToList(); |
| | 3186 | 198 | | await Task.WhenAll(savingTasks); |
| | 3186 | 199 | | } |
| | 3186 | 200 | | |
| | 3186 | 201 | | // When doing a custom SQL query (Bulk Upsert), none of the installed query filters will be applied. Hen |
| | 17468 | 202 | | foreach (var entity in entityList) |
| | 3186 | 203 | | { |
| | 5548 | 204 | | if (entity is Entity entityWithTenant) |
| | 3186 | 205 | | { |
| | 3186 | 206 | | // Don't touch tenant-agnostic entities (marked with "*") |
| | 5548 | 207 | | if (entityWithTenant.TenantId == Tenant.AgnosticTenantId) |
| | 3186 | 208 | | continue; |
| | 3186 | 209 | | |
| | 3186 | 210 | | // Apply current tenant ID to entities without one |
| | 3280 | 211 | | if (entityWithTenant.TenantId == null && tenantId != null) |
| | 2725 | 212 | | entityWithTenant.TenantId = tenantId; |
| | 3186 | 213 | | } |
| | 3186 | 214 | | } |
| | 3186 | 215 | | |
| | 3186 | 216 | | await dbContext.BulkUpsertAsync(entityList, keySelector, ct); |
| | 6372 | 217 | | }, cancellationToken); |
| | 3186 | 218 | | } |
| | 0 | 219 | | catch (Exception ex) |
| | | 220 | | { |
| | 0 | 221 | | await HandleDbExceptionAsync(ex, cancellationToken); |
| | 0 | 222 | | throw; |
| | | 223 | | } |
| | | 224 | | finally |
| | | 225 | | { |
| | 3186 | 226 | | Semaphore.Release(); |
| | | 227 | | } |
| | 3186 | 228 | | } |
| | | 229 | | |
| | | 230 | | private async Task HandleDbExceptionAsync(Exception exception, CancellationToken cancellationToken) |
| | | 231 | | { |
| | 1 | 232 | | var handler = serviceProvider.GetService<IDbExceptionHandler>(); |
| | | 233 | | |
| | 1 | 234 | | if (handler == null) |
| | 1 | 235 | | return; |
| | | 236 | | |
| | 0 | 237 | | var context = new DbUpdateExceptionContext(exception, cancellationToken); |
| | 0 | 238 | | await handler.HandleAsync(context); |
| | 1 | 239 | | } |
| | | 240 | | |
| | | 241 | | private async Task ExecuteBulkWriteWithSqlServerRetryAsync( |
| | | 242 | | Func<TDbContext, CancellationToken, Task> operation, |
| | | 243 | | CancellationToken cancellationToken) |
| | | 244 | | { |
| | 3564 | 245 | | for (var attempt = 0;; attempt++) |
| | | 246 | | { |
| | 3564 | 247 | | var providerName = string.Empty; |
| | | 248 | | |
| | | 249 | | try |
| | | 250 | | { |
| | 3564 | 251 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 3564 | 252 | | providerName = dbContext.Database.ProviderName ?? string.Empty; |
| | 3564 | 253 | | await operation(dbContext, cancellationToken); |
| | 3564 | 254 | | return; |
| | 0 | 255 | | } |
| | 0 | 256 | | catch (Exception ex) |
| | | 257 | | { |
| | 0 | 258 | | if (ShouldRetrySqlServerBulkWrite(providerName, ex, attempt, cancellationToken)) |
| | | 259 | | { |
| | 0 | 260 | | await Task.Delay(GetSqlServerBulkWriteRetryDelay(attempt), cancellationToken); |
| | 0 | 261 | | continue; |
| | | 262 | | } |
| | | 263 | | |
| | 0 | 264 | | throw; |
| | | 265 | | } |
| | 0 | 266 | | } |
| | 3564 | 267 | | } |
| | | 268 | | |
| | | 269 | | private static bool ShouldRetrySqlServerBulkWrite(string providerName, Exception exception, int attempt, Cancellatio |
| | | 270 | | { |
| | 0 | 271 | | return attempt < SqlServerBulkWriteMaxRetryCount |
| | 0 | 272 | | && !cancellationToken.IsCancellationRequested |
| | 0 | 273 | | && exception is not OperationCanceledException |
| | 0 | 274 | | && DbExceptionClassifier.IsSqlServerTransient(providerName, exception); |
| | | 275 | | } |
| | | 276 | | |
| | 0 | 277 | | private static TimeSpan GetSqlServerBulkWriteRetryDelay(int attempt) => TimeSpan.FromMilliseconds(SqlServerBulkWrite |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// Updates the entity. |
| | | 281 | | /// </summary> |
| | | 282 | | /// <param name="entity">The entity to update.</param> |
| | | 283 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 284 | | public Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default) |
| | | 285 | | { |
| | 0 | 286 | | return UpdateAsync(entity, null, cancellationToken); |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <summary> |
| | | 290 | | /// Updates the entity. |
| | | 291 | | /// </summary> |
| | | 292 | | /// <param name="entity">The entity to update.</param> |
| | | 293 | | /// <param name="onSaving">The callback to invoke before saving the entity.</param> |
| | | 294 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 295 | | public async Task UpdateAsync(TEntity entity, Func<TDbContext, TEntity, CancellationToken, ValueTask>? onSaving, Can |
| | | 296 | | { |
| | 0 | 297 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | | 298 | | |
| | 0 | 299 | | if (onSaving != null) |
| | 0 | 300 | | await onSaving(dbContext, entity, cancellationToken); |
| | | 301 | | |
| | 0 | 302 | | var set = dbContext.Set<TEntity>(); |
| | 0 | 303 | | set.Entry(entity).State = EntityState.Modified; |
| | 0 | 304 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 0 | 305 | | } |
| | | 306 | | |
| | | 307 | | /// <summary> |
| | | 308 | | /// Updates specific properties of an entity in the database. |
| | | 309 | | /// </summary> |
| | | 310 | | /// <param name="entity">The entity to update.</param> |
| | | 311 | | /// <param name="properties">An array of expressions indicating the properties to update.</param> |
| | | 312 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 313 | | /// <returns>A task that represents the asynchronous operation.</returns> |
| | | 314 | | public async Task UpdatePartialAsync(TEntity entity, Expression<Func<TEntity, object>>[] properties, CancellationTok |
| | | 315 | | { |
| | 0 | 316 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 317 | | dbContext.Attach(entity); |
| | | 318 | | |
| | 0 | 319 | | foreach (var property in properties) |
| | 0 | 320 | | dbContext.Entry(entity).Property(property).IsModified = true; |
| | | 321 | | |
| | 0 | 322 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 0 | 323 | | } |
| | | 324 | | |
| | | 325 | | /// <summary> |
| | | 326 | | /// Finds the entity matching the specified predicate. |
| | | 327 | | /// </summary> |
| | | 328 | | /// <param name="predicate">The predicate to use.</param> |
| | | 329 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 330 | | /// <returns>The entity if found, otherwise <c>null</c>.</returns> |
| | 0 | 331 | | public async Task<TEntity?> FindAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken |
| | | 332 | | |
| | | 333 | | /// <summary> |
| | | 334 | | /// Finds the entity matching the specified predicate. |
| | | 335 | | /// </summary> |
| | | 336 | | /// <param name="predicate">The predicate to use.</param> |
| | | 337 | | /// <param name="onLoading">A callback to run after the entity is loaded</param> |
| | | 338 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 339 | | /// <returns></returns> |
| | | 340 | | public async Task<TEntity?> FindAsync(Expression<Func<TEntity, bool>> predicate, Func<TDbContext, TEntity?, TEntity? |
| | | 341 | | { |
| | 0 | 342 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 343 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 0 | 344 | | var entity = await set.FirstOrDefaultAsync(predicate, cancellationToken); |
| | | 345 | | |
| | 0 | 346 | | if (entity == null) |
| | 0 | 347 | | return null; |
| | | 348 | | |
| | 0 | 349 | | if (onLoading != null) |
| | 0 | 350 | | entity = onLoading.Invoke(dbContext, entity); |
| | | 351 | | |
| | 0 | 352 | | return entity; |
| | 0 | 353 | | } |
| | | 354 | | |
| | | 355 | | /// <summary> |
| | | 356 | | /// Finds a single entity using a query |
| | | 357 | | /// </summary> |
| | | 358 | | /// <param name="query">The query to use</param> |
| | | 359 | | /// <param name="onLoading">A callback to run after the entity is loaded</param> |
| | | 360 | | /// <param name="cancellationToken">The cancellation token</param> |
| | | 361 | | /// <returns>The entity if found, otherwise <c>null</c></returns> |
| | | 362 | | public async Task<TEntity?> FindAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Func<TDbContext, TEntity |
| | | 363 | | { |
| | 0 | 364 | | return await FindAsync(query, onLoading, false, cancellationToken); |
| | 0 | 365 | | } |
| | | 366 | | |
| | | 367 | | /// <summary> |
| | | 368 | | /// Finds a single entity using a query |
| | | 369 | | /// </summary> |
| | | 370 | | /// <param name="query">The query to use</param> |
| | | 371 | | /// <param name="onLoading">A callback to run after the entity is loaded</param> |
| | | 372 | | /// <param name="tenantAgnostic">Define is the request should be tenant agnostic or not</param> |
| | | 373 | | /// <param name="cancellationToken">The cancellation token</param> |
| | | 374 | | /// <returns>The entity if found, otherwise <c>null</c></returns> |
| | | 375 | | public async Task<TEntity?> FindAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Func<TDbContext, TEntity |
| | | 376 | | { |
| | 2 | 377 | | return await QueryAsync(query, onLoading, tenantAgnostic, cancellationToken).FirstOrDefault(); |
| | 2 | 378 | | } |
| | | 379 | | |
| | | 380 | | /// <summary> |
| | | 381 | | /// Finds a single entity using a query |
| | | 382 | | /// </summary> |
| | | 383 | | /// <param name="query">The query to use</param> |
| | | 384 | | /// <param name="cancellationToken">The cancellation token</param> |
| | | 385 | | /// <returns>The entity if found, otherwise <c>null</c></returns> |
| | | 386 | | public async Task<TEntity?> FindAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel |
| | | 387 | | { |
| | 0 | 388 | | return await FindAsync(query, false, cancellationToken); |
| | 0 | 389 | | } |
| | | 390 | | |
| | | 391 | | /// <summary> |
| | | 392 | | /// Finds a single entity using a query |
| | | 393 | | /// </summary> |
| | | 394 | | /// <param name="query">The query to use</param> |
| | | 395 | | /// <param name="tenantAgnostic">Define is the request should be tenant agnostic or not</param> |
| | | 396 | | /// <param name="cancellationToken">The cancellation token</param> |
| | | 397 | | /// <returns>The entity if found, otherwise <c>null</c></returns> |
| | | 398 | | public async Task<TEntity?> FindAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, bool tenantAgnostic = fa |
| | | 399 | | { |
| | 0 | 400 | | return await QueryAsync(query, tenantAgnostic, cancellationToken).FirstOrDefault(); |
| | 0 | 401 | | } |
| | | 402 | | |
| | | 403 | | /// <summary> |
| | | 404 | | /// Finds a list of entities using a query |
| | | 405 | | /// </summary> |
| | 0 | 406 | | public async Task<IEnumerable<TEntity>> FindManyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken c |
| | | 407 | | |
| | | 408 | | /// <summary> |
| | | 409 | | /// Finds a list of entities using a query |
| | | 410 | | /// </summary> |
| | | 411 | | public async Task<IEnumerable<TEntity>> FindManyAsync(Expression<Func<TEntity, bool>> predicate, Action<TDbContext, |
| | | 412 | | { |
| | 0 | 413 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 414 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 0 | 415 | | var entities = await set.Where(predicate).ToListAsync(cancellationToken); |
| | | 416 | | |
| | 0 | 417 | | if (onLoading != null) |
| | 0 | 418 | | foreach (var entity in entities) |
| | 0 | 419 | | onLoading(dbContext, entity); |
| | | 420 | | |
| | 0 | 421 | | return entities; |
| | 0 | 422 | | } |
| | | 423 | | |
| | | 424 | | /// <summary> |
| | | 425 | | /// Finds a list of entities using a query |
| | | 426 | | /// </summary> |
| | | 427 | | public async Task<Page<TEntity>> FindManyAsync<TKey>( |
| | | 428 | | Expression<Func<TEntity, bool>> predicate, |
| | | 429 | | Expression<Func<TEntity, TKey>> orderBy, |
| | | 430 | | OrderDirection orderDirection = OrderDirection.Ascending, |
| | | 431 | | PageArgs? pageArgs = null, |
| | | 432 | | CancellationToken cancellationToken = default) => |
| | 0 | 433 | | await FindManyAsync(predicate, orderBy, orderDirection, pageArgs, null, cancellationToken); |
| | | 434 | | |
| | | 435 | | /// <summary> |
| | | 436 | | /// Returns a list of entities using a query |
| | | 437 | | /// </summary> |
| | | 438 | | public async Task<Page<TEntity>> FindManyAsync<TKey>( |
| | | 439 | | Expression<Func<TEntity, bool>>? predicate, |
| | | 440 | | Expression<Func<TEntity, TKey>>? orderBy, |
| | | 441 | | OrderDirection orderDirection = OrderDirection.Ascending, |
| | | 442 | | PageArgs? pageArgs = null, |
| | | 443 | | Func<TDbContext, TEntity?, TEntity?>? onLoading = null, |
| | | 444 | | CancellationToken cancellationToken = default) |
| | | 445 | | { |
| | 0 | 446 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 447 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | | 448 | | |
| | 0 | 449 | | if (predicate != null) |
| | 0 | 450 | | set = set.Where(predicate); |
| | | 451 | | |
| | 0 | 452 | | if (orderBy != null) |
| | 0 | 453 | | set = orderDirection switch |
| | 0 | 454 | | { |
| | 0 | 455 | | OrderDirection.Ascending => set.OrderBy(orderBy), |
| | 0 | 456 | | OrderDirection.Descending => set.OrderByDescending(orderBy), |
| | 0 | 457 | | _ => set.OrderBy(orderBy) |
| | 0 | 458 | | }; |
| | | 459 | | |
| | 0 | 460 | | var page = await set.PaginateAsync(pageArgs); |
| | | 461 | | |
| | 0 | 462 | | if (onLoading != null) |
| | 0 | 463 | | page = page with |
| | 0 | 464 | | { |
| | 0 | 465 | | Items = page.Items.Select(x => onLoading(dbContext, x)!).ToList() |
| | 0 | 466 | | }; |
| | | 467 | | |
| | 0 | 468 | | return page; |
| | 0 | 469 | | } |
| | | 470 | | |
| | | 471 | | public Task<IEnumerable<TEntity>> ListAsync(CancellationToken cancellationToken = default) |
| | | 472 | | { |
| | 0 | 473 | | return ListAsync(null, cancellationToken); |
| | | 474 | | } |
| | | 475 | | |
| | | 476 | | public async Task<IEnumerable<TEntity>> ListAsync(Action<TDbContext, TEntity?>? onLoading = null, CancellationToken |
| | | 477 | | { |
| | 0 | 478 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 479 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 0 | 480 | | var entities = await set.ToListAsync(cancellationToken); |
| | | 481 | | |
| | 0 | 482 | | if (onLoading != null) |
| | 0 | 483 | | foreach (var entity in entities) |
| | 0 | 484 | | onLoading(dbContext, entity); |
| | | 485 | | |
| | 0 | 486 | | return entities; |
| | 0 | 487 | | } |
| | | 488 | | |
| | | 489 | | /// <summary> |
| | | 490 | | /// Finds a single entity using a query. |
| | | 491 | | /// </summary> |
| | | 492 | | /// <returns>True if the entity was found, otherwise false.</returns> |
| | | 493 | | public async Task<bool> DeleteAsync(TEntity entity, CancellationToken cancellationToken = default) |
| | | 494 | | { |
| | 0 | 495 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 496 | | var set = dbContext.Set<TEntity>(); |
| | 0 | 497 | | set.Attach(entity).State = EntityState.Deleted; |
| | 0 | 498 | | return await dbContext.SaveChangesAsync(cancellationToken) == 1; |
| | 0 | 499 | | } |
| | | 500 | | |
| | | 501 | | /// <summary> |
| | | 502 | | /// Deletes entities using a predicate. |
| | | 503 | | /// </summary> |
| | | 504 | | /// <returns>The number of entities deleted.</returns> |
| | | 505 | | public async Task<long> DeleteWhereAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationTo |
| | | 506 | | { |
| | 4 | 507 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 4 | 508 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 4 | 509 | | return await set.Where(predicate).ExecuteDeleteAsync(cancellationToken); |
| | 4 | 510 | | } |
| | | 511 | | |
| | | 512 | | /// <summary> |
| | | 513 | | /// Deletes entities using a query. |
| | | 514 | | /// </summary> |
| | | 515 | | /// <returns>The number of entities deleted.</returns> |
| | | 516 | | public async Task<long> DeleteWhereAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken can |
| | | 517 | | { |
| | 115 | 518 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 115 | 519 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 115 | 520 | | var queryable = query(set.AsQueryable()); |
| | 115 | 521 | | return await queryable.ExecuteDeleteAsync(cancellationToken); |
| | 115 | 522 | | } |
| | | 523 | | |
| | | 524 | | /// <summary> |
| | | 525 | | /// Queries the database using a query. |
| | | 526 | | /// </summary> |
| | | 527 | | public async Task<IEnumerable<TEntity>> QueryAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Cancellatio |
| | | 528 | | { |
| | 340 | 529 | | return await QueryAsync(query, null, false, cancellationToken); |
| | 340 | 530 | | } |
| | | 531 | | |
| | | 532 | | /// <summary> |
| | | 533 | | /// Queries the database using a query. |
| | | 534 | | /// </summary> |
| | | 535 | | public async Task<IEnumerable<TEntity>> QueryAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, bool tenant |
| | | 536 | | { |
| | 0 | 537 | | return await QueryAsync(query, null, tenantAgnostic, cancellationToken); |
| | 0 | 538 | | } |
| | | 539 | | |
| | | 540 | | /// <summary> |
| | | 541 | | /// Queries the database using a query and a selector. |
| | | 542 | | /// </summary> |
| | | 543 | | public async Task<IEnumerable<TEntity>> QueryAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Func<TDbCon |
| | | 544 | | { |
| | 389 | 545 | | return await QueryAsync(query, onLoading, false, cancellationToken); |
| | 389 | 546 | | } |
| | | 547 | | |
| | | 548 | | /// <summary> |
| | | 549 | | /// Queries the database using a query and a selector. |
| | | 550 | | /// </summary> |
| | | 551 | | public async Task<IEnumerable<TEntity>> QueryAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Func<TDbCon |
| | | 552 | | { |
| | 18510 | 553 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 18510 | 554 | | var asNoTracking = onLoading == null; |
| | 18510 | 555 | | var set = asNoTracking ? dbContext.Set<TEntity>().AsNoTracking() : dbContext.Set<TEntity>(); |
| | 18510 | 556 | | var queryable = query(set.AsQueryable()); |
| | | 557 | | |
| | 18510 | 558 | | if (ignoreQueryFilters) |
| | 0 | 559 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 560 | | |
| | 18510 | 561 | | var entities = await queryable.ToListAsync(cancellationToken); |
| | | 562 | | |
| | 18510 | 563 | | if (onLoading != null) |
| | | 564 | | { |
| | 35385 | 565 | | var loadingTasks = entities.Select(entity => onLoading(dbContext, entity, cancellationToken).AsTask()).ToLis |
| | 18170 | 566 | | await Task.WhenAll(loadingTasks); |
| | | 567 | | } |
| | | 568 | | |
| | 18510 | 569 | | return entities; |
| | 18510 | 570 | | } |
| | | 571 | | |
| | | 572 | | /// <summary> |
| | | 573 | | /// Queries the database using a query and a selector. |
| | | 574 | | /// </summary> |
| | | 575 | | public async Task<IEnumerable<TResult>> QueryAsync<TResult>(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Ex |
| | | 576 | | { |
| | 8 | 577 | | return await QueryAsync(query, selector, false, cancellationToken); |
| | 8 | 578 | | } |
| | | 579 | | |
| | | 580 | | /// <summary> |
| | | 581 | | /// Queries the database using a query and a selector. |
| | | 582 | | /// </summary> |
| | | 583 | | public async Task<IEnumerable<TResult>> QueryAsync<TResult>(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, Ex |
| | | 584 | | { |
| | 8 | 585 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 8 | 586 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 8 | 587 | | var queryable = query(set.AsQueryable()); |
| | | 588 | | |
| | 8 | 589 | | if (ignoreQueryFilters) |
| | 0 | 590 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 591 | | |
| | 8 | 592 | | queryable = query(queryable); |
| | 8 | 593 | | return await queryable.Select(selector).ToListAsync(cancellationToken); |
| | 8 | 594 | | } |
| | | 595 | | |
| | | 596 | | /// <summary> |
| | | 597 | | /// Counts the number of entities matching a query. |
| | | 598 | | /// </summary> |
| | | 599 | | public async Task<long> CountAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancellat |
| | | 600 | | { |
| | 0 | 601 | | return await CountAsync(query, false, cancellationToken); |
| | 0 | 602 | | } |
| | | 603 | | |
| | | 604 | | /// <summary> |
| | | 605 | | /// Counts the number of entities matching a query. |
| | | 606 | | /// </summary> |
| | | 607 | | public async Task<long> CountAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, bool ignoreQueryFilters = f |
| | | 608 | | { |
| | 0 | 609 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 610 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 0 | 611 | | var queryable = query(set.AsQueryable()); |
| | | 612 | | |
| | 0 | 613 | | if (ignoreQueryFilters) |
| | 0 | 614 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 615 | | |
| | 0 | 616 | | queryable = query(queryable); |
| | 0 | 617 | | return await queryable.LongCountAsync(cancellationToken: cancellationToken); |
| | 0 | 618 | | } |
| | | 619 | | |
| | | 620 | | /// <summary> |
| | | 621 | | /// Checks if any entities exist. |
| | | 622 | | /// </summary> |
| | | 623 | | public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = de |
| | | 624 | | { |
| | 0 | 625 | | return await AnyAsync(predicate, false, cancellationToken); |
| | 0 | 626 | | } |
| | | 627 | | |
| | | 628 | | /// <summary> |
| | | 629 | | /// Checks if any entities exist. |
| | | 630 | | /// </summary> |
| | | 631 | | public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, bool ignoreQueryFilters = false, Cancell |
| | | 632 | | { |
| | 0 | 633 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 634 | | var set = dbContext.Set<TEntity>().AsNoTracking(); |
| | 0 | 635 | | return await set.AnyAsync(predicate, cancellationToken); |
| | 0 | 636 | | } |
| | | 637 | | |
| | | 638 | | /// <summary> |
| | | 639 | | /// Counts the number of entities matching a predicate. |
| | | 640 | | /// </summary> |
| | | 641 | | /// <param name="predicate">The predicate.</param> |
| | | 642 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 643 | | public async Task<long> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = |
| | | 644 | | { |
| | 0 | 645 | | return await CountAsync(predicate, false, cancellationToken); |
| | 0 | 646 | | } |
| | | 647 | | |
| | | 648 | | /// <summary> |
| | | 649 | | /// Counts the number of entities matching a predicate. |
| | | 650 | | /// </summary> |
| | | 651 | | /// <param name="predicate">The predicate.</param> |
| | | 652 | | /// <param name="ignoreQueryFilters">Whether to ignore query filters.</param> |
| | | 653 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 654 | | public async Task<long> CountAsync(Expression<Func<TEntity, bool>> predicate, bool ignoreQueryFilters = false, Cance |
| | | 655 | | { |
| | 0 | 656 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 657 | | var queryable = dbContext.Set<TEntity>().AsNoTracking(); |
| | | 658 | | |
| | 0 | 659 | | if (ignoreQueryFilters) |
| | 0 | 660 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 661 | | |
| | 0 | 662 | | return await queryable.CountAsync(predicate, cancellationToken); |
| | 0 | 663 | | } |
| | | 664 | | |
| | | 665 | | /// <summary> |
| | | 666 | | /// Counts the distinct number of entities matching a predicate. |
| | | 667 | | /// </summary> |
| | | 668 | | /// <param name="predicate">The predicate.</param> |
| | | 669 | | /// <param name="propertySelector">The property selector to distinct by.</param> |
| | | 670 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 671 | | public async Task<long> CountAsync<TProperty>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TP |
| | | 672 | | { |
| | 0 | 673 | | return await CountAsync(predicate, propertySelector, false, cancellationToken); |
| | 0 | 674 | | } |
| | | 675 | | |
| | | 676 | | /// <summary> |
| | | 677 | | /// Counts the distinct number of entities matching a predicate. |
| | | 678 | | /// </summary> |
| | | 679 | | /// <param name="predicate">The predicate.</param> |
| | | 680 | | /// <param name="propertySelector">The property selector to distinct by.</param> |
| | | 681 | | /// <param name="ignoreQueryFilters">Whether to ignore query filters.</param> |
| | | 682 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 683 | | public async Task<long> CountAsync<TProperty>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TP |
| | | 684 | | { |
| | 0 | 685 | | await using var dbContext = await CreateDbContextAsync(cancellationToken); |
| | 0 | 686 | | var queryable = dbContext.Set<TEntity>().AsNoTracking(); |
| | | 687 | | |
| | 0 | 688 | | if (ignoreQueryFilters) |
| | 0 | 689 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 690 | | |
| | 0 | 691 | | return await queryable |
| | 0 | 692 | | .Where(predicate) |
| | 0 | 693 | | .Select(propertySelector) |
| | 0 | 694 | | .Distinct() |
| | 0 | 695 | | .CountAsync(cancellationToken); |
| | 0 | 696 | | } |
| | | 697 | | } |