From 81ea79b64276f4650afa727cf4ff9514db18f31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Armatys?= Date: Sat, 21 Sep 2024 16:47:12 +0200 Subject: [PATCH] 116: Added database 'EventInterceptors' --- ...SSISTENTE.Persistence.Configuration.csproj | 3 +- .../AssistenteDbContext.cs | 36 ++--------------- .../DependencyInjection.cs | 13 ++++-- .../Interceptors/EventInterceptor.cs | 40 +++++++++++++++++++ .../ASSISTENTE.Persistence.MSSQL.csproj | 17 -------- .../DependencyInjection.cs | 21 ---------- .../ASSISTENTE.Persistence.POSTGRESQL.csproj | 17 -------- .../DependencyInjection.cs | 16 -------- API/ASSISTENTE.sln | 14 ------- 9 files changed, 54 insertions(+), 123 deletions(-) create mode 100644 API/ASSISTENTE.Persistence.Configuration/Interceptors/EventInterceptor.cs delete mode 100644 API/ASSISTENTE.Persistence.MSSQL/ASSISTENTE.Persistence.MSSQL.csproj delete mode 100644 API/ASSISTENTE.Persistence.MSSQL/DependencyInjection.cs delete mode 100644 API/ASSISTENTE.Persistence.POSTGRESQL/ASSISTENTE.Persistence.POSTGRESQL.csproj delete mode 100644 API/ASSISTENTE.Persistence.POSTGRESQL/DependencyInjection.cs diff --git a/API/ASSISTENTE.Persistence.Configuration/ASSISTENTE.Persistence.Configuration.csproj b/API/ASSISTENTE.Persistence.Configuration/ASSISTENTE.Persistence.Configuration.csproj index 7abd37a..a004bbe 100644 --- a/API/ASSISTENTE.Persistence.Configuration/ASSISTENTE.Persistence.Configuration.csproj +++ b/API/ASSISTENTE.Persistence.Configuration/ASSISTENTE.Persistence.Configuration.csproj @@ -7,6 +7,7 @@ + @@ -21,8 +22,6 @@ - - diff --git a/API/ASSISTENTE.Persistence.Configuration/AssistenteDbContext.cs b/API/ASSISTENTE.Persistence.Configuration/AssistenteDbContext.cs index cf69a7b..bbefa5e 100644 --- a/API/ASSISTENTE.Persistence.Configuration/AssistenteDbContext.cs +++ b/API/ASSISTENTE.Persistence.Configuration/AssistenteDbContext.cs @@ -23,8 +23,6 @@ public class AssistenteDbContext : DbContext, IAssistenteDbContext { private readonly IUserResolver? _userResolver; private readonly ISystemTimeProvider? _systemTimeProvider; - private readonly IPublisher? _publisher; - private readonly ILogger? _logger; public AssistenteDbContext(DbContextOptions options) : base(options) { @@ -33,15 +31,11 @@ public AssistenteDbContext(DbContextOptions options) : base public AssistenteDbContext( DbContextOptions options, IUserResolver userResolver, - ISystemTimeProvider systemTimeProvider, - IPublisher publisher, - ILogger 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 @@ -100,34 +94,10 @@ public override async Task 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() - .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 Set() where TEntity : class, IEntity { return base.Set(); diff --git a/API/ASSISTENTE.Persistence.Configuration/DependencyInjection.cs b/API/ASSISTENTE.Persistence.Configuration/DependencyInjection.cs index f137777..e2a4293 100644 --- a/API/ASSISTENTE.Persistence.Configuration/DependencyInjection.cs +++ b/API/ASSISTENTE.Persistence.Configuration/DependencyInjection.cs @@ -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; @@ -12,12 +13,18 @@ internal static class DependencyInjection public static IServiceCollection AddConfiguration(this IServiceCollection services) where TSettings : IDatabaseSettings { + services.AddScoped(); + var settings = services.GetSettings(x => x.Database); services.AddScoped(); - services.AddPostreSql(settings.ConnectionString); - + services.AddDbContext((serviceProvider, options) => options.UseNpgsql( + settings.ConnectionString + ) + .AddInterceptors(serviceProvider.GetRequiredService()) + ); + services.AddCommonHealthCheck(); return services; diff --git a/API/ASSISTENTE.Persistence.Configuration/Interceptors/EventInterceptor.cs b/API/ASSISTENTE.Persistence.Configuration/Interceptors/EventInterceptor.cs new file mode 100644 index 0000000..f9b5650 --- /dev/null +++ b/API/ASSISTENTE.Persistence.Configuration/Interceptors/EventInterceptor.cs @@ -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 logger) : SaveChangesInterceptor +{ + public override async ValueTask 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() + .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; + } +} \ No newline at end of file diff --git a/API/ASSISTENTE.Persistence.MSSQL/ASSISTENTE.Persistence.MSSQL.csproj b/API/ASSISTENTE.Persistence.MSSQL/ASSISTENTE.Persistence.MSSQL.csproj deleted file mode 100644 index b816b07..0000000 --- a/API/ASSISTENTE.Persistence.MSSQL/ASSISTENTE.Persistence.MSSQL.csproj +++ /dev/null @@ -1,17 +0,0 @@ - - - - net8.0 - enable - enable - - - - - - - - - - - diff --git a/API/ASSISTENTE.Persistence.MSSQL/DependencyInjection.cs b/API/ASSISTENTE.Persistence.MSSQL/DependencyInjection.cs deleted file mode 100644 index 8907bd9..0000000 --- a/API/ASSISTENTE.Persistence.MSSQL/DependencyInjection.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; - -namespace ASSISTENTE.Persistence.MSSQL -{ - internal static class DependencyInjection - { - public static IServiceCollection AddMssql( - this IServiceCollection services, - IConfiguration configuration) - where TContext : DbContext - { - var connectionString = configuration.GetConnectionString("AssistenteDatabase"); - - services.AddDbContext(options => options.UseSqlServer(connectionString)); - - return services; - } - } -} \ No newline at end of file diff --git a/API/ASSISTENTE.Persistence.POSTGRESQL/ASSISTENTE.Persistence.POSTGRESQL.csproj b/API/ASSISTENTE.Persistence.POSTGRESQL/ASSISTENTE.Persistence.POSTGRESQL.csproj deleted file mode 100644 index a97d05d..0000000 --- a/API/ASSISTENTE.Persistence.POSTGRESQL/ASSISTENTE.Persistence.POSTGRESQL.csproj +++ /dev/null @@ -1,17 +0,0 @@ - - - - net8.0 - enable - enable - - - - - - - - - - - diff --git a/API/ASSISTENTE.Persistence.POSTGRESQL/DependencyInjection.cs b/API/ASSISTENTE.Persistence.POSTGRESQL/DependencyInjection.cs deleted file mode 100644 index 754e037..0000000 --- a/API/ASSISTENTE.Persistence.POSTGRESQL/DependencyInjection.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; - -namespace ASSISTENTE.Persistence.POSTGRESQL -{ - internal static class DependencyInjection - { - public static IServiceCollection AddPostreSql(this IServiceCollection services, string connectionString) - where TContext : DbContext - { - services.AddDbContext(options => options.UseNpgsql(connectionString)); - - return services; - } - } -} \ No newline at end of file diff --git a/API/ASSISTENTE.sln b/API/ASSISTENTE.sln index 2b5a966..7a43764 100644 --- a/API/ASSISTENTE.sln +++ b/API/ASSISTENTE.sln @@ -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}" @@ -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}" @@ -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 @@ -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 @@ -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} @@ -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}