| | | 1 | | using System.Data.Common; |
| | | 2 | | using ArturRios.Data.Relational.Core.Configuration; |
| | | 3 | | using ArturRios.Data.Relational.Core.Entities; |
| | | 4 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 5 | | using ArturRios.Output; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | |
| | | 8 | | namespace ArturRios.Data.Relational.Core.Repositories; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provider-agnostic Entity Framework Core implementation of the repository contracts. |
| | | 12 | | /// Every write auto-saves; inside an active unit-of-work transaction, |
| | | 13 | | /// saves flush without committing. Infrastructure failures are returned as <see cref="DataOutput{T}" /> errors. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="T">The entity type.</typeparam> |
| | | 16 | | /// <param name="context">The application's <see cref="BaseDbContext" />.</param> |
| | 25 | 17 | | public class EfRepository<T>(BaseDbContext context) : IRepository<T>, IAsyncRepository<T> |
| | | 18 | | where T : Entity |
| | | 19 | | { |
| | | 20 | | /// <summary>Message returned when an optimistic-concurrency conflict is detected.</summary> |
| | | 21 | | protected const string ConcurrencyMessage = |
| | | 22 | | "Concurrency conflict: the record was modified or removed by another process."; |
| | | 23 | | |
| | | 24 | | /// <summary>Message prefix returned when a persistence operation fails.</summary> |
| | | 25 | | protected const string PersistenceMessage = "A data-access error occurred:"; |
| | | 26 | | |
| | | 27 | | /// <summary>The tracked entity set for <typeparamref name="T" />.</summary> |
| | 44 | 28 | | protected DbSet<T> Set => context.Set<T>(); |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public Task<DataOutput<IEnumerable<T>>> GetAllAsync(CancellationToken ct = default) => |
| | 6 | 32 | | GuardedAsync<IEnumerable<T>>(async () => await Set.ToListAsync(ct)); |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public Task<DataOutput<T?>> GetByIdAsync(long id, CancellationToken ct = default) => |
| | 4 | 36 | | GuardedAsync(async () => await Set.FirstOrDefaultAsync(e => e.Id == id, ct)); |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public Task<DataOutput<long>> CreateAsync(T entity, CancellationToken ct = default) => |
| | 5 | 40 | | GuardedAsync(async () => |
| | 5 | 41 | | { |
| | 5 | 42 | | await Set.AddAsync(entity, ct); |
| | 5 | 43 | | await context.SaveChangesAsync(ct); |
| | 5 | 44 | | |
| | 5 | 45 | | return entity.Id; |
| | 10 | 46 | | }); |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public Task<DataOutput<IEnumerable<long>>> |
| | | 50 | | CreateRangeAsync(IEnumerable<T> entities, CancellationToken ct = default) => |
| | 2 | 51 | | GuardedAsync<IEnumerable<long>>(async () => |
| | 2 | 52 | | { |
| | 2 | 53 | | var list = entities.ToList(); |
| | 2 | 54 | | await Set.AddRangeAsync(list, ct); |
| | 2 | 55 | | await context.SaveChangesAsync(ct); |
| | 2 | 56 | | |
| | 6 | 57 | | return list.Select(e => e.Id).ToList(); |
| | 4 | 58 | | }); |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public Task<DataOutput<T>> UpdateAsync(T entity, CancellationToken ct = default) => |
| | 1 | 62 | | GuardedAsync(async () => |
| | 1 | 63 | | { |
| | 1 | 64 | | Set.Update(entity); |
| | 1 | 65 | | await context.SaveChangesAsync(ct); |
| | 1 | 66 | | |
| | 1 | 67 | | return entity; |
| | 2 | 68 | | }); |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | public Task<DataOutput<IEnumerable<T>>> UpdateRangeAsync(IEnumerable<T> entities, CancellationToken ct = default) => |
| | 0 | 72 | | GuardedAsync<IEnumerable<T>>(async () => |
| | 0 | 73 | | { |
| | 0 | 74 | | var list = entities.ToList(); |
| | 0 | 75 | | Set.UpdateRange(list); |
| | 0 | 76 | | await context.SaveChangesAsync(ct); |
| | 0 | 77 | | |
| | 0 | 78 | | return list; |
| | 0 | 79 | | }); |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | | 82 | | public Task<DataOutput<long>> DeleteAsync(T entity, CancellationToken ct = default) => |
| | 1 | 83 | | GuardedAsync(async () => |
| | 1 | 84 | | { |
| | 1 | 85 | | Set.Remove(entity); |
| | 1 | 86 | | await context.SaveChangesAsync(ct); |
| | 1 | 87 | | |
| | 1 | 88 | | return entity.Id; |
| | 2 | 89 | | }); |
| | | 90 | | |
| | | 91 | | /// <inheritdoc /> |
| | | 92 | | public Task<DataOutput<IEnumerable<long>>> DeleteRangeAsync(IEnumerable<long> ids, CancellationToken ct = default) = |
| | 1 | 93 | | GuardedAsync<IEnumerable<long>>(async () => |
| | 1 | 94 | | { |
| | 1 | 95 | | var idList = ids.ToList(); |
| | 1 | 96 | | var matches = await Set.Where(e => idList.Contains(e.Id)).ToListAsync(ct); |
| | 1 | 97 | | Set.RemoveRange(matches); |
| | 1 | 98 | | await context.SaveChangesAsync(ct); |
| | 1 | 99 | | |
| | 3 | 100 | | return matches.Select(e => e.Id).ToList(); |
| | 2 | 101 | | }); |
| | | 102 | | |
| | | 103 | | /// <inheritdoc cref="Query" /> |
| | 1 | 104 | | public IQueryable<T> Query() => Set.AsQueryable(); |
| | | 105 | | |
| | | 106 | | /// <inheritdoc /> |
| | | 107 | | public DataOutput<IEnumerable<T>> GetAll() => |
| | 10 | 108 | | Guarded(IEnumerable<T> () => Set.ToList()); |
| | | 109 | | |
| | | 110 | | /// <inheritdoc /> |
| | | 111 | | public DataOutput<T?> GetById(long id) => |
| | 10 | 112 | | Guarded(() => Set.FirstOrDefault(e => e.Id == id)); |
| | | 113 | | |
| | | 114 | | /// <inheritdoc /> |
| | 8 | 115 | | public DataOutput<long> Create(T entity) => Guarded(() => |
| | 8 | 116 | | { |
| | 8 | 117 | | Set.Add(entity); |
| | 8 | 118 | | context.SaveChanges(); |
| | 8 | 119 | | return entity.Id; |
| | 16 | 120 | | }); |
| | | 121 | | |
| | | 122 | | /// <inheritdoc /> |
| | 3 | 123 | | public DataOutput<IEnumerable<long>> CreateRange(IEnumerable<T> entities) => Guarded(IEnumerable<long> () => |
| | 3 | 124 | | { |
| | 3 | 125 | | var list = entities.ToList(); |
| | 3 | 126 | | Set.AddRange(list); |
| | 3 | 127 | | context.SaveChanges(); |
| | 9 | 128 | | return list.Select(e => e.Id).ToList(); |
| | 6 | 129 | | }); |
| | | 130 | | |
| | | 131 | | /// <inheritdoc /> |
| | 3 | 132 | | public DataOutput<T> Update(T entity) => Guarded(() => |
| | 3 | 133 | | { |
| | 3 | 134 | | Set.Update(entity); |
| | 3 | 135 | | context.SaveChanges(); |
| | 2 | 136 | | return entity; |
| | 5 | 137 | | }); |
| | | 138 | | |
| | | 139 | | /// <inheritdoc /> |
| | 0 | 140 | | public DataOutput<IEnumerable<T>> UpdateRange(IEnumerable<T> entities) => Guarded(IEnumerable<T> () => |
| | 0 | 141 | | { |
| | 0 | 142 | | var list = entities.ToList(); |
| | 0 | 143 | | Set.UpdateRange(list); |
| | 0 | 144 | | context.SaveChanges(); |
| | 0 | 145 | | return list; |
| | 0 | 146 | | }); |
| | | 147 | | |
| | | 148 | | /// <inheritdoc /> |
| | 1 | 149 | | public DataOutput<long> Delete(T entity) => Guarded(() => |
| | 1 | 150 | | { |
| | 1 | 151 | | Set.Remove(entity); |
| | 1 | 152 | | context.SaveChanges(); |
| | 1 | 153 | | return entity.Id; |
| | 2 | 154 | | }); |
| | | 155 | | |
| | | 156 | | /// <inheritdoc /> |
| | 1 | 157 | | public DataOutput<IEnumerable<long>> DeleteRange(IEnumerable<long> ids) => Guarded(IEnumerable<long> () => |
| | 1 | 158 | | { |
| | 1 | 159 | | var idList = ids.ToList(); |
| | 1 | 160 | | var matches = Set.Where(e => idList.Contains(e.Id)).ToList(); |
| | 1 | 161 | | Set.RemoveRange(matches); |
| | 1 | 162 | | context.SaveChanges(); |
| | 3 | 163 | | return matches.Select(e => e.Id).ToList(); |
| | 2 | 164 | | }); |
| | | 165 | | |
| | | 166 | | /// <summary>Maps an exception caught by a guard to an error envelope.</summary> |
| | 2 | 167 | | private static DataOutput<TResult> Fail<TResult>(Exception ex) => ex switch |
| | 2 | 168 | | { |
| | 1 | 169 | | DbUpdateConcurrencyException => DataOutput<TResult>.New.WithError(ConcurrencyMessage), |
| | 0 | 170 | | DbUpdateException => DataOutput<TResult>.New.WithError($"{PersistenceMessage} {ex.GetBaseException().Message}"), |
| | 0 | 171 | | DbException => DataOutput<TResult>.New.WithError($"{PersistenceMessage} {ex.Message}"), |
| | 1 | 172 | | _ => DataOutput<TResult>.New.WithError($"{PersistenceMessage} {ex.GetBaseException().Message}") |
| | 2 | 173 | | }; |
| | | 174 | | |
| | | 175 | | /// <summary>Runs a synchronous data operation, converting failures to envelope errors.</summary> |
| | | 176 | | protected static DataOutput<TResult> Guarded<TResult>(Func<TResult> operation) |
| | 26 | 177 | | { |
| | | 178 | | try |
| | 26 | 179 | | { |
| | 26 | 180 | | return DataOutput<TResult>.New.WithData(operation()); |
| | | 181 | | } |
| | 0 | 182 | | catch (OperationCanceledException) |
| | 0 | 183 | | { |
| | 0 | 184 | | throw; |
| | | 185 | | } |
| | 2 | 186 | | catch (Exception ex) |
| | 2 | 187 | | { |
| | 2 | 188 | | return Fail<TResult>(ex); |
| | | 189 | | } |
| | 26 | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary>Runs an asynchronous data operation, converting failures to envelope errors.</summary> |
| | | 193 | | protected static async Task<DataOutput<TResult>> GuardedAsync<TResult>(Func<Task<TResult>> operation) |
| | 15 | 194 | | { |
| | | 195 | | try |
| | 15 | 196 | | { |
| | 15 | 197 | | return DataOutput<TResult>.New.WithData(await operation()); |
| | | 198 | | } |
| | 0 | 199 | | catch (OperationCanceledException) |
| | 0 | 200 | | { |
| | 0 | 201 | | throw; |
| | | 202 | | } |
| | 0 | 203 | | catch (Exception ex) |
| | 0 | 204 | | { |
| | 0 | 205 | | return Fail<TResult>(ex); |
| | | 206 | | } |
| | 15 | 207 | | } |
| | | 208 | | } |