From 224d029c18fca89ecf74904ea6f4eacf9c93acbb Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 17 Nov 2026 20:09:23 +0100 Subject: [PATCH] Refactor DI and update dependencies - Enhanced `AddAutoMapper` configuration for flexibility. - Replaced `IEnvelopeMailService` with `IEnvelopeNotificationService`. - Migrated from `IUserRepository` to `EGDbContext` for data access. - Updated RabbitMQ configuration to use `AddMessagingServiceHttpClient`. - Removed `AddUserManager` and `AddDispatcher` registrations. - Added conditional `FluentValidation` dependencies for target frameworks. - Upgraded `MediatR` to 14.2.0 and `AutoMapper` to 16.2.0. - Added `HtmlSanitizer` dependency. - Improved error handling and logging in `ReadOnlyController`. --- .../DependencyInjection.cs | 6 ++-- .../EnvelopeGenerator.Application.csproj | 15 +++++--- .../DependencyInjection.cs | 11 +++--- .../Executor/EnvelopeExecutor.cs | 16 ++++----- .../Controllers/ReadOnlyController.cs | 34 ++++++++++--------- .../EnvelopeGenerator.Server/Program.cs | 10 ------ 6 files changed, 47 insertions(+), 45 deletions(-) diff --git a/EnvelopeGenerator.Application/DependencyInjection.cs b/EnvelopeGenerator.Application/DependencyInjection.cs index d8d086c1..06335c04 100644 --- a/EnvelopeGenerator.Application/DependencyInjection.cs +++ b/EnvelopeGenerator.Application/DependencyInjection.cs @@ -45,8 +45,10 @@ public static class DependencyInjection services.TryAddScoped(); //Auto mapping profiles - services.AddAutoMapper(Assembly.GetExecutingAssembly()); - services.AddAutoMapper(typeof(DigitalData.UserManager.Application.DIExtensions)); + services.AddAutoMapper( + _ => { }, + Assembly.GetExecutingAssembly(), + typeof(DigitalData.UserManager.Application.DIExtensions).Assembly); services.Configure(config.GetSection(nameof(DispatcherParams))); services.Configure(config.GetSection(nameof(MailParams))); diff --git a/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj b/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj index 8f6c7c7b..5777d0d3 100644 --- a/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj +++ b/EnvelopeGenerator.Application/EnvelopeGenerator.Application.csproj @@ -20,10 +20,9 @@ - - + - + @@ -36,14 +35,20 @@ + + + + + + @@ -89,14 +94,14 @@ - + 8.1.1 - + 8.1.1 diff --git a/EnvelopeGenerator.Infrastructure.Notification/DependencyInjection.cs b/EnvelopeGenerator.Infrastructure.Notification/DependencyInjection.cs index 5eb1ff0b..192c688d 100644 --- a/EnvelopeGenerator.Infrastructure.Notification/DependencyInjection.cs +++ b/EnvelopeGenerator.Infrastructure.Notification/DependencyInjection.cs @@ -31,12 +31,15 @@ public static class DependencyInjection $"MessagingServiceApiUrl must be provided when ClientType is set to Api in the '{sectionName}' configuration section."); break; case ConsumerType.RabbitMQ: - if(options.RabbitMQ is not null) - services.AddMessagingServiceClient(ntfConfiguration, nameof(options.RabbitMQ)); + if (options.ApiUrl is string rabbitModeApiUrl) + services.AddMessagingServiceHttpClient(opt => + { + opt.BaseAddress = new Uri(rabbitModeApiUrl); + }); else throw new InvalidOperationException( - $"Missing RabbitMQ connection configuration." + - $"When ClientType is set to RabbitMQ, a valid RabbitMQ configuration must be provided in the '{sectionName}' configuration section."); + $"Missing API URL configuration." + + $"When ClientType is set to RabbitMQ in an isolated host, Notification:ApiUrl must be provided in the '{sectionName}' configuration section."); break; default: throw new InvalidOperationException( diff --git a/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs b/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs index 5efc6826..24b74ba5 100644 --- a/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs +++ b/EnvelopeGenerator.Infrastructure/Executor/EnvelopeExecutor.cs @@ -1,10 +1,10 @@ #if NET using Dapper; -using DigitalData.UserManager.Application.Contracts.Repositories; using EnvelopeGenerator.Application.Common.Interfaces.SQLExecutor; using EnvelopeGenerator.Application.Common.SQL; using EnvelopeGenerator.Domain.Entities; using Microsoft.Data.SqlClient; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; @@ -12,13 +12,11 @@ namespace EnvelopeGenerator.Infrastructure.Executor; public class EnvelopeExecutor : SQLExecutor, IEnvelopeExecutor { - [Obsolete("Use IRepository")] - private readonly IUserRepository _userRepository; + private readonly EGDbContext _dbContext; - [Obsolete("Use IRepository")] - public EnvelopeExecutor(IServiceProvider provider, IOptions sqlExecutorParamsOptions, IUserRepository userRepository) : base(provider, sqlExecutorParamsOptions) + public EnvelopeExecutor(IServiceProvider provider, IOptions sqlExecutorParamsOptions, EGDbContext dbContext) : base(provider, sqlExecutorParamsOptions) { - _userRepository = userRepository; + _dbContext = dbContext; } [Obsolete("Use IRepository")] @@ -33,9 +31,11 @@ public class EnvelopeExecutor : SQLExecutor, IEnvelopeExecutor ?? throw new InvalidOperationException($"Envelope creation failed. Parameters:" + $"userId={userId}, title='{title}', message='{message}', tfaEnabled={tfaEnabled}."); - envelope.User = await _userRepository.ReadByIdAsync(envelope.UserId); + envelope.User = await _dbContext.Users + .AsNoTracking() + .FirstOrDefaultAsync(x => x.Id == envelope.UserId, cancellation); return envelope; } } -#endif \ No newline at end of file +#endif diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Controllers/ReadOnlyController.cs b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Controllers/ReadOnlyController.cs index f55d417e..5dd5ba0f 100644 --- a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Controllers/ReadOnlyController.cs +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Controllers/ReadOnlyController.cs @@ -1,6 +1,7 @@ using DigitalData.Core.Abstraction.Application.DTO; using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiverReadOnly; using EnvelopeGenerator.Application.Common.Interfaces.Services; +using EnvelopeGenerator.Application.Common.Interfaces.Services.Notification; using EnvelopeGenerator.Application.EnvelopeReceivers.Queries; using EnvelopeGenerator.Domain.Constants; using MediatR; @@ -20,17 +21,17 @@ public class ReadOnlyController : ControllerBase { private readonly ILogger _logger; private readonly IEnvelopeReceiverReadOnlyService _readOnlyService; - private readonly IEnvelopeMailService _mailService; + private readonly IEnvelopeNotificationService _notificationService; private readonly IEnvelopeHistoryService _historyService; /// /// Initializes a new instance of the class. /// - public ReadOnlyController(ILogger logger, IEnvelopeReceiverReadOnlyService readOnlyService, IEnvelopeMailService mailService, IEnvelopeHistoryService historyService) + public ReadOnlyController(ILogger logger, IEnvelopeReceiverReadOnlyService readOnlyService, IEnvelopeNotificationService notificationService, IEnvelopeHistoryService historyService) { _logger = logger; _readOnlyService = readOnlyService; - _mailService = mailService; + _notificationService = notificationService; _historyService = historyService; } @@ -84,22 +85,23 @@ public class ReadOnlyController : ControllerBase var newReadOnly = readRes.Data; - return await _mailService.SendAsync(newReadOnly).ThenAsync(SuccessAsync: async _ => + var notification = new DocumentSharedNotification { - var histRes = await _historyService.RecordAsync((int)createDto.EnvelopeId, createDto.AddedWho, EnvelopeStatus.EnvelopeShared); - if (histRes.IsFailed) - { - _logger.LogError("Although the envelope was sent as read-only, the EnvelopeShared history could not be saved. Create DTO:\n{createDto}", JsonConvert.SerializeObject(createDto)); - _logger.LogNotice(histRes.Notices); - } + DocumentTitle = newReadOnly.Envelope?.Title ?? string.Empty, + NameReceiver = newReadOnly.Receiver?.LastUsedName ?? string.Empty, + Origin = new Uri($"{Request.Scheme}://{Request.Host}"), + ReadOnlyId = newReadOnly.Id + }; - return Ok(); - }, + await _notificationService.Notify(newReadOnly.ReceiverMail, notification, cancel); - Fail: (msg, ntc) => + var histRes = await _historyService.RecordAsync((int)createDto.EnvelopeId, createDto.AddedWho, EnvelopeStatus.EnvelopeShared); + if (histRes.IsFailed) { - _logger.LogNotice(ntc); - return StatusCode(StatusCodes.Status500InternalServerError); - }); + _logger.LogError("Although the envelope was sent as read-only, the EnvelopeShared history could not be saved. Create DTO:\n{createDto}", JsonConvert.SerializeObject(createDto)); + _logger.LogNotice(histRes.Notices); + } + + return Ok(); } } diff --git a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs index ac7991db..5d01952b 100644 --- a/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs +++ b/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Program.cs @@ -5,8 +5,6 @@ using DigitalData.Core.Abstractions; using DigitalData.Core.Abstractions.Security.Extensions; using DigitalData.Core.API; using DigitalData.Core.Application; -using DigitalData.EmailProfilerDispatcher; -using DigitalData.UserManager.DependencyInjection; using EnvelopeGenerator.Application; using EnvelopeGenerator.Application.Common.Interfaces; using EnvelopeGenerator.Application.Common.Interfaces.Doc; @@ -288,11 +286,6 @@ try .AddAuthenticationSchemes(AuthScheme.Sender, AuthScheme.Receiver) .RequireAuthenticatedUser()); - // User Manager -#pragma warning disable CS0618 - builder.Services.AddUserManager(); -#pragma warning restore CS0618 - // LDAP Directory Search builder.ConfigureBySection(); builder.Services.AddDirectorySearchService(config.GetSection("DirectorySearchOptions")); @@ -335,8 +328,6 @@ try .AddNotificationInfrastructure(config); #pragma warning restore CS0618 - builder.Services.AddDispatcher(); - // User Preferences (theme, dark mode, grid layouts — stored in TBDD_CACHE) // Registration handled by Infrastructure DependencyInjection (AddEnvelopeGeneratorInfrastructureServices) @@ -367,7 +358,6 @@ try builder.Services.AddScoped(); builder.Services.AddDocInfrastructure(); builder.Services.AddCryptInfrastructure(config); - builder.Services.AddScoped(); builder.Services.AddSingleton(); // EnvelopeService with HttpClient factory (for SSR scenarios)