From 3844f9d8d802106194f508a21fd9865edbbfb4af Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Sat, 15 Jun 2024 00:41:24 +0200 Subject: [PATCH] Refactored ICRUDService interface to remove the generic TCRUDRepository parameter for simplification and improved readability. --- .../BasicCRUDControllerBase.cs | 5 ++- DigitalData.Core.API/CRUDControllerBase.cs | 6 ++-- DigitalData.Core.API/ReadControllerBase.cs | 33 ++++++++++--------- .../BasicCRUDService.cs | 2 +- DigitalData.Core.Application/CRUDService.cs | 2 +- DigitalData.Core.Application/DIExtensions.cs | 4 +-- .../Application/IBasicCRUDService.cs | 5 ++- .../Application/ICRUDService.cs | 4 +-- 8 files changed, 29 insertions(+), 32 deletions(-) diff --git a/DigitalData.Core.API/BasicCRUDControllerBase.cs b/DigitalData.Core.API/BasicCRUDControllerBase.cs index 873878a..2a0cc5a 100644 --- a/DigitalData.Core.API/BasicCRUDControllerBase.cs +++ b/DigitalData.Core.API/BasicCRUDControllerBase.cs @@ -6,9 +6,8 @@ namespace DigitalData.Core.API { [ApiController] [Route("api/[controller]")] - public class BasicCRUDControllerBase : CRUDControllerBase - where TCRUDService : ICRUDService - where TCRUDRepository : ICRUDRepository + public class BasicCRUDControllerBase : CRUDControllerBase + where TCRUDService : ICRUDService where TDto : class where TEntity : class { diff --git a/DigitalData.Core.API/CRUDControllerBase.cs b/DigitalData.Core.API/CRUDControllerBase.cs index 52e18eb..725cdd8 100644 --- a/DigitalData.Core.API/CRUDControllerBase.cs +++ b/DigitalData.Core.API/CRUDControllerBase.cs @@ -1,5 +1,4 @@ using DigitalData.Core.Contracts.Application; -using DigitalData.Core.Contracts.Infrastructure; using DigitalData.Core.DTO; using Microsoft.AspNetCore.Mvc; @@ -16,9 +15,8 @@ namespace DigitalData.Core.API /// The type of the entity's identifier. [ApiController] [Route("api/[controller]")] - public class CRUDControllerBase : ControllerBase - where TCRUDService : ICRUDService - where TCRUDRepository : ICRUDRepository + public class CRUDControllerBase : ControllerBase + where TCRUDService : ICRUDService where TCreateDto : class where TReadDto : class where TUpdateDto : class diff --git a/DigitalData.Core.API/ReadControllerBase.cs b/DigitalData.Core.API/ReadControllerBase.cs index 5142fbe..1847448 100644 --- a/DigitalData.Core.API/ReadControllerBase.cs +++ b/DigitalData.Core.API/ReadControllerBase.cs @@ -1,5 +1,5 @@ using DigitalData.Core.Contracts.Application; -using DigitalData.Core.Contracts.Infrastructure; +using DigitalData.Core.DTO; using Microsoft.AspNetCore.Mvc; namespace DigitalData.Core.API @@ -12,9 +12,8 @@ namespace DigitalData.Core.API /// The type of the entity's identifier. [ApiController] [Route("api/[controller]")] - public class ReadControllerBase : ControllerBase - where TBasicCRUDService : IBasicCRUDService - where TCRUDRepository : ICRUDRepository + public class ReadControllerBase : ControllerBase + where TBasicCRUDService : IBasicCRUDService where TReadDto : class where TEntity : class { @@ -42,12 +41,13 @@ namespace DigitalData.Core.API [HttpGet("{id}")] public virtual async Task GetById([FromRoute] TId id) { - var result = await _service.ReadByIdAsync(id); - if (result.IsSuccess) - { - return Ok(result); - } - return NotFound(result); + return await _service.ReadByIdAsync(id).ThenAsync( + Success: Ok, + Fail: IActionResult (messages, notices) => + { + _logger.LogNotice(notices); + return NotFound(messages); + }); } /// @@ -57,12 +57,13 @@ namespace DigitalData.Core.API [HttpGet] public virtual async Task GetAll() { - var result = await _service.ReadAllAsync(); - if (result.IsSuccess) - { - return Ok(result); - } - return NotFound(result); + return await _service.ReadAllAsync().ThenAsync( + Success: Ok, + Fail: IActionResult (messages, notices) => + { + _logger.LogNotice(notices); + return NotFound(messages); + }); } } } \ No newline at end of file diff --git a/DigitalData.Core.Application/BasicCRUDService.cs b/DigitalData.Core.Application/BasicCRUDService.cs index c8d5835..9446a14 100644 --- a/DigitalData.Core.Application/BasicCRUDService.cs +++ b/DigitalData.Core.Application/BasicCRUDService.cs @@ -20,7 +20,7 @@ namespace DigitalData.Core.Application /// and a culture-specific translation service for any necessary text translations, ensuring a versatile and internationalized approach to CRUD operations. /// public class BasicCRUDService : - CRUDService, IBasicCRUDService + CRUDService, IBasicCRUDService where TCRUDRepository : ICRUDRepository where TDto : class where TEntity : class { /// diff --git a/DigitalData.Core.Application/CRUDService.cs b/DigitalData.Core.Application/CRUDService.cs index 6aead52..8d45196 100644 --- a/DigitalData.Core.Application/CRUDService.cs +++ b/DigitalData.Core.Application/CRUDService.cs @@ -15,7 +15,7 @@ namespace DigitalData.Core.Application /// The DTO type for update operations. /// The entity type. /// The type of the identifier for the entity. - public class CRUDService : ICRUDService + public class CRUDService : ICRUDService where TCRUDRepository : ICRUDRepository where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class { protected readonly TCRUDRepository _repository; diff --git a/DigitalData.Core.Application/DIExtensions.cs b/DigitalData.Core.Application/DIExtensions.cs index 31983f6..a9fd013 100644 --- a/DigitalData.Core.Application/DIExtensions.cs +++ b/DigitalData.Core.Application/DIExtensions.cs @@ -25,7 +25,7 @@ namespace DigitalData.Core.Application public static IServiceCollection AddCleanBasicCRUDService(this IServiceCollection services, Action? configureService = null) where TCRUDRepository : ICRUDRepository where TDto : class where TEntity : class where TProfile : Profile { - services.AddScoped, BasicCRUDService>(); + services.AddScoped, BasicCRUDService>(); configureService?.Invoke(services); services.AddAutoMapper(typeof(TProfile).Assembly); @@ -49,7 +49,7 @@ namespace DigitalData.Core.Application public static IServiceCollection AddCleanCRUDService(this IServiceCollection services, Action? configureService = null) where TCRUDRepository : ICRUDRepository where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class where TProfile : Profile { - services.AddScoped, CRUDService>(); + services.AddScoped, CRUDService>(); configureService?.Invoke(services); services.AddAutoMapper(typeof(TProfile).Assembly); diff --git a/DigitalData.Core.Contracts/Application/IBasicCRUDService.cs b/DigitalData.Core.Contracts/Application/IBasicCRUDService.cs index 05ba156..7f035e9 100644 --- a/DigitalData.Core.Contracts/Application/IBasicCRUDService.cs +++ b/DigitalData.Core.Contracts/Application/IBasicCRUDService.cs @@ -8,7 +8,6 @@ namespace DigitalData.Core.Contracts.Application /// This interface inherits from the ICRUDService interface, applying the same DTO type for all generic type parameters, /// thereby simplifying the usage for cases where a single DTO is sufficient for all operations on an entity. /// - /// The repository type that provides CRUD operations for entities of type TEntity. /// The type of the Data Transfer Object used for all CRUD operations. /// The type of the entity this service maps to. /// The type of the identifier for the entity. @@ -16,8 +15,8 @@ namespace DigitalData.Core.Contracts.Application /// This interface is useful for entities that do not require different DTOs for different operations, /// allowing for a more concise and maintainable codebase when implementing services for such entities. /// - public interface IBasicCRUDService : ICRUDService - where TCRUDRepository : ICRUDRepository where TDto : class where TEntity : class + public interface IBasicCRUDService : ICRUDService + where TDto : class where TEntity : class { } } \ No newline at end of file diff --git a/DigitalData.Core.Contracts/Application/ICRUDService.cs b/DigitalData.Core.Contracts/Application/ICRUDService.cs index 43fac72..ec5921d 100644 --- a/DigitalData.Core.Contracts/Application/ICRUDService.cs +++ b/DigitalData.Core.Contracts/Application/ICRUDService.cs @@ -3,8 +3,8 @@ using DigitalData.Core.DTO; namespace DigitalData.Core.Contracts.Application { - public interface ICRUDService - where TCRUDRepository : ICRUDRepository where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class + public interface ICRUDService + where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class { Task> CreateAsync(TCreateDto createDto);