| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using AsiBackbone.Core.Audit; |
| | | 4 | | using AsiBackbone.EntityFrameworkCore.Persistence; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | namespace AsiBackbone.EntityFrameworkCore.Audit; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Entity Framework Core-backed audit residue lifecycle store that persists records through a host-owned <see cref="DbC |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// This store appends provider-neutral lifecycle events and intentionally relies on the host application to expose the |
| | | 14 | | /// </remarks> |
| | | 15 | | public sealed class EfCoreAuditResidueLifecycleStore : IAsiBackboneAuditResidueLifecycleStore |
| | | 16 | | { |
| | 2 | 17 | | private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); |
| | | 18 | | |
| | | 19 | | private readonly DbContext dbContext; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initializes a new instance of the <see cref="EfCoreAuditResidueLifecycleStore" /> class. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="dbContext">The host-owned database context.</param> |
| | 36 | 25 | | public EfCoreAuditResidueLifecycleStore(DbContext dbContext) |
| | | 26 | | { |
| | 36 | 27 | | ArgumentNullException.ThrowIfNull(dbContext); |
| | | 28 | | |
| | 36 | 29 | | this.dbContext = dbContext; |
| | 36 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public async ValueTask<AuditResidueLifecycleEvent> AppendAsync( |
| | | 34 | | AuditResidueLifecycleEvent lifecycleEvent, |
| | | 35 | | CancellationToken cancellationToken = default) |
| | | 36 | | { |
| | 38 | 37 | | ArgumentNullException.ThrowIfNull(lifecycleEvent); |
| | 38 | 38 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 39 | | |
| | 38 | 40 | | _ = await dbContext |
| | 38 | 41 | | .Set<AsiBackboneAuditResidueLifecycleEventEntity>() |
| | 38 | 42 | | .AddAsync(ToEntity(lifecycleEvent), cancellationToken) |
| | 38 | 43 | | .ConfigureAwait(false); |
| | | 44 | | |
| | 38 | 45 | | _ = await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | | 46 | | |
| | 38 | 47 | | return lifecycleEvent; |
| | 38 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public async ValueTask<AuditResidueLifecycleEvent?> FindByEventIdAsync( |
| | | 52 | | string eventId, |
| | | 53 | | CancellationToken cancellationToken = default) |
| | | 54 | | { |
| | 2 | 55 | | ArgumentException.ThrowIfNullOrWhiteSpace(eventId); |
| | | 56 | | |
| | 2 | 57 | | string normalizedEventId = eventId.Trim(); |
| | | 58 | | |
| | 2 | 59 | | AsiBackboneAuditResidueLifecycleEventEntity? entity = await LifecycleEvents() |
| | 2 | 60 | | .Where(lifecycleEvent => lifecycleEvent.EventId == normalizedEventId) |
| | 2 | 61 | | .SingleOrDefaultAsync(cancellationToken) |
| | 2 | 62 | | .ConfigureAwait(false); |
| | | 63 | | |
| | 2 | 64 | | return entity is null ? null : ToLifecycleEvent(entity); |
| | 2 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public async ValueTask<IReadOnlyList<AuditResidueLifecycleEvent>> FindByCorrelationIdAsync( |
| | | 69 | | string correlationId, |
| | | 70 | | CancellationToken cancellationToken = default) |
| | | 71 | | { |
| | 4 | 72 | | ArgumentException.ThrowIfNullOrWhiteSpace(correlationId); |
| | | 73 | | |
| | 4 | 74 | | string normalizedCorrelationId = correlationId.Trim(); |
| | | 75 | | |
| | 4 | 76 | | List<AsiBackboneAuditResidueLifecycleEventEntity> entities = await LifecycleEvents() |
| | 4 | 77 | | .Where(lifecycleEvent => lifecycleEvent.CorrelationId == normalizedCorrelationId) |
| | 4 | 78 | | .ToListAsync(cancellationToken) |
| | 4 | 79 | | .ConfigureAwait(false); |
| | | 80 | | |
| | 4 | 81 | | return [.. ToLifecycleEvents(entities) |
| | 36 | 82 | | .OrderBy(lifecycleEvent => lifecycleEvent.OccurredUtc) |
| | 40 | 83 | | .ThenBy(lifecycleEvent => lifecycleEvent.EventId, StringComparer.Ordinal)]; |
| | 4 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | | 87 | | public async ValueTask<IReadOnlyList<AuditResidueLifecycleEvent>> FindByAuditResidueIdAsync( |
| | | 88 | | string auditResidueId, |
| | | 89 | | CancellationToken cancellationToken = default) |
| | | 90 | | { |
| | 2 | 91 | | ArgumentException.ThrowIfNullOrWhiteSpace(auditResidueId); |
| | | 92 | | |
| | 2 | 93 | | string normalizedAuditResidueId = auditResidueId.Trim(); |
| | | 94 | | |
| | 2 | 95 | | List<AsiBackboneAuditResidueLifecycleEventEntity> entities = await LifecycleEvents() |
| | 2 | 96 | | .Where(lifecycleEvent => lifecycleEvent.AuditResidueId == normalizedAuditResidueId) |
| | 2 | 97 | | .ToListAsync(cancellationToken) |
| | 2 | 98 | | .ConfigureAwait(false); |
| | | 99 | | |
| | 2 | 100 | | return [.. ToLifecycleEvents(entities) |
| | 4 | 101 | | .OrderBy(lifecycleEvent => lifecycleEvent.OccurredUtc) |
| | 6 | 102 | | .ThenBy(lifecycleEvent => lifecycleEvent.EventId, StringComparer.Ordinal)]; |
| | 2 | 103 | | } |
| | | 104 | | |
| | | 105 | | private IQueryable<AsiBackboneAuditResidueLifecycleEventEntity> LifecycleEvents() |
| | | 106 | | { |
| | 8 | 107 | | return dbContext.Set<AsiBackboneAuditResidueLifecycleEventEntity>().AsNoTracking(); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | private static AsiBackboneAuditResidueLifecycleEventEntity ToEntity(AuditResidueLifecycleEvent lifecycleEvent) |
| | | 111 | | { |
| | 38 | 112 | | return new AsiBackboneAuditResidueLifecycleEventEntity |
| | 38 | 113 | | { |
| | 38 | 114 | | EventId = lifecycleEvent.EventId, |
| | 38 | 115 | | Stage = lifecycleEvent.Stage, |
| | 38 | 116 | | StageSequence = lifecycleEvent.StageSequence, |
| | 38 | 117 | | OccurredUtc = lifecycleEvent.OccurredUtc, |
| | 38 | 118 | | CorrelationId = lifecycleEvent.CorrelationId, |
| | 38 | 119 | | AuditResidueId = lifecycleEvent.AuditResidueId, |
| | 38 | 120 | | TraceId = lifecycleEvent.TraceId, |
| | 38 | 121 | | OperationName = lifecycleEvent.OperationName, |
| | 38 | 122 | | Outcome = lifecycleEvent.Outcome, |
| | 38 | 123 | | MetadataJson = JsonSerializer.Serialize(lifecycleEvent.Metadata, JsonOptions) |
| | 38 | 124 | | }; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | private static AuditResidueLifecycleEvent[] ToLifecycleEvents(IEnumerable<AsiBackboneAuditResidueLifecycleEventEntit |
| | | 128 | | { |
| | 6 | 129 | | return [.. entities.Select(ToLifecycleEvent)]; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | private static AuditResidueLifecycleEvent ToLifecycleEvent(AsiBackboneAuditResidueLifecycleEventEntity entity) |
| | | 133 | | { |
| | 42 | 134 | | return AuditResidueLifecycleEvent.Create( |
| | 42 | 135 | | entity.Stage, |
| | 42 | 136 | | entity.CorrelationId, |
| | 42 | 137 | | entity.AuditResidueId, |
| | 42 | 138 | | entity.EventId, |
| | 42 | 139 | | entity.OccurredUtc, |
| | 42 | 140 | | entity.TraceId, |
| | 42 | 141 | | entity.OperationName, |
| | 42 | 142 | | entity.Outcome, |
| | 42 | 143 | | DeserializeMetadata(entity.MetadataJson)); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | private static ReadOnlyDictionary<string, string> DeserializeMetadata(string? json) |
| | | 147 | | { |
| | 42 | 148 | | if (string.IsNullOrWhiteSpace(json)) |
| | | 149 | | { |
| | 0 | 150 | | return EmptyMetadata(); |
| | | 151 | | } |
| | | 152 | | |
| | 42 | 153 | | Dictionary<string, string>? metadata = JsonSerializer.Deserialize<Dictionary<string, string>>(json, JsonOptions) |
| | | 154 | | |
| | 42 | 155 | | return metadata is null || metadata.Count == 0 |
| | 42 | 156 | | ? EmptyMetadata() |
| | 42 | 157 | | : new ReadOnlyDictionary<string, string>(new Dictionary<string, string>(metadata, StringComparer.Ordinal)); |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | private static ReadOnlyDictionary<string, string> EmptyMetadata() |
| | | 161 | | { |
| | 0 | 162 | | return new ReadOnlyDictionary<string, string>(new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 163 | | } |
| | | 164 | | } |