-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add outbox between domain events and their publishing
- Loading branch information
Showing
35 changed files
with
650 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Evently/Common/Evently.Common.Infrastructure/Outbox/InsertOutboxMessagesInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Evently.Common.Domain; | ||
using Evently.Common.Infrastructure.Serialization; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Newtonsoft.Json; | ||
|
||
namespace Evently.Common.Infrastructure.Outbox; | ||
|
||
public sealed class InsertOutboxMessagesInterceptor : SaveChangesInterceptor | ||
{ | ||
public override async ValueTask<InterceptionResult<int>> SavingChangesAsync( | ||
DbContextEventData eventData, | ||
InterceptionResult<int> result, | ||
CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
if(eventData.Context is not null) | ||
InsertOutboxMessages(eventData.Context); | ||
|
||
return await base.SavingChangesAsync(eventData, result, cancellationToken); | ||
} | ||
private static void InsertOutboxMessages( | ||
DbContext context) | ||
{ | ||
var outboxMessages = context | ||
.ChangeTracker | ||
.Entries<Entity>() | ||
.Select(entry => entry.Entity) | ||
.SelectMany(entity => | ||
{ | ||
var domainEvents = entity.GetDomainEvents(); | ||
entity.ClearDomainEvents(); | ||
return domainEvents; | ||
}) | ||
.Select(domainEvent => new OutboxMessage | ||
{ | ||
Id = domainEvent.Id, | ||
Type = domainEvent.GetType().Name, | ||
Content = JsonConvert.SerializeObject(domainEvent, SerializerSettings.Instance), | ||
OccuredAtUtc = domainEvent.OccuredAtUtc, | ||
}) | ||
.ToList(); | ||
|
||
context.Set<OutboxMessage>().AddRange(outboxMessages); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Evently/Common/Evently.Common.Infrastructure/Outbox/OutboxMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Evently.Common.Infrastructure.Outbox; | ||
|
||
public sealed class OutboxMessage | ||
{ | ||
public Guid Id { get; init; } | ||
public string Type { get; init; } = string.Empty; | ||
public string Content { get; init; } = string.Empty; | ||
public DateTime OccuredAtUtc { get; init; } | ||
public DateTime? ProcessedAtUtc { get; init; } | ||
public string? Error { get; init; } | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Evently/Common/Evently.Common.Infrastructure/Outbox/OutboxMessageConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace Evently.Common.Infrastructure.Outbox; | ||
|
||
public sealed class OutboxMessageConfiguration : IEntityTypeConfiguration<OutboxMessage> | ||
{ | ||
public void Configure(EntityTypeBuilder<OutboxMessage> builder) | ||
{ | ||
builder.ToTable("outbox_messages"); | ||
|
||
builder.HasKey(outboxMessage => outboxMessage.Id); | ||
|
||
builder.Property(outBoxMessage => outBoxMessage.Content) | ||
.HasMaxLength(3000) | ||
.HasColumnType("jsonb"); | ||
} | ||
} |
46 changes: 0 additions & 46 deletions
46
src/Evently/Common/Evently.Common.Infrastructure/Outbox/PublishDomainEventsInterceptor.cs
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/Evently/Common/Evently.Common.Infrastructure/Serialization/SerializerSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Evently.Common.Infrastructure.Serialization; | ||
|
||
public static class SerializerSettings | ||
{ | ||
public static readonly JsonSerializerSettings Instance = new() | ||
{ | ||
TypeNameHandling = TypeNameHandling.All, | ||
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead, | ||
}; | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Evently/Modules/Attendance/Evently.Modules.Attendance.Domain/Attendees/Attendee.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Evently/Modules/Attendance/Evently.Modules.Attendance.Domain/Events/Event.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/Evently/Modules/Attendance/Evently.Modules.Attendance.Domain/Tickets/Ticket.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 37 additions & 1 deletion
38
...ations/20240527211028_Initial.Designer.cs → ...ations/20240527213228_Initial.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.