-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee31874
commit 56b2f5b
Showing
25 changed files
with
184 additions
and
59 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
2 changes: 1 addition & 1 deletion
2
src/Simple.App/ExecutionContext.cs → src/Simple.App/Contracts/ExecutionContext.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
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,6 @@ | ||
namespace Simple.App.Contracts; | ||
|
||
public interface IDomainEventHandler<in T> where T: IDomainEvent | ||
{ | ||
Task Handle(T domainEvent); | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/Simple.App/Tenants/DomainEvents/TenantCreatedHandler.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,13 @@ | ||
using Simple.App.Contracts; | ||
using Simple.Domain.Tenants.DomainEvents; | ||
|
||
namespace Simple.App.Tenants.DomainEvents; | ||
|
||
public class TenantCreatedHandler(ILogger<TenantCreatedHandler> log) : IDomainEventHandler<TenantCreatedEvent> | ||
{ | ||
public Task Handle(TenantCreatedEvent domainEvent) | ||
{ | ||
log.LogInformation("Tenant Created: {Tenant}", domainEvent.Tenant); | ||
return Task.CompletedTask; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Simple.Domain; | ||
|
||
public interface IEntity | ||
{ | ||
IEnumerable<IDomainEvent> DomainEvents { get; } | ||
void AddDomainEvent(IDomainEvent eventItem); | ||
void ClearDomainEvents(); | ||
} | ||
|
||
public abstract class Entity : IEntity | ||
{ | ||
private readonly List<IDomainEvent> _domainEvents = []; | ||
|
||
public IEnumerable<IDomainEvent> DomainEvents => _domainEvents; | ||
|
||
public void AddDomainEvent(IDomainEvent eventItem) | ||
{ | ||
_domainEvents.Add(eventItem); | ||
} | ||
|
||
public void ClearDomainEvents() | ||
{ | ||
_domainEvents.Clear(); | ||
} | ||
} |
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,5 +1,3 @@ | ||
namespace Simple.Domain; | ||
|
||
public interface IAggregateRoot | ||
{ | ||
} | ||
public interface IAggregateRoot; |
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,8 @@ | ||
using MediatR; | ||
|
||
namespace Simple.Domain; | ||
|
||
/// <summary> | ||
/// https://learn.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/domain-events-design-implementation | ||
/// </summary> | ||
public interface IDomainEvent : INotification; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Simple.Domain.Tenants.DomainEvents; | ||
|
||
public class TenantCreatedEvent(Tenant tenant) : IDomainEvent | ||
{ | ||
public Tenant Tenant { get; } = tenant; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Collections.Immutable; | ||
using Simple.Infra.Database; | ||
|
||
namespace Simple.Infra.DomainEvents; | ||
|
||
public interface IDomainEventDispatcher | ||
{ | ||
Task Publish(); | ||
} | ||
|
||
internal class DomainEventDispatcher(Db db, IPublisher mediator, ILogger<DomainEventDispatcher> log) : IDomainEventDispatcher | ||
{ | ||
public async Task Publish() | ||
{ | ||
var entities = db.ChangeTracker.Entries<Entity>() | ||
.Select(po => po.Entity) | ||
.Where(po => po.DomainEvents.Any()) | ||
.ToArray(); | ||
|
||
foreach (var entity in entities) | ||
{ | ||
log.LogDebug($"Publishing domain events from {entity.GetType().Name}"); | ||
foreach (var domainEvent in entity.DomainEvents) | ||
{ | ||
log.LogDebug($"Publishing domain event {domainEvent.GetType().Name}"); | ||
await mediator.Publish(domainEvent); | ||
} | ||
entity.ClearDomainEvents(); | ||
} | ||
} | ||
} |
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.