| | 1 | | using StronglyTypedIds; |
| | 2 | | using System.Diagnostics; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Events; |
| | 5 | |
|
| | 6 | | public class DomainEvent |
| | 7 | | { |
| 5044 | 8 | | public DomainEventId Id { get; set; } |
| 5044 | 9 | | public Guid InstitutionId { get; set; } |
| 2530 | 10 | | public Guid EntityId { get; set; } |
| 15110 | 11 | | public string Type { get; set; } |
| 5044 | 12 | | public string Data { get; set; } |
| 2514 | 13 | | public DomainEventStatus Status { get; set; } |
| 2530 | 14 | | public DateTime OccurredAt { get; set; } |
| 2514 | 15 | | public DateTime? ProcessedAt { get; set; } |
| 0 | 16 | | public Guid? ProcessorId { get; set; } |
| 2514 | 17 | | public string? Error { get; set; } |
| 2514 | 18 | | public int Duration { get; set; } |
| 5044 | 19 | | public string? ActivityId { get; set; } |
| | 20 | |
|
| 5048 | 21 | | public DomainEvent() { } |
| | 22 | |
|
| 2530 | 23 | | public DomainEvent(Guid institutionId, Guid entityId, object data, string? activityId) |
| | 24 | | { |
| 2530 | 25 | | Id = DomainEventId.CreateNew(); |
| 2530 | 26 | | EntityId = entityId; |
| 2530 | 27 | | Type = data.GetType().ToString(); |
| 2530 | 28 | | Data = data.Serialize(); |
| 2530 | 29 | | OccurredAt = DateTime.UtcNow; |
| 2530 | 30 | | InstitutionId = institutionId; |
| 2530 | 31 | | ActivityId = activityId; |
| 2530 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Processed(double duration) |
| | 35 | | { |
| 2514 | 36 | | ProcessedAt = DateTime.UtcNow; |
| 2514 | 37 | | Duration = Convert.ToInt32(duration); |
| 2514 | 38 | | Status = Error.HasValue() ? DomainEventStatus.Error : DomainEventStatus.Success; |
| 2514 | 39 | | } |
| | 40 | |
|
| | 41 | | public ActivityContext GetParentContext() |
| | 42 | | { |
| 2514 | 43 | | ActivityContext.TryParse(ActivityId, null, out var parsedContext); |
| 2514 | 44 | | return parsedContext; |
| | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| | 48 | | [StronglyTypedId] |
| | 49 | | public partial struct DomainEventId |
| | 50 | | { |
| | 51 | | public static DomainEventId CreateNew() |
| | 52 | | { |
| | 53 | | return new DomainEventId(Guid.CreateVersion7()); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | public class DomainEventIdEfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverte |
| | 57 | | { |
| | 58 | | public DomainEventIdEfCoreValueConverter() : this(null) { } |
| | 59 | |
|
| | 60 | | public DomainEventIdEfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingH |
| | 61 | | : base( |
| | 62 | | id => id.Value, |
| | 63 | | value => new DomainEventId(value), |
| | 64 | | mappingHints |
| | 65 | | ) |
| | 66 | | { } |
| | 67 | | } |
| | 68 | | } |