mirror of
https://github.com/farcasclaudiu/PracticeCalendar.git
synced 2026-06-22 07:01:16 +03:00
refactoring and integration tests
This commit is contained in:
@@ -5,6 +5,7 @@ using PracticeCalendar.Domain.Common.Interfaces;
|
||||
using PracticeCalendar.Domain.Interfaces;
|
||||
using PracticeCalendar.Infrastructure.Notification;
|
||||
using PracticeCalendar.Infrastructure.Persistence;
|
||||
using PracticeCalendar.Infrastructure.Services;
|
||||
|
||||
namespace PracticeCalendar.Infrastructure
|
||||
{
|
||||
@@ -17,6 +18,7 @@ namespace PracticeCalendar.Infrastructure
|
||||
|
||||
services.AddDbContext(connectionString);
|
||||
services.AddTransient<IEmailSender, FileEmailSender>();
|
||||
services.AddTransient<IDomainEventService, DomainEventService>();
|
||||
|
||||
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PracticeCalendar.Domain.Common;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
using PracticeCalendar.Domain.Entities;
|
||||
using System.Reflection;
|
||||
|
||||
@@ -6,9 +8,15 @@ namespace PracticeCalendar.Infrastructure.Persistence
|
||||
{
|
||||
public class ApplicationDbContext : DbContext
|
||||
{
|
||||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
|
||||
private readonly IDomainEventService domainEventService;
|
||||
|
||||
public DbSet<Attendee> Atendees => Set<Attendee>();
|
||||
public DbSet<PracticeEvent> PracticeEvents => Set<PracticeEvent>();
|
||||
|
||||
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IDomainEventService domainEventService)
|
||||
: base(options)
|
||||
{
|
||||
this.domainEventService = domainEventService;
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
@@ -17,7 +25,30 @@ namespace PracticeCalendar.Infrastructure.Persistence
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
|
||||
}
|
||||
|
||||
public DbSet<Attendee> Atendees => Set<Attendee>();
|
||||
public DbSet<PracticeEvent> PracticeEvents => Set<PracticeEvent>();
|
||||
|
||||
|
||||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
var events = ChangeTracker.Entries<EntityBase>()
|
||||
.Select(x => x.Entity.DomainEvents)
|
||||
.SelectMany(x => x)
|
||||
.Where(domainEvent => !domainEvent.IsPublished)
|
||||
.ToArray();
|
||||
|
||||
var result = await base.SaveChangesAsync(cancellationToken);
|
||||
|
||||
await DispatchEvents(events);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task DispatchEvents(DomainEventBase[] events)
|
||||
{
|
||||
foreach (var @event in events)
|
||||
{
|
||||
@event.IsPublished = true;
|
||||
await domainEventService.Publish(@event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PracticeCalendar.Domain.Common;
|
||||
using PracticeCalendar.Domain.Common.Interfaces;
|
||||
|
||||
namespace PracticeCalendar.Infrastructure.Services
|
||||
{
|
||||
public class DomainEventService : IDomainEventService
|
||||
{
|
||||
private readonly ILogger<DomainEventService> logger;
|
||||
private readonly IPublisher mediator;
|
||||
|
||||
public DomainEventService(ILogger<DomainEventService> logger, IPublisher mediator)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.mediator = mediator;
|
||||
}
|
||||
public async Task Publish(DomainEventBase domainEvent)
|
||||
{
|
||||
logger.LogInformation("Publishing domain event. Event - {event}", domainEvent.GetType().Name);
|
||||
await mediator.Publish(GetNotificationCorrespondingToDomainEvent(domainEvent));
|
||||
}
|
||||
|
||||
private INotification GetNotificationCorrespondingToDomainEvent(DomainEventBase domainEvent)
|
||||
{
|
||||
return (INotification)Activator.CreateInstance(
|
||||
typeof(DomainEventNotification<>).MakeGenericType(domainEvent.GetType()), domainEvent)!;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user