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