Skip to content

Commit

Permalink
[PATRO23-58] Add IDomainEventHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
IzabelaPalubicka committed Mar 13, 2023
1 parent 0ed0f78 commit bf438a3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/api/app/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Intive.Patronage2023.Modules.Example.Api;
using Microsoft.OpenApi.Models;
using Intive.Patronage2023.Shared.Infrastructure;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);



builder.Services.AddEndpointsApiExplorer();

builder.Services.AddExampleModule();
Expand All @@ -14,7 +12,6 @@

builder.Services.AddMediatR(cfg =>
{
cfg.Lifetime = ServiceLifetime.Singleton;
cfg.RegisterServicesFromAssembly(typeof(Program).Assembly);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Diagnostics;
using Intive.Patronage2023.Modules.Example.Contracts.Events;
using MediatR;
using Intive.Patronage2023.Shared.Infrastructure.EventHandlers;

namespace Intive.Patronage2023.Modules.Example.Infrastructure.Domain.EventHandlers
{
/// <summary>
/// Example created domain event handler.
/// </summary>
public class ExampleCreatedDomainEventHandler : INotificationHandler<ExampleCreatedDomainEvent>
public class ExampleCreatedDomainEventHandler : IDomainEventHandler<ExampleCreatedDomainEvent>
{
/// <summary>
/// Handle the notification.
Expand All @@ -17,8 +16,9 @@ public class ExampleCreatedDomainEventHandler : INotificationHandler<ExampleCrea
/// <returns>Task.</returns>
public Task Handle(ExampleCreatedDomainEvent notification, CancellationToken cancellationToken)
{
Debug.WriteLine($"Example create domian event. {nameof(ExampleCreatedDomainEventHandler)}");
cancellationToken.ThrowIfCancellationRequested();

// TODO: Use logger
return Task.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System.Diagnostics;
using Intive.Patronage2023.Modules.Example.Contracts.Events;
using MediatR;
using Intive.Patronage2023.Shared.Infrastructure.EventHandlers;

namespace Intive.Patronage2023.Modules.Example.Infrastructure.Domain.EventHandlers
{
/// <summary>
/// Example name updated domain event handler.
/// </summary>
public class ExampleNameUpdatedDomainEventHandler : INotificationHandler<ExampleNameUpdatedDomainEvent>
public class ExampleNameUpdatedDomainEventHandler : IDomainEventHandler<ExampleNameUpdatedDomainEvent>
{
/// <summary>
/// Handle the notification.
Expand All @@ -18,7 +17,8 @@ public class ExampleNameUpdatedDomainEventHandler : INotificationHandler<Example
public Task Handle(ExampleNameUpdatedDomainEvent notification, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Debug.WriteLine($"Example create domian event. {nameof(ExampleNameUpdatedDomainEventHandler)}");

// TODO: Add logging using ILogger
return Task.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
namespace Intive.Patronage2023.Modules.Example.Infrastructure.Domain;

using Intive.Patronage2023.Modules.Example.Domain;
using Intive.Patronage2023.Shared.Abstractions.Events;
using Intive.Patronage2023.Shared.Infrastructure.EventDispachers;

/// <summary>
/// Example aggregate repository.
/// </summary>
public class ExampleRepository : IExampleRepository
{
private readonly DomainEventDispatcher domainEventDispatcher;
private readonly IEventDispatcher<IEvent> domainEventDispatcher;

/// <summary>
/// Initializes a new instance of the <see cref="ExampleRepository"/> class.
/// </summary>
/// <param name="domainEventDispatcher">Event dispatcher.</param>
public ExampleRepository(DomainEventDispatcher domainEventDispatcher)
public ExampleRepository(IEventDispatcher<IEvent> domainEventDispatcher)
{
this.domainEventDispatcher = domainEventDispatcher;
}
Expand Down
14 changes: 14 additions & 0 deletions src/shared/infrastructure/EventHandlers/IDomainEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Intive.Patronage2023.Shared.Abstractions.Events;
using MediatR;

namespace Intive.Patronage2023.Shared.Infrastructure.EventHandlers
{
/// <summary>
/// Domain event handler base interface.
/// </summary>
/// <typeparam name="TEvent">Event type.</typeparam>
public interface IDomainEventHandler<TEvent> : INotificationHandler<TEvent>
where TEvent : IEvent
{
}
}

0 comments on commit bf438a3

Please sign in to comment.