-
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 Idempotent domain event handler implementation for user module
- Loading branch information
Showing
24 changed files
with
779 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,4 @@ private static void ApplyMigrations<TDbContext>(IServiceScope scope) | |
|
||
context.Database.Migrate(); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/Evently/Common/Evently.Common.Application/Messaging/DomainEventHandler.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,14 @@ | ||
using Evently.Common.Domain; | ||
|
||
namespace Evently.Common.Application.Messaging; | ||
|
||
public abstract class DomainEventHandler<TDomainEvent> : IDomainEventHandler, IDomainEventHandler<TDomainEvent> | ||
where TDomainEvent : IDomainEvent | ||
{ | ||
public Task Handle(IDomainEvent domainEvent, CancellationToken cancellationToken = default) | ||
{ | ||
return Handle((TDomainEvent)domainEvent, cancellationToken); | ||
} | ||
|
||
public abstract Task Handle(TDomainEvent domainEvent, CancellationToken cancellationToken = default); | ||
} |
13 changes: 10 additions & 3 deletions
13
src/Evently/Common/Evently.Common.Application/Messaging/IDomainEventHandler.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 |
---|---|---|
@@ -1,7 +1,14 @@ | ||
using Evently.Common.Domain; | ||
using MediatR; | ||
|
||
namespace Evently.Common.Application.Messaging; | ||
|
||
public interface IDomainEventHandler<in TDomainEvent> : INotificationHandler<TDomainEvent> | ||
where TDomainEvent : IDomainEvent; | ||
public interface IDomainEventHandler | ||
{ | ||
Task Handle(IDomainEvent domainEvent, CancellationToken cancellationToken = default); | ||
} | ||
|
||
public interface IDomainEventHandler<in TDomainEvent> | ||
where TDomainEvent : IDomainEvent | ||
{ | ||
Task Handle(TDomainEvent domainEvent, CancellationToken cancellationToken = default); | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/Evently/Common/Evently.Common.Infrastructure/Outbox/DomainEventHandlersFactory.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,29 @@ | ||
using System.Collections.Concurrent; | ||
using System.Reflection; | ||
using Evently.Common.Application.Messaging; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Evently.Common.Infrastructure.Outbox; | ||
|
||
public static class DomainEventHandlersFactory | ||
{ | ||
private static readonly ConcurrentDictionary<string, Type[]> HandlersDictionary = new(); | ||
|
||
public static IEnumerable<IDomainEventHandler> GetHandlers( | ||
Type domainEventType, | ||
IServiceProvider serviceProvider, | ||
Assembly assembly) | ||
{ | ||
var domainEventHandlerTypes = HandlersDictionary.GetOrAdd( | ||
$"{assembly.GetName().Name}{domainEventType.Name}", | ||
_ => assembly.GetTypes() | ||
.Where(handlerType => handlerType.IsAssignableTo(typeof(IDomainEventHandler<>).MakeGenericType(domainEventType))) | ||
.ToArray()); | ||
|
||
return domainEventHandlerTypes | ||
.Select(serviceProvider.GetRequiredService) | ||
.Select(domainEventHandler => (domainEventHandler as IDomainEventHandler)!) | ||
.ToList(); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/Evently/Common/Evently.Common.Infrastructure/Outbox/OutboxMessageConsumer.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,7 @@ | ||
namespace Evently.Common.Infrastructure.Outbox; | ||
|
||
public sealed class OutboxMessageConsumer(Guid outboxMessageId, string name) | ||
{ | ||
public Guid OutboxMessageId { get; init; } = outboxMessageId; | ||
public string Name { get; init; } = name; | ||
} |
16 changes: 16 additions & 0 deletions
16
...Evently/Common/Evently.Common.Infrastructure/Outbox/OutboxMessageConsumerConfiguration.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,16 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace Evently.Common.Infrastructure.Outbox; | ||
|
||
public sealed class OutboxMessageConsumerConfiguration : IEntityTypeConfiguration<OutboxMessageConsumer> | ||
{ | ||
public void Configure(EntityTypeBuilder<OutboxMessageConsumer> builder) | ||
{ | ||
builder.ToTable("outbox_message_consumers"); | ||
|
||
builder.HasKey(outboxMessageConsumer => new { outboxMessageConsumer.OutboxMessageId, outboxMessageConsumer.Name }); | ||
|
||
builder.Property(outboxMessageConsumer => outboxMessageConsumer.OutboxMessageId).HasMaxLength(500); | ||
} | ||
} |
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
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
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.