From 0a10bb87bba9fbc68d2ed36185ec424aca59c05d Mon Sep 17 00:00:00 2001 From: Fredi Machado Date: Thu, 16 May 2024 18:31:00 +1000 Subject: [PATCH] Fix remaining code cleanup suggestions --- .../Projections/ProductProjectionService.cs | 6 ++-- .../Commands/CreateProductTests.cs | 18 +++++----- .../Commands/CreateProduct.cs | 11 ++---- .../InvalidProductPriceException.cs | 6 ++-- .../NCafe.Admin.Domain/Queries/GetProducts.cs | 11 ++---- src/Barista/NCafe.Barista.Api/Program.cs | 2 +- .../BaristaOrderProjectionService.cs | 8 ++--- .../Commands/CompleteOrderTests.cs | 20 +++++------ .../Commands/PlaceOrderTests.cs | 14 ++++---- .../Commands/CompleteOrder.cs | 18 +++------- .../Commands/PlaceOrder.cs | 11 ++---- .../Exceptions/OrderNotFoundException.cs | 10 ++---- .../NCafe.Barista.Domain/Queries/GetOrders.cs | 12 +++---- .../Projections/ProductProjectionService.cs | 6 ++-- .../Commands/PayForOrderTests.cs | 22 ++++++------ .../Commands/PlaceOrderTests.cs | 34 +++++++++---------- .../Exceptions/OrderNotFoundException.cs | 10 ++---- .../Exceptions/ProductNotFoundException.cs | 10 ++---- .../Queries/GetProducts.cs | 12 +++---- .../Architecture/InfrastructureTests.cs | 2 ++ src/Common/NCafe.Core/Domain/AggregateRoot.cs | 4 +-- src/Common/NCafe.Core/Domain/Event.cs | 2 +- .../Exceptions/InvalidVersionException.cs | 7 ++-- .../NCafe.Core/Exceptions/NCafeException.cs | 5 +-- .../Commands/CommandDispatcher.cs | 11 ++---- .../Logging/CommandHandlerLogger.cs | 21 +++++------- .../MessageBus/RabbitMqPublisher.cs | 11 ++---- .../Queries/QueryDispatcher.cs | 12 +++---- .../ReadModels/InMemoryReadModelRepository.cs | 8 ++--- src/UI/NCafe.Web/Models/Product.cs | 5 ++- 30 files changed, 126 insertions(+), 203 deletions(-) diff --git a/src/Admin/NCafe.Admin.Api/Projections/ProductProjectionService.cs b/src/Admin/NCafe.Admin.Api/Projections/ProductProjectionService.cs index a8dc3c0..d6ebed7 100644 --- a/src/Admin/NCafe.Admin.Api/Projections/ProductProjectionService.cs +++ b/src/Admin/NCafe.Admin.Api/Projections/ProductProjectionService.cs @@ -6,11 +6,11 @@ namespace NCafe.Admin.Api.Projections; public class ProductProjectionService : BackgroundService { - private readonly IProjectionService projectionService; + private readonly IProjectionService _projectionService; public ProductProjectionService(IProjectionService projectionService) { - this.projectionService = projectionService; + _projectionService = projectionService; projectionService.OnCreate(@event => new Product { @@ -22,7 +22,7 @@ public ProductProjectionService(IProjectionService projectionService) protected async override Task ExecuteAsync(CancellationToken stoppingToken) { - await projectionService.Start(stoppingToken); + await _projectionService.Start(stoppingToken); await Task.Delay(Timeout.Infinite, stoppingToken); } diff --git a/src/Admin/NCafe.Admin.Domain.Tests/Commands/CreateProductTests.cs b/src/Admin/NCafe.Admin.Domain.Tests/Commands/CreateProductTests.cs index 582e30f..29e01e5 100644 --- a/src/Admin/NCafe.Admin.Domain.Tests/Commands/CreateProductTests.cs +++ b/src/Admin/NCafe.Admin.Domain.Tests/Commands/CreateProductTests.cs @@ -7,15 +7,13 @@ namespace NCafe.Admin.Domain.Tests.Commands; public class CreateProductTests { - private readonly CreateProductHandler sut; - - private readonly IRepository repository; + private readonly CreateProductHandler _sut; + private readonly IRepository _repository; public CreateProductTests() { - repository = A.Fake(); - - sut = new CreateProductHandler(repository); + _repository = A.Fake(); + _sut = new CreateProductHandler(_repository); } [Theory] @@ -28,7 +26,7 @@ public async Task GivenInvalidName_ShouldThrowException(string name) var command = new CreateProduct(name, 3); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeOfType(); @@ -43,7 +41,7 @@ public async Task GivenInvalidPrice_ShouldThrowException(decimal price) var command = new CreateProduct("Flat White", price); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeOfType(); @@ -56,11 +54,11 @@ public async Task GivenValidProductInformation_ShouldCreateAndStoreProduct() var command = new CreateProduct("Flat White", 3.5m); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeNull(); - A.CallTo(() => repository.Save(A.That.Matches(p => p.Name == command.Name && p.Price == command.Price))) + A.CallTo(() => _repository.Save(A.That.Matches(p => p.Name == command.Name && p.Price == command.Price))) .MustHaveHappenedOnceExactly(); ; } } diff --git a/src/Admin/NCafe.Admin.Domain/Commands/CreateProduct.cs b/src/Admin/NCafe.Admin.Domain/Commands/CreateProduct.cs index 6aa2d16..aa4962c 100644 --- a/src/Admin/NCafe.Admin.Domain/Commands/CreateProduct.cs +++ b/src/Admin/NCafe.Admin.Domain/Commands/CreateProduct.cs @@ -7,14 +7,9 @@ namespace NCafe.Admin.Domain.Commands; public record CreateProduct(string Name, decimal Price) : ICommand; -internal sealed class CreateProductHandler : ICommandHandler +internal sealed class CreateProductHandler(IRepository repository) : ICommandHandler { - private readonly IRepository repository; - - public CreateProductHandler(IRepository repository) - { - this.repository = repository; - } + private readonly IRepository _repository = repository; public async Task HandleAsync(CreateProduct command) { @@ -30,6 +25,6 @@ public async Task HandleAsync(CreateProduct command) var product = new Product(Guid.NewGuid(), command.Name, command.Price); - await repository.Save(product); + await _repository.Save(product); } } diff --git a/src/Admin/NCafe.Admin.Domain/Exceptions/InvalidProductPriceException.cs b/src/Admin/NCafe.Admin.Domain/Exceptions/InvalidProductPriceException.cs index 7abbd50..dcf8308 100644 --- a/src/Admin/NCafe.Admin.Domain/Exceptions/InvalidProductPriceException.cs +++ b/src/Admin/NCafe.Admin.Domain/Exceptions/InvalidProductPriceException.cs @@ -2,9 +2,7 @@ namespace NCafe.Admin.Domain.Exceptions; -public class InvalidProductPriceException : DomainException +public class InvalidProductPriceException(decimal price) + : DomainException($"The price '{price}' must be greater than zero.") { - public InvalidProductPriceException(decimal price) : base($"The price '{price}' must be greater than zero.") - { - } } diff --git a/src/Admin/NCafe.Admin.Domain/Queries/GetProducts.cs b/src/Admin/NCafe.Admin.Domain/Queries/GetProducts.cs index 96708fa..85c104d 100644 --- a/src/Admin/NCafe.Admin.Domain/Queries/GetProducts.cs +++ b/src/Admin/NCafe.Admin.Domain/Queries/GetProducts.cs @@ -6,18 +6,13 @@ namespace NCafe.Admin.Domain.Queries; public record GetProducts : IQuery; -internal sealed class GetProductsHandler : IQueryHandler +internal sealed class GetProductsHandler(IReadModelRepository productRepository) : IQueryHandler { - private readonly IReadModelRepository productRepository; - - public GetProductsHandler(IReadModelRepository productRepository) - { - this.productRepository = productRepository; - } + private readonly IReadModelRepository _productRepository = productRepository; public Task HandleAsync(GetProducts query) { - var products = productRepository.GetAll() + var products = _productRepository.GetAll() .ToArray(); return Task.FromResult(products); } diff --git a/src/Barista/NCafe.Barista.Api/Program.cs b/src/Barista/NCafe.Barista.Api/Program.cs index dc6d9a5..d1b91a5 100644 --- a/src/Barista/NCafe.Barista.Api/Program.cs +++ b/src/Barista/NCafe.Barista.Api/Program.cs @@ -42,7 +42,7 @@ builder.Services.AddResponseCompression(opts => { opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat( - new[] { "application/octet-stream" }); + ["application/octet-stream"]); }); var app = builder.Build(); diff --git a/src/Barista/NCafe.Barista.Api/Projections/BaristaOrderProjectionService.cs b/src/Barista/NCafe.Barista.Api/Projections/BaristaOrderProjectionService.cs index d0bdd4b..532b804 100644 --- a/src/Barista/NCafe.Barista.Api/Projections/BaristaOrderProjectionService.cs +++ b/src/Barista/NCafe.Barista.Api/Projections/BaristaOrderProjectionService.cs @@ -6,11 +6,11 @@ namespace NCafe.Barista.Api.Projections; public class BaristaOrderProjectionService : BackgroundService { - private readonly IProjectionService projectionService; + private readonly IProjectionService _projectionService; public BaristaOrderProjectionService(IProjectionService projectionService) { - this.projectionService = projectionService; + _projectionService = projectionService; projectionService.OnCreate(@event => new BaristaOrder { @@ -24,9 +24,9 @@ public BaristaOrderProjectionService(IProjectionService projection (@event, order) => order.IsCompleted = true); } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) + protected async override Task ExecuteAsync(CancellationToken stoppingToken) { - await projectionService.Start(stoppingToken); + await _projectionService.Start(stoppingToken); await Task.Delay(Timeout.Infinite, stoppingToken); } diff --git a/src/Barista/NCafe.Barista.Domain.Tests/Commands/CompleteOrderTests.cs b/src/Barista/NCafe.Barista.Domain.Tests/Commands/CompleteOrderTests.cs index d7b8708..1554f70 100644 --- a/src/Barista/NCafe.Barista.Domain.Tests/Commands/CompleteOrderTests.cs +++ b/src/Barista/NCafe.Barista.Domain.Tests/Commands/CompleteOrderTests.cs @@ -7,15 +7,13 @@ namespace NCafe.Barista.Domain.Tests.Commands; public class CompleteOrderTests { - private readonly CompleteOrderHandler sut; - - private readonly IRepository repository; + private readonly CompleteOrderHandler _sut; + private readonly IRepository _repository; public CompleteOrderTests() { - repository = A.Fake(); - - sut = new CompleteOrderHandler(repository); + _repository = A.Fake(); + _sut = new CompleteOrderHandler(_repository); } [Fact] @@ -23,13 +21,13 @@ public async Task GivenOrderNotFound_ShouldThrowException() { // Arrange var orderId = Guid.NewGuid(); - A.CallTo(() => repository.GetById(orderId)) + A.CallTo(() => _repository.GetById(orderId)) .Returns((BaristaOrder)null); var command = new CompleteOrder(orderId); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeOfType(); @@ -40,16 +38,16 @@ public async Task ShouldSaveOrder() { // Arrange var orderId = Guid.NewGuid(); - A.CallTo(() => repository.GetById(orderId)) + A.CallTo(() => _repository.GetById(orderId)) .Returns(new BaristaOrder(orderId, Guid.NewGuid(), 1)); var command = new CompleteOrder(orderId); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeNull(); - A.CallTo(() => repository.Save(A.That.Matches(o => o.Id == orderId && o.IsCompleted == true))) + A.CallTo(() => _repository.Save(A.That.Matches(o => o.Id == orderId && o.IsCompleted == true))) .MustHaveHappenedOnceExactly(); } } diff --git a/src/Barista/NCafe.Barista.Domain.Tests/Commands/PlaceOrderTests.cs b/src/Barista/NCafe.Barista.Domain.Tests/Commands/PlaceOrderTests.cs index dc193f1..992e1ea 100644 --- a/src/Barista/NCafe.Barista.Domain.Tests/Commands/PlaceOrderTests.cs +++ b/src/Barista/NCafe.Barista.Domain.Tests/Commands/PlaceOrderTests.cs @@ -6,15 +6,13 @@ namespace NCafe.Barista.Domain.Tests.Commands; public class PlaceOrderTests { - private readonly PlaceOrderHandler sut; - - private readonly IRepository repository; + private readonly PlaceOrderHandler _sut; + private readonly IRepository _repository; public PlaceOrderTests() { - repository = A.Fake(); - - sut = new PlaceOrderHandler(repository); + _repository = A.Fake(); + _sut = new PlaceOrderHandler(_repository); } [Fact] @@ -26,11 +24,11 @@ public async Task ShouldSaveOrder() var command = new PlaceOrder(orderId, productId, 1); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeNull(); - A.CallTo(() => repository.Save(A.That.Matches(o => o.Id == orderId && o.ProductId == productId && o.Quantity == 1))) + A.CallTo(() => _repository.Save(A.That.Matches(o => o.Id == orderId && o.ProductId == productId && o.Quantity == 1))) .MustHaveHappenedOnceExactly(); } } diff --git a/src/Barista/NCafe.Barista.Domain/Commands/CompleteOrder.cs b/src/Barista/NCafe.Barista.Domain/Commands/CompleteOrder.cs index 0e6ba98..cfb3ad7 100644 --- a/src/Barista/NCafe.Barista.Domain/Commands/CompleteOrder.cs +++ b/src/Barista/NCafe.Barista.Domain/Commands/CompleteOrder.cs @@ -7,26 +7,16 @@ namespace NCafe.Barista.Domain.Commands; public record CompleteOrder(Guid Id) : ICommand; -internal sealed class CompleteOrderHandler : ICommandHandler +internal sealed class CompleteOrderHandler(IRepository repository) : ICommandHandler { - private readonly IRepository repository; - - public CompleteOrderHandler(IRepository repository) - { - this.repository = repository; - } + private readonly IRepository _repository = repository; public async Task HandleAsync(CompleteOrder command) { - var order = await repository.GetById(command.Id); - - if (order == null) - { - throw new OrderNotFoundException(command.Id); - } + var order = await _repository.GetById(command.Id) ?? throw new OrderNotFoundException(command.Id); order.CompletePreparation(); - await repository.Save(order); + await _repository.Save(order); } } diff --git a/src/Barista/NCafe.Barista.Domain/Commands/PlaceOrder.cs b/src/Barista/NCafe.Barista.Domain/Commands/PlaceOrder.cs index 6e76498..a0b6d0b 100644 --- a/src/Barista/NCafe.Barista.Domain/Commands/PlaceOrder.cs +++ b/src/Barista/NCafe.Barista.Domain/Commands/PlaceOrder.cs @@ -6,19 +6,14 @@ namespace NCafe.Barista.Domain.Commands; public record PlaceOrder(Guid Id, Guid ProductId, int Quantity) : ICommand; -internal sealed class PlaceOrderHandler : ICommandHandler +internal sealed class PlaceOrderHandler(IRepository repository) : ICommandHandler { - private readonly IRepository repository; - - public PlaceOrderHandler(IRepository repository) - { - this.repository = repository; - } + private readonly IRepository _repository = repository; public async Task HandleAsync(PlaceOrder command) { var order = new BaristaOrder(command.Id, command.ProductId, command.Quantity); - await repository.Save(order); + await _repository.Save(order); } } diff --git a/src/Barista/NCafe.Barista.Domain/Exceptions/OrderNotFoundException.cs b/src/Barista/NCafe.Barista.Domain/Exceptions/OrderNotFoundException.cs index 737bbf5..92b4c5f 100644 --- a/src/Barista/NCafe.Barista.Domain/Exceptions/OrderNotFoundException.cs +++ b/src/Barista/NCafe.Barista.Domain/Exceptions/OrderNotFoundException.cs @@ -2,12 +2,8 @@ namespace NCafe.Barista.Domain.Exceptions; -public class OrderNotFoundException : DomainException +public class OrderNotFoundException(Guid orderId) + : DomainException($"Order '{orderId}' was not found.") { - public OrderNotFoundException(Guid orderId) : base($"Order '{orderId}' was not found.") - { - OrderId = orderId; - } - - public Guid OrderId { get; } + public Guid OrderId { get; } = orderId; } diff --git a/src/Barista/NCafe.Barista.Domain/Queries/GetOrders.cs b/src/Barista/NCafe.Barista.Domain/Queries/GetOrders.cs index a945d3a..705a07f 100644 --- a/src/Barista/NCafe.Barista.Domain/Queries/GetOrders.cs +++ b/src/Barista/NCafe.Barista.Domain/Queries/GetOrders.cs @@ -6,18 +6,14 @@ namespace NCafe.Barista.Domain.Queries; public record GetOrders : IQuery; -internal sealed class GetOrdersHandler : IQueryHandler +internal sealed class GetOrdersHandler(IReadModelRepository orderRepository) + : IQueryHandler { - private readonly IReadModelRepository orderRepository; - - public GetOrdersHandler(IReadModelRepository orderRepository) - { - this.orderRepository = orderRepository; - } + private readonly IReadModelRepository _orderRepository = orderRepository; public Task HandleAsync(GetOrders query) { - var products = orderRepository.GetAll() + var products = _orderRepository.GetAll() .ToArray(); return Task.FromResult(products); } diff --git a/src/Cashier/NCafe.Cashier.Api/Projections/ProductProjectionService.cs b/src/Cashier/NCafe.Cashier.Api/Projections/ProductProjectionService.cs index 0779730..bd13e2d 100644 --- a/src/Cashier/NCafe.Cashier.Api/Projections/ProductProjectionService.cs +++ b/src/Cashier/NCafe.Cashier.Api/Projections/ProductProjectionService.cs @@ -5,11 +5,11 @@ namespace NCafe.Cashier.Api.Projections; public class ProductProjectionService : BackgroundService { - private readonly IProjectionService projectionService; + private readonly IProjectionService _projectionService; public ProductProjectionService(IProjectionService projectionService) { - this.projectionService = projectionService; + _projectionService = projectionService; projectionService.OnCreate(@event => new Product { @@ -21,7 +21,7 @@ public ProductProjectionService(IProjectionService projectionService) protected async override Task ExecuteAsync(CancellationToken stoppingToken) { - await projectionService.Start(stoppingToken); + await _projectionService.Start(stoppingToken); await Task.Delay(Timeout.Infinite, stoppingToken); } diff --git a/src/Cashier/NCafe.Cashier.Domain.Tests/Commands/PayForOrderTests.cs b/src/Cashier/NCafe.Cashier.Domain.Tests/Commands/PayForOrderTests.cs index 9501cdb..04111b0 100644 --- a/src/Cashier/NCafe.Cashier.Domain.Tests/Commands/PayForOrderTests.cs +++ b/src/Cashier/NCafe.Cashier.Domain.Tests/Commands/PayForOrderTests.cs @@ -8,15 +8,13 @@ namespace NCafe.Cashier.Domain.Tests.Commands; public class PayForOrderTests { - private readonly PayForOrderHandler sut; - - private readonly IRepository repository; + private readonly PayForOrderHandler _sut; + private readonly IRepository _repository; public PayForOrderTests() { - repository = A.Fake(); - - sut = new PayForOrderHandler(repository); + _repository = A.Fake(); + _sut = new PayForOrderHandler(_repository); } [Fact] @@ -26,7 +24,7 @@ public async Task GivenInvalidOrderId_ShouldThrowException() var command = new PayForOrder(Guid.Empty); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeOfType(); @@ -36,12 +34,12 @@ public async Task GivenInvalidOrderId_ShouldThrowException() public async Task GivenOrderNotFound_ShouldThrowException() { // Arrange - A.CallTo(() => repository.GetById(A._)) + A.CallTo(() => _repository.GetById(A._)) .Returns((Order)null); var command = new PayForOrder(Guid.NewGuid()); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeOfType(); @@ -52,17 +50,17 @@ public async Task GivenPlacedOrder_WhenPayingForOrder_ShouldUpdateOrder() { // Arrange var orderId = Guid.NewGuid(); - A.CallTo(() => repository.GetById(orderId)) + A.CallTo(() => _repository.GetById(orderId)) .Returns(new Order(orderId, Guid.NewGuid(), 1)); var command = new PayForOrder(orderId); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeNull(); - A.CallTo(() => repository.Save(A.That.Matches(o => o.Id == orderId && o.HasBeenPaid == true))) + A.CallTo(() => _repository.Save(A.That.Matches(o => o.Id == orderId && o.HasBeenPaid == true))) .MustHaveHappenedOnceExactly(); } } diff --git a/src/Cashier/NCafe.Cashier.Domain.Tests/Commands/PlaceOrderTests.cs b/src/Cashier/NCafe.Cashier.Domain.Tests/Commands/PlaceOrderTests.cs index 6d394e1..d235bbd 100644 --- a/src/Cashier/NCafe.Cashier.Domain.Tests/Commands/PlaceOrderTests.cs +++ b/src/Cashier/NCafe.Cashier.Domain.Tests/Commands/PlaceOrderTests.cs @@ -11,19 +11,17 @@ namespace NCafe.Cashier.Domain.Tests.Commands; public class PlaceOrderTests { - private readonly PlaceOrderHandler sut; - - private readonly IRepository repository; - private readonly IReadModelRepository productRepository; - private readonly IPublisher publisher; + private readonly PlaceOrderHandler _sut; + private readonly IRepository _repository; + private readonly IReadModelRepository _productRepository; + private readonly IPublisher _publisher; public PlaceOrderTests() { - repository = A.Fake(); - productRepository = A.Fake>(); - publisher = A.Fake(); - - sut = new PlaceOrderHandler(repository, productRepository, publisher); + _repository = A.Fake(); + _productRepository = A.Fake>(); + _publisher = A.Fake(); + _sut = new PlaceOrderHandler(_repository, _productRepository, _publisher); } [Fact] @@ -31,13 +29,13 @@ public async Task GivenProductNotFound_ShouldThrowException() { // Arrange var productId = Guid.NewGuid(); - A.CallTo(() => productRepository.GetById(productId)) + A.CallTo(() => _productRepository.GetById(productId)) .Returns(null); var command = new PlaceOrder(productId, 1); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeOfType(); @@ -48,17 +46,17 @@ public async Task GivenProductExists_ShouldSaveOrder() { // Arrange var productId = Guid.NewGuid(); - A.CallTo(() => productRepository.GetById(productId)) + A.CallTo(() => _productRepository.GetById(productId)) .Returns(new Product { Id = productId }); var command = new PlaceOrder(productId, 1); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeNull(); - A.CallTo(() => repository.Save(A.That.Matches(o => o.ProductId == productId && o.Quantity == 1))) + A.CallTo(() => _repository.Save(A.That.Matches(o => o.ProductId == productId && o.Quantity == 1))) .MustHaveHappenedOnceExactly(); } @@ -67,17 +65,17 @@ public async Task GivenOrderSaved_ShouldPublishToMessageBus() { // Arrange var productId = Guid.NewGuid(); - A.CallTo(() => productRepository.GetById(productId)) + A.CallTo(() => _productRepository.GetById(productId)) .Returns(new Product { Id = productId }); var command = new PlaceOrder(productId, 1); // Act - var exception = await Record.ExceptionAsync(() => sut.HandleAsync(command)); + var exception = await Record.ExceptionAsync(() => _sut.HandleAsync(command)); // Assert exception.ShouldBeNull(); - A.CallTo(() => publisher.Publish("orders", A.That.Matches(o => o.ProductId == productId && o.Quantity == 1))) + A.CallTo(() => _publisher.Publish("orders", A.That.Matches(o => o.ProductId == productId && o.Quantity == 1))) .MustHaveHappenedOnceExactly(); } } diff --git a/src/Cashier/NCafe.Cashier.Domain/Exceptions/OrderNotFoundException.cs b/src/Cashier/NCafe.Cashier.Domain/Exceptions/OrderNotFoundException.cs index ab560aa..8fe5210 100644 --- a/src/Cashier/NCafe.Cashier.Domain/Exceptions/OrderNotFoundException.cs +++ b/src/Cashier/NCafe.Cashier.Domain/Exceptions/OrderNotFoundException.cs @@ -2,12 +2,8 @@ namespace NCafe.Cashier.Domain.Exceptions; -public class OrderNotFoundException : DomainException +public class OrderNotFoundException(Guid orderId) + : DomainException($"Order '{orderId}' was not found.") { - public OrderNotFoundException(Guid orderId) : base($"Order '{orderId}' was not found.") - { - OrderId = orderId; - } - - public Guid OrderId { get; } + public Guid OrderId { get; } = orderId; } diff --git a/src/Cashier/NCafe.Cashier.Domain/Exceptions/ProductNotFoundException.cs b/src/Cashier/NCafe.Cashier.Domain/Exceptions/ProductNotFoundException.cs index f757716..63443d4 100644 --- a/src/Cashier/NCafe.Cashier.Domain/Exceptions/ProductNotFoundException.cs +++ b/src/Cashier/NCafe.Cashier.Domain/Exceptions/ProductNotFoundException.cs @@ -2,12 +2,8 @@ namespace NCafe.Cashier.Domain.Exceptions; -public class ProductNotFoundException : DomainException +public class ProductNotFoundException(Guid productId) + : DomainException($"Product '{productId}' was not found.") { - public ProductNotFoundException(Guid productId) : base($"Product '{productId}' was not found.") - { - ProductId = productId; - } - - public Guid ProductId { get; } + public Guid ProductId { get; } = productId; } diff --git a/src/Cashier/NCafe.Cashier.Domain/Queries/GetProducts.cs b/src/Cashier/NCafe.Cashier.Domain/Queries/GetProducts.cs index 4c69424..c3008fd 100644 --- a/src/Cashier/NCafe.Cashier.Domain/Queries/GetProducts.cs +++ b/src/Cashier/NCafe.Cashier.Domain/Queries/GetProducts.cs @@ -6,18 +6,14 @@ namespace NCafe.Cashier.Domain.Queries; public record GetProducts : IQuery; -internal sealed class GetProductsHandler : IQueryHandler +internal sealed class GetProductsHandler(IReadModelRepository productRepository) + : IQueryHandler { - private readonly IReadModelRepository productRepository; - - public GetProductsHandler(IReadModelRepository productRepository) - { - this.productRepository = productRepository; - } + private readonly IReadModelRepository _productRepository = productRepository; public Task HandleAsync(GetProducts query) { - var products = productRepository.GetAll() + var products = _productRepository.GetAll() .ToArray(); return Task.FromResult(products); } diff --git a/src/Common/NCafe.Common.Tests/Architecture/InfrastructureTests.cs b/src/Common/NCafe.Common.Tests/Architecture/InfrastructureTests.cs index 102f907..b00e2d9 100644 --- a/src/Common/NCafe.Common.Tests/Architecture/InfrastructureTests.cs +++ b/src/Common/NCafe.Common.Tests/Architecture/InfrastructureTests.cs @@ -7,6 +7,7 @@ namespace NCafe.Common.Tests.Architecture; public class InfrastructureTests { +#pragma warning disable IDE1006 // Naming Styles private readonly static System.Reflection.Assembly InfrastructureAssembly = System.Reflection.Assembly.Load("NCafe.Infrastructure"); @@ -32,4 +33,5 @@ public void Infrastructure_Project_Types_Should_Be_Internal() typesShouldBeInternal.Check(Architecture); } +#pragma warning restore IDE1006 // Naming Styles } diff --git a/src/Common/NCafe.Core/Domain/AggregateRoot.cs b/src/Common/NCafe.Core/Domain/AggregateRoot.cs index 267f492..cde4304 100644 --- a/src/Common/NCafe.Core/Domain/AggregateRoot.cs +++ b/src/Common/NCafe.Core/Domain/AggregateRoot.cs @@ -7,7 +7,7 @@ public abstract class AggregateRoot public Guid Id { get; protected set; } public long Version { get; protected set; } = -1; - private readonly List _pendingEvents = new(); + private readonly List _pendingEvents = []; protected void RaiseEvent(Event @event) { @@ -20,7 +20,7 @@ protected void RaiseEvent(Event @event) public IEnumerable GetPendingEvents() { - return _pendingEvents.ToArray(); + return [.. _pendingEvents]; } internal void ClearPendingEvents() diff --git a/src/Common/NCafe.Core/Domain/Event.cs b/src/Common/NCafe.Core/Domain/Event.cs index f5a0253..1b218df 100644 --- a/src/Common/NCafe.Core/Domain/Event.cs +++ b/src/Common/NCafe.Core/Domain/Event.cs @@ -4,5 +4,5 @@ public abstract class Event : IEvent { public Guid Id { get; protected set; } - public long Version { get; protected internal set; } + public long Version { get; internal protected set; } } diff --git a/src/Common/NCafe.Core/Exceptions/InvalidVersionException.cs b/src/Common/NCafe.Core/Exceptions/InvalidVersionException.cs index 64e36e9..0f57b61 100644 --- a/src/Common/NCafe.Core/Exceptions/InvalidVersionException.cs +++ b/src/Common/NCafe.Core/Exceptions/InvalidVersionException.cs @@ -2,10 +2,7 @@ namespace NCafe.Core.Exceptions; -public class InvalidVersionException : DomainException +public class InvalidVersionException(Event @event, AggregateRoot aggregateRoot) + : DomainException($"{@event.GetType().Name} Event v{@event.Version} cannot be applied to Aggregate {aggregateRoot.GetType().Name} : {aggregateRoot.Id} v{aggregateRoot.Version}") { - public InvalidVersionException(Event @event, AggregateRoot aggregateRoot) - : base($"{@event.GetType().Name} Event v{@event.Version} cannot be applied to Aggregate {aggregateRoot.GetType().Name} : {aggregateRoot.Id} v{aggregateRoot.Version}") - { - } } diff --git a/src/Common/NCafe.Core/Exceptions/NCafeException.cs b/src/Common/NCafe.Core/Exceptions/NCafeException.cs index 320ace8..c6fdef0 100644 --- a/src/Common/NCafe.Core/Exceptions/NCafeException.cs +++ b/src/Common/NCafe.Core/Exceptions/NCafeException.cs @@ -1,8 +1,5 @@ namespace NCafe.Core.Exceptions; -public abstract class DomainException : Exception +public abstract class DomainException(string message) : Exception(message) { - protected DomainException(string message) : base(message) - { - } } diff --git a/src/Common/NCafe.Infrastructure/Commands/CommandDispatcher.cs b/src/Common/NCafe.Infrastructure/Commands/CommandDispatcher.cs index 4716fb8..14a44e0 100644 --- a/src/Common/NCafe.Infrastructure/Commands/CommandDispatcher.cs +++ b/src/Common/NCafe.Infrastructure/Commands/CommandDispatcher.cs @@ -3,18 +3,13 @@ namespace NCafe.Infrastructure.Commands; -internal sealed class CommandDispatcher : ICommandDispatcher +internal sealed class CommandDispatcher(IServiceProvider serviceProvider) : ICommandDispatcher { - private readonly IServiceProvider serviceProvider; - - public CommandDispatcher(IServiceProvider serviceProvider) - { - this.serviceProvider = serviceProvider; - } + private readonly IServiceProvider _serviceProvider = serviceProvider; public async Task DispatchAsync(TCommand command) where TCommand : class, ICommand { - using var scope = serviceProvider.CreateScope(); + using var scope = _serviceProvider.CreateScope(); var handler = scope.ServiceProvider.GetRequiredService>(); await handler.HandleAsync(command); diff --git a/src/Common/NCafe.Infrastructure/Logging/CommandHandlerLogger.cs b/src/Common/NCafe.Infrastructure/Logging/CommandHandlerLogger.cs index 1d5b3f7..83ae3a7 100644 --- a/src/Common/NCafe.Infrastructure/Logging/CommandHandlerLogger.cs +++ b/src/Common/NCafe.Infrastructure/Logging/CommandHandlerLogger.cs @@ -3,16 +3,11 @@ namespace NCafe.Infrastructure.Logging; -internal sealed class CommandHandlerLogger : ICommandHandler where TCommand : class, ICommand +internal sealed class CommandHandlerLogger(ICommandHandler handler, ILogger> logger) + : ICommandHandler where TCommand : class, ICommand { - private readonly ICommandHandler handler; - private readonly ILogger logger; - - public CommandHandlerLogger(ICommandHandler handler, ILogger> logger) - { - this.handler = handler; - this.logger = logger; - } + private readonly ICommandHandler _handler = handler; + private readonly ILogger _logger = logger; public async Task HandleAsync(TCommand command) { @@ -20,14 +15,14 @@ public async Task HandleAsync(TCommand command) try { - logger.LogInformation("Started processing {commandType} command.", commandType); - await handler.HandleAsync(command); - logger.LogInformation("Finished processing {commandType} command.", commandType); + _logger.LogInformation("Started processing {commandType} command.", commandType); + await _handler.HandleAsync(command); + _logger.LogInformation("Finished processing {commandType} command.", commandType); } catch (Exception ex) { - logger.LogError(ex, "Failed to process {commandType} command.", commandType); + _logger.LogError(ex, "Failed to process {commandType} command.", commandType); throw; } } diff --git a/src/Common/NCafe.Infrastructure/MessageBus/RabbitMqPublisher.cs b/src/Common/NCafe.Infrastructure/MessageBus/RabbitMqPublisher.cs index 03e1a6e..2125540 100644 --- a/src/Common/NCafe.Infrastructure/MessageBus/RabbitMqPublisher.cs +++ b/src/Common/NCafe.Infrastructure/MessageBus/RabbitMqPublisher.cs @@ -4,17 +4,12 @@ namespace NCafe.Infrastructure.MessageBus; -internal class RabbitMqPublisher : IPublisher +internal class RabbitMqPublisher(IConfiguration configuration) : IPublisher { - private readonly IBus bus; - - public RabbitMqPublisher(IConfiguration configuration) - { - bus = RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMq")); - } + private readonly IBus _bus = RabbitHutch.CreateBus(configuration.GetConnectionString("RabbitMq")); public async Task Publish(string topicName, T message) where T : class, IBusMessage { - await bus.PubSub.PublishAsync(message, topicName); + await _bus.PubSub.PublishAsync(message, topicName); } } diff --git a/src/Common/NCafe.Infrastructure/Queries/QueryDispatcher.cs b/src/Common/NCafe.Infrastructure/Queries/QueryDispatcher.cs index 1cff9bc..ad02e27 100644 --- a/src/Common/NCafe.Infrastructure/Queries/QueryDispatcher.cs +++ b/src/Common/NCafe.Infrastructure/Queries/QueryDispatcher.cs @@ -3,18 +3,14 @@ namespace NCafe.Infrastructure.Queries; -internal sealed class QueryDispatcher : IQueryDispatcher +internal sealed class QueryDispatcher(IServiceScopeFactory serviceScopeFactory) : IQueryDispatcher { - private readonly IServiceProvider serviceProvider; - - public QueryDispatcher(IServiceProvider serviceProvider) - { - this.serviceProvider = serviceProvider; - } + private readonly IServiceScopeFactory _serviceScopeFactory = serviceScopeFactory; public async Task QueryAsync(IQuery query) { - using var scope = serviceProvider.CreateScope(); + using var scope = _serviceScopeFactory.CreateScope(); + var handlerType = typeof(IQueryHandler<,>).MakeGenericType(query.GetType(), typeof(TResult)); var handler = scope.ServiceProvider.GetRequiredService(handlerType); diff --git a/src/Common/NCafe.Infrastructure/ReadModels/InMemoryReadModelRepository.cs b/src/Common/NCafe.Infrastructure/ReadModels/InMemoryReadModelRepository.cs index 049a460..8136a62 100644 --- a/src/Common/NCafe.Infrastructure/ReadModels/InMemoryReadModelRepository.cs +++ b/src/Common/NCafe.Infrastructure/ReadModels/InMemoryReadModelRepository.cs @@ -5,20 +5,20 @@ namespace NCafe.Infrastructure.ReadModels; internal class InMemoryReadModelRepository : IReadModelRepository where T : ReadModel { - private static readonly ConcurrentDictionary items = new(); + private readonly static ConcurrentDictionary _items = new(); public void Add(T model) { - items[model.Id] = model; + _items[model.Id] = model; } public IEnumerable GetAll() { - return items.Values.ToList(); + return [.. _items.Values]; } public T GetById(Guid id) { - return items.TryGetValue(id, out var result) ? result : null; + return _items.TryGetValue(id, out var result) ? result : null; } } diff --git a/src/UI/NCafe.Web/Models/Product.cs b/src/UI/NCafe.Web/Models/Product.cs index baab514..a777ada 100644 --- a/src/UI/NCafe.Web/Models/Product.cs +++ b/src/UI/NCafe.Web/Models/Product.cs @@ -6,5 +6,8 @@ public class Product public string Name { get; set; } public decimal Price { get; set; } - public override string ToString() => Name; + public override string ToString() + { + return Name; + } }