From e49192dac1df494307e01c5cac513f3dee332ac0 Mon Sep 17 00:00:00 2001 From: Giuliano Paschoalino Date: Wed, 1 Oct 2025 09:10:18 -0300 Subject: [PATCH] =?UTF-8?q?Refatora=C3=A7=C3=A3o=20e=20atualiza=C3=A7?= =?UTF-8?q?=C3=B5es=20de=20depend=C3=AAncias?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Modificado `InvoiceIngestionService` para usar `IServiceScopeFactory`, permitindo melhor gerenciamento do ciclo de vida de dependências. - Atualizações de pacotes em diversos projetos (`ComplianceNFs.Core`, `ComplianceNFs.Infrastructure`, `ComplianceNFs.Service`, `ComplianceNFs.Monitor`, `ComplianceNFs.Infrastructure.Tests`). - Alterado namespace de `AuditComplianceNFsTest.cs` para `ComplianceNFs.Infrastructure.Tests`. - Refatorações no `AttachmentRepository` e `ComplianceNFsDbContext` para simplificar código e melhorar legibilidade. - Atualizado teste `InvoiceIngestionServiceTests` para refletir mudanças no uso de escopos de serviço. - Melhorias gerais na arquitetura e alinhamento com boas práticas. --- .../Services/ApplicationServices.cs | 10 ++++++---- ComplianceNFs.Core/ComplianceNFs.Core.csproj | 1 + .../AuditComplianceNFsTest.cs | 2 +- .../ComplianceNFs.Infrastructure.Tests.csproj | 17 ++++++++++++----- .../InvoiceIngestionServiceTests.cs | 12 +++++++++++- .../ComplianceNFs.Infrastructure.csproj | 12 ++++++------ .../Repositories/AttachmentRepository.cs | 18 ++++-------------- .../Repositories/ComplianceNFsDbContext.cs | 4 +--- .../ComplianceNFs.Monitor.csproj | 4 ++-- .../ComplianceNFs.Service.csproj | 2 +- 10 files changed, 45 insertions(+), 37 deletions(-) diff --git a/ComplianceNFs.Core/Application/Services/ApplicationServices.cs b/ComplianceNFs.Core/Application/Services/ApplicationServices.cs index 7860686..04d29b1 100644 --- a/ComplianceNFs.Core/Application/Services/ApplicationServices.cs +++ b/ComplianceNFs.Core/Application/Services/ApplicationServices.cs @@ -8,6 +8,7 @@ using ComplianceNFs.Core.Ports; using System.Linq; using System.Numerics; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; namespace ComplianceNFs.Core.Application.Services { @@ -15,15 +16,14 @@ namespace ComplianceNFs.Core.Application.Services public class InvoiceIngestionService : IInvoiceIngestionService { private readonly IMailListener _mailListener; - private readonly IAttachmentRepository _attachmentRepository; private readonly IXmlParser _xmlParser; private readonly IPdfParser _pdfParser; private readonly ILogger _logger; - - public InvoiceIngestionService(IMailListener mailListener, IAttachmentRepository attachmentRepository, IXmlParser xmlParser, IPdfParser pdfParser, ILogger logger) + private readonly IServiceScopeFactory _scopeFactory; + public InvoiceIngestionService(IMailListener mailListener, IServiceScopeFactory scopeFactory, IXmlParser xmlParser, IPdfParser pdfParser, ILogger logger) { _mailListener = mailListener; - _attachmentRepository = attachmentRepository; + _scopeFactory = scopeFactory; _xmlParser = xmlParser; _pdfParser = pdfParser; _logger = logger; @@ -31,6 +31,8 @@ namespace ComplianceNFs.Core.Application.Services } private async void OnNewMailReceived(MailMessage mail) { + using var scope = _scopeFactory.CreateScope(); + var _attachmentRepository = scope.ServiceProvider.GetRequiredService(); _logger.LogInformation("New mail received: {Subject}", mail.Subject); try { diff --git a/ComplianceNFs.Core/ComplianceNFs.Core.csproj b/ComplianceNFs.Core/ComplianceNFs.Core.csproj index 3f225f5..43f91bd 100644 --- a/ComplianceNFs.Core/ComplianceNFs.Core.csproj +++ b/ComplianceNFs.Core/ComplianceNFs.Core.csproj @@ -6,6 +6,7 @@ + diff --git a/ComplianceNFs.Infrastructure.Tests/AuditComplianceNFsTest.cs b/ComplianceNFs.Infrastructure.Tests/AuditComplianceNFsTest.cs index ee8197c..26b7f4d 100644 --- a/ComplianceNFs.Infrastructure.Tests/AuditComplianceNFsTest.cs +++ b/ComplianceNFs.Infrastructure.Tests/AuditComplianceNFsTest.cs @@ -3,7 +3,7 @@ using System.Text.Json; using System.IO; using NUnit.Framework; -namespace ComplianceNFs.Tests +namespace ComplianceNFs.Infrastructure.Tests { [TestFixture] public class AuditComplianceNFsTest diff --git a/ComplianceNFs.Infrastructure.Tests/ComplianceNFs.Infrastructure.Tests.csproj b/ComplianceNFs.Infrastructure.Tests/ComplianceNFs.Infrastructure.Tests.csproj index 088e650..a39f039 100644 --- a/ComplianceNFs.Infrastructure.Tests/ComplianceNFs.Infrastructure.Tests.csproj +++ b/ComplianceNFs.Infrastructure.Tests/ComplianceNFs.Infrastructure.Tests.csproj @@ -10,13 +10,20 @@ - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + - + - - - + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/ComplianceNFs.Infrastructure.Tests/InvoiceIngestionServiceTests.cs b/ComplianceNFs.Infrastructure.Tests/InvoiceIngestionServiceTests.cs index 04db6af..eb7330a 100644 --- a/ComplianceNFs.Infrastructure.Tests/InvoiceIngestionServiceTests.cs +++ b/ComplianceNFs.Infrastructure.Tests/InvoiceIngestionServiceTests.cs @@ -9,6 +9,7 @@ using ComplianceNFs.Core.Application.Services; using ComplianceNFs.Core.Ports; using ComplianceNFs.Core.Entities; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; namespace ComplianceNFs.Infrastructure.Tests { @@ -19,17 +20,26 @@ namespace ComplianceNFs.Infrastructure.Tests { // Arrange var mockMailListener = new Mock(); + var mockScopeFactory = new Mock(); + var mockScope = new Mock(); + var mockProvider = new Mock(); var mockAttachmentRepo = new Mock(); var mockXmlParser = new Mock(); var mockPdfParser = new Mock(); var mockLogger = new Mock>(); + mockProvider.Setup(x => x.GetService(typeof(IAttachmentRepository))) + .Returns(mockAttachmentRepo.Object); + mockScope.Setup(x => x.ServiceProvider).Returns(mockProvider.Object); + mockScopeFactory.Setup(x => x.CreateScope()).Returns(mockScope.Object); + + var testParsed = new ParsedInvoice { CnpjComp = "123", NumeroNF = "456" }; mockXmlParser.Setup(x => x.Parse(It.IsAny())).Returns(testParsed); var service = new InvoiceIngestionService( mockMailListener.Object, - mockAttachmentRepo.Object, + mockScopeFactory.Object, mockXmlParser.Object, mockPdfParser.Object, mockLogger.Object diff --git a/ComplianceNFs.Infrastructure/ComplianceNFs.Infrastructure.csproj b/ComplianceNFs.Infrastructure/ComplianceNFs.Infrastructure.csproj index 8402d22..a192fa2 100644 --- a/ComplianceNFs.Infrastructure/ComplianceNFs.Infrastructure.csproj +++ b/ComplianceNFs.Infrastructure/ComplianceNFs.Infrastructure.csproj @@ -5,22 +5,22 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + - + - + diff --git a/ComplianceNFs.Infrastructure/Repositories/AttachmentRepository.cs b/ComplianceNFs.Infrastructure/Repositories/AttachmentRepository.cs index b2b4bda..9ef7776 100644 --- a/ComplianceNFs.Infrastructure/Repositories/AttachmentRepository.cs +++ b/ComplianceNFs.Infrastructure/Repositories/AttachmentRepository.cs @@ -10,16 +10,10 @@ using Microsoft.EntityFrameworkCore; namespace ComplianceNFs.Infrastructure.Repositories { - public class AttachmentRepository : IAttachmentRepository + public class AttachmentRepository(ComplianceNFsDbContext dbContext, ILogger logger) : IAttachmentRepository { - private readonly ComplianceNFsDbContext _dbContext; - private readonly ILogger _logger; - - public AttachmentRepository(ComplianceNFsDbContext dbContext, ILogger logger) - { - _dbContext = dbContext; - _logger = logger; - } + private readonly ComplianceNFsDbContext _dbContext = dbContext; + private readonly ILogger _logger = logger; public async Task SaveRawAsync(EnergyInvoice invoice) { @@ -40,11 +34,7 @@ namespace ComplianceNFs.Infrastructure.Repositories { try { - var invoice = await _dbContext.EnergyInvoices.FirstOrDefaultAsync(e => e.InvoiceId == invoiceId); - if (invoice == null) - { - throw new InvalidOperationException($"Invoice with ID {invoiceId} not found."); - } + var invoice = await _dbContext.EnergyInvoices.FirstOrDefaultAsync(e => e.InvoiceId == invoiceId) ?? throw new InvalidOperationException($"Invoice with ID {invoiceId} not found."); invoice.MatchedCodTE = matchedCodTE; invoice.Status = status; invoice.DiscrepancyNotes = notes; diff --git a/ComplianceNFs.Infrastructure/Repositories/ComplianceNFsDbContext.cs b/ComplianceNFs.Infrastructure/Repositories/ComplianceNFsDbContext.cs index 4000733..d2f3983 100644 --- a/ComplianceNFs.Infrastructure/Repositories/ComplianceNFsDbContext.cs +++ b/ComplianceNFs.Infrastructure/Repositories/ComplianceNFsDbContext.cs @@ -3,10 +3,8 @@ using ComplianceNFs.Core.Entities; namespace ComplianceNFs.Infrastructure.Repositories { - public class ComplianceNFsDbContext : DbContext + public class ComplianceNFsDbContext(DbContextOptions options) : DbContext(options) { - public ComplianceNFsDbContext(DbContextOptions options) : base(options) { } - public DbSet EnergyInvoices { get; set; } // Add other DbSets as needed (e.g., BuyingRecord, etc.) } diff --git a/ComplianceNFs.Monitor/ComplianceNFs.Monitor.csproj b/ComplianceNFs.Monitor/ComplianceNFs.Monitor.csproj index 65c6328..df27003 100644 --- a/ComplianceNFs.Monitor/ComplianceNFs.Monitor.csproj +++ b/ComplianceNFs.Monitor/ComplianceNFs.Monitor.csproj @@ -3,8 +3,8 @@ - - + + diff --git a/ComplianceNFs.Service/ComplianceNFs.Service.csproj b/ComplianceNFs.Service/ComplianceNFs.Service.csproj index fe2e67d..ffbe58b 100644 --- a/ComplianceNFs.Service/ComplianceNFs.Service.csproj +++ b/ComplianceNFs.Service/ComplianceNFs.Service.csproj @@ -8,7 +8,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all