Skip to content

Commit

Permalink
116: Added database 'EventInterceptors'
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmatys committed Sep 21, 2024
1 parent 7c25f95 commit 81ea79b
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4"/>
<PackageReference Include="MediatR" Version="12.2.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4"/>
Expand All @@ -21,8 +22,6 @@

<ItemGroup>
<ProjectReference Include="..\ASSISTENTE.Domain\ASSISTENTE.Domain.csproj"/>
<ProjectReference Include="..\ASSISTENTE.Persistence.MSSQL\ASSISTENTE.Persistence.MSSQL.csproj"/>
<ProjectReference Include="..\ASSISTENTE.Persistence.POSTGRESQL\ASSISTENTE.Persistence.POSTGRESQL.csproj"/>
</ItemGroup>

<ItemGroup>
Expand Down
36 changes: 3 additions & 33 deletions API/ASSISTENTE.Persistence.Configuration/AssistenteDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public class AssistenteDbContext : DbContext, IAssistenteDbContext
{
private readonly IUserResolver? _userResolver;
private readonly ISystemTimeProvider? _systemTimeProvider;
private readonly IPublisher? _publisher;
private readonly ILogger<AssistenteDbContext>? _logger;

public AssistenteDbContext(DbContextOptions<AssistenteDbContext> options) : base(options)
{
Expand All @@ -33,15 +31,11 @@ public AssistenteDbContext(DbContextOptions<AssistenteDbContext> options) : base
public AssistenteDbContext(
DbContextOptions<AssistenteDbContext> options,
IUserResolver userResolver,
ISystemTimeProvider systemTimeProvider,
IPublisher publisher,
ILogger<AssistenteDbContext> logger)
ISystemTimeProvider systemTimeProvider)
: base(options)
{
_userResolver = userResolver ?? throw new ArgumentNullException(nameof(userResolver));
_systemTimeProvider = systemTimeProvider ?? throw new ArgumentNullException(nameof(systemTimeProvider));
_publisher = publisher ?? throw new ArgumentNullException(nameof(publisher));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

#region ENTITIES
Expand Down Expand Up @@ -100,34 +94,10 @@ public override async Task<int> SaveChangesAsync(CancellationToken cancellationT
}
}

var result = await base.SaveChangesAsync(cancellationToken);

await PublishEventsAsync();

return result;
return await base.SaveChangesAsync(cancellationToken);
}

private async Task PublishEventsAsync()
{
var domainEvents = ChangeTracker
.Entries<IEntity>()
.Select(entry => entry.Entity)
.SelectMany(entity =>
{
var events = entity.GetEvents();
entity.ClearEvents();
return events;
})
.ToList();

foreach (var domainEvent in domainEvents)
{
_logger!.LogInformation("Publishing domain event: {EventName}", domainEvent.GetType().Name);

await _publisher!.Publish(domainEvent);
}
}


public new DbSet<TEntity> Set<TEntity>() where TEntity : class, IEntity
{
return base.Set<TEntity>();
Expand Down
13 changes: 10 additions & 3 deletions API/ASSISTENTE.Persistence.Configuration/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ASSISTENTE.Persistence.Configuration.HealthChecks;
using ASSISTENTE.Persistence.Configuration.Interceptors;
using ASSISTENTE.Persistence.Configuration.Settings;
using ASSISTENTE.Persistence.POSTGRESQL;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using SOFTURE.Common.HealthCheck;
using SOFTURE.Settings.Extensions;
Expand All @@ -12,12 +13,18 @@ internal static class DependencyInjection
public static IServiceCollection AddConfiguration<TSettings>(this IServiceCollection services)
where TSettings : IDatabaseSettings
{
services.AddScoped<EventInterceptor>();

var settings = services.GetSettings<TSettings, DatabaseSettings>(x => x.Database);

services.AddScoped<IAssistenteDbContext, AssistenteDbContext>();

services.AddPostreSql<AssistenteDbContext>(settings.ConnectionString);

services.AddDbContext<AssistenteDbContext>((serviceProvider, options) => options.UseNpgsql(
settings.ConnectionString
)
.AddInterceptors(serviceProvider.GetRequiredService<EventInterceptor>())
);

services.AddCommonHealthCheck<DatabaseHealthCheck>();

return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using ASSISTENTE.Domain.Common.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;

namespace ASSISTENTE.Persistence.Configuration.Interceptors;

public class EventInterceptor(IPublisher publisher, ILogger<EventInterceptor> logger) : SaveChangesInterceptor
{
public override async ValueTask<int> SavedChangesAsync(
SaveChangesCompletedEventData eventData,
int result,
CancellationToken cancellationToken = default)
{
var savedChangesResult = await base.SavedChangesAsync(eventData, result, cancellationToken);

if (eventData.Context is null)
return savedChangesResult;

var domainEvents = eventData.Context.ChangeTracker
.Entries<IEntity>()
.Select(entry => entry.Entity)
.SelectMany(entity =>
{
var events = entity.GetEvents();
entity.ClearEvents();
return events;
})
.ToList();

foreach (var domainEvent in domainEvents)
{
logger.LogInformation("Publishing domain event: {EventName}", domainEvent.GetType().Name);

await publisher.Publish(domainEvent, cancellationToken);
}

return savedChangesResult;
}
}

This file was deleted.

21 changes: 0 additions & 21 deletions API/ASSISTENTE.Persistence.MSSQL/DependencyInjection.cs

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions API/ASSISTENTE.Persistence.POSTGRESQL/DependencyInjection.cs

This file was deleted.

14 changes: 0 additions & 14 deletions API/ASSISTENTE.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASSISTENTE.Infrastructure.Q
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Databases", "Databases", "{D501A620-C909-4367-9656-00D212EC1018}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASSISTENTE.Persistence.MSSQL", "ASSISTENTE.Persistence.MSSQL\ASSISTENTE.Persistence.MSSQL.csproj", "{F24D6783-B094-41B1-A8DE-8DD8260275E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASSISTENTE.DB.Upgrade", "ASSISTENTE.DB.Upgrade\ASSISTENTE.DB.Upgrade.csproj", "{91E78F5A-C226-4982-A178-6816956D8D54}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASSISTENTE.Infrastructure.PromptGenerator", "ASSISTENTE.Infrastructure.PromptGenerator\ASSISTENTE.Infrastructure.PromptGenerator.csproj", "{EAA9983F-DCC1-4DC5-9ACE-9C661B99C561}"
Expand Down Expand Up @@ -73,8 +71,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASSISTENTE.Application.Midd
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASSISTENTE.Persistence.Configuration", "ASSISTENTE.Persistence.Configuration\ASSISTENTE.Persistence.Configuration.csproj", "{0C920E22-63AC-4D7F-AC34-FBC461CF729C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASSISTENTE.Persistence.POSTGRESQL", "ASSISTENTE.Persistence.POSTGRESQL\ASSISTENTE.Persistence.POSTGRESQL.csproj", "{6DACA8BD-D540-4702-960E-CDA20DEA3A2E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{9B4F8CB3-29F8-40AC-AA0A-DD4B273EFF6C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Unit", "Unit", "{C38CC763-14E8-43EA-8D06-D7F9A2DE9FBD}"
Expand Down Expand Up @@ -140,10 +136,6 @@ Global
{528D414B-68A8-437D-BE2E-C1CB9CC22E4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{528D414B-68A8-437D-BE2E-C1CB9CC22E4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{528D414B-68A8-437D-BE2E-C1CB9CC22E4F}.Release|Any CPU.Build.0 = Release|Any CPU
{F24D6783-B094-41B1-A8DE-8DD8260275E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F24D6783-B094-41B1-A8DE-8DD8260275E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F24D6783-B094-41B1-A8DE-8DD8260275E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F24D6783-B094-41B1-A8DE-8DD8260275E2}.Release|Any CPU.Build.0 = Release|Any CPU
{91E78F5A-C226-4982-A178-6816956D8D54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{91E78F5A-C226-4982-A178-6816956D8D54}.Debug|Any CPU.Build.0 = Debug|Any CPU
{91E78F5A-C226-4982-A178-6816956D8D54}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -200,10 +192,6 @@ Global
{0C920E22-63AC-4D7F-AC34-FBC461CF729C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C920E22-63AC-4D7F-AC34-FBC461CF729C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C920E22-63AC-4D7F-AC34-FBC461CF729C}.Release|Any CPU.Build.0 = Release|Any CPU
{6DACA8BD-D540-4702-960E-CDA20DEA3A2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6DACA8BD-D540-4702-960E-CDA20DEA3A2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DACA8BD-D540-4702-960E-CDA20DEA3A2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DACA8BD-D540-4702-960E-CDA20DEA3A2E}.Release|Any CPU.Build.0 = Release|Any CPU
{E079A371-2133-4F57-9CA1-9AB02CD3590F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E079A371-2133-4F57-9CA1-9AB02CD3590F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E079A371-2133-4F57-9CA1-9AB02CD3590F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -235,7 +223,6 @@ Global
{56B52A7F-2834-4F11-892A-D9BE386C0833} = {450106FD-82CE-441D-93AD-10B7F1641A1A}
{528D414B-68A8-437D-BE2E-C1CB9CC22E4F} = {450106FD-82CE-441D-93AD-10B7F1641A1A}
{D501A620-C909-4367-9656-00D212EC1018} = {2B12AFDA-6C6C-4A60-BBEE-EA9FDA0BD664}
{F24D6783-B094-41B1-A8DE-8DD8260275E2} = {D501A620-C909-4367-9656-00D212EC1018}
{91E78F5A-C226-4982-A178-6816956D8D54} = {D501A620-C909-4367-9656-00D212EC1018}
{EAA9983F-DCC1-4DC5-9ACE-9C661B99C561} = {450106FD-82CE-441D-93AD-10B7F1641A1A}
{EB499850-7AF5-462D-B2A4-6F53911968BB} = {450106FD-82CE-441D-93AD-10B7F1641A1A}
Expand All @@ -252,7 +239,6 @@ Global
{0E7ED6C3-58DD-4E27-8237-0ABD91C45828} = {DC9028E5-75D2-445D-8D39-75011BFC4E10}
{28369BDF-507A-4997-B902-11300036044E} = {7E06C814-09F4-4431-AF9D-78F2E12FF0AC}
{0C920E22-63AC-4D7F-AC34-FBC461CF729C} = {D501A620-C909-4367-9656-00D212EC1018}
{6DACA8BD-D540-4702-960E-CDA20DEA3A2E} = {D501A620-C909-4367-9656-00D212EC1018}
{C38CC763-14E8-43EA-8D06-D7F9A2DE9FBD} = {9B4F8CB3-29F8-40AC-AA0A-DD4B273EFF6C}
{E079A371-2133-4F57-9CA1-9AB02CD3590F} = {C38CC763-14E8-43EA-8D06-D7F9A2DE9FBD}
{681CA443-1B91-4EA9-8A97-F21D0B5DD2CD} = {896896AD-0860-42EA-AC98-A4F5FAB926A4}
Expand Down

0 comments on commit 81ea79b

Please sign in to comment.