Refactored ICRUDService interface to remove the generic TCRUDRepository parameter for simplification and improved readability.

This commit is contained in:
Developer 02
2024-06-15 00:41:24 +02:00
parent 0697f5ff58
commit 3844f9d8d8
8 changed files with 29 additions and 32 deletions

View File

@@ -6,9 +6,8 @@ namespace DigitalData.Core.API
{
[ApiController]
[Route("api/[controller]")]
public class BasicCRUDControllerBase<TCRUDService, TCRUDRepository, TDto, TEntity, TId> : CRUDControllerBase<TCRUDService, TCRUDRepository, TDto, TDto, TDto, TEntity, TId>
where TCRUDService : ICRUDService<TCRUDRepository, TDto, TDto, TDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId>
public class BasicCRUDControllerBase<TCRUDService, TDto, TEntity, TId> : CRUDControllerBase<TCRUDService, TDto, TDto, TDto, TEntity, TId>
where TCRUDService : ICRUDService<TDto, TDto, TDto, TEntity, TId>
where TDto : class
where TEntity : class
{

View File

@@ -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
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
[ApiController]
[Route("api/[controller]")]
public class CRUDControllerBase<TCRUDService, TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
where TCRUDService : ICRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId>
public class CRUDControllerBase<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
where TCreateDto : class
where TReadDto : class
where TUpdateDto : class

View File

@@ -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
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
[ApiController]
[Route("api/[controller]")]
public class ReadControllerBase<TBasicCRUDService, TCRUDRepository, TReadDto, TEntity, TId> : ControllerBase
where TBasicCRUDService : IBasicCRUDService<TCRUDRepository, TReadDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId>
public class ReadControllerBase<TBasicCRUDService, TReadDto, TEntity, TId> : ControllerBase
where TBasicCRUDService : IBasicCRUDService<TReadDto, TEntity, TId>
where TReadDto : class
where TEntity : class
{
@@ -42,12 +41,13 @@ namespace DigitalData.Core.API
[HttpGet("{id}")]
public virtual async Task<IActionResult> 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);
});
}
/// <summary>
@@ -57,12 +57,13 @@ namespace DigitalData.Core.API
[HttpGet]
public virtual async Task<IActionResult> 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);
});
}
}
}

View File

@@ -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.
/// </remarks>
public class BasicCRUDService<TCRUDRepository, TDto, TEntity, TId> :
CRUDService<TCRUDRepository, TDto, TDto, TDto, TEntity, TId>, IBasicCRUDService<TCRUDRepository, TDto, TEntity, TId>
CRUDService<TCRUDRepository, TDto, TDto, TDto, TEntity, TId>, IBasicCRUDService<TDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TDto : class where TEntity : class
{
/// <summary>

View File

@@ -15,7 +15,7 @@ namespace DigitalData.Core.Application
/// <typeparam name="TUpdateDto">The DTO type for update operations.</typeparam>
/// <typeparam name="TEntity">The entity type.</typeparam>
/// <typeparam name="TId">The type of the identifier for the entity.</typeparam>
public class CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ICRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
public class CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class
{
protected readonly TCRUDRepository _repository;

View File

@@ -25,7 +25,7 @@ namespace DigitalData.Core.Application
public static IServiceCollection AddCleanBasicCRUDService<TCRUDRepository, TDto, TEntity, TId, TProfile>(this IServiceCollection services, Action<IServiceCollection>? configureService = null)
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TDto : class where TEntity : class where TProfile : Profile
{
services.AddScoped<IBasicCRUDService<TCRUDRepository, TDto, TEntity, TId>, BasicCRUDService<TCRUDRepository, TDto, TEntity, TId>>();
services.AddScoped<IBasicCRUDService<TDto, TEntity, TId>, BasicCRUDService<TCRUDRepository, TDto, TEntity, TId>>();
configureService?.Invoke(services);
services.AddAutoMapper(typeof(TProfile).Assembly);
@@ -49,7 +49,7 @@ namespace DigitalData.Core.Application
public static IServiceCollection AddCleanCRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId, TProfile>(this IServiceCollection services, Action<IServiceCollection>? configureService = null)
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class where TProfile : Profile
{
services.AddScoped<ICRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>, CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>>();
services.AddScoped<ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>, CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>>();
configureService?.Invoke(services);
services.AddAutoMapper(typeof(TProfile).Assembly);

View File

@@ -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.
/// </summary>
/// <typeparam name="TCRUDRepository">The repository type that provides CRUD operations for entities of type TEntity.</typeparam>
/// <typeparam name="TDto">The type of the Data Transfer Object used for all CRUD operations.</typeparam>
/// <typeparam name="TEntity">The type of the entity this service maps to.</typeparam>
/// <typeparam name="TId">The type of the identifier for the entity.</typeparam>
@@ -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.
/// </remarks>
public interface IBasicCRUDService<TCRUDRepository, TDto, TEntity, TId> : ICRUDService<TCRUDRepository, TDto, TDto, TDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TDto : class where TEntity : class
public interface IBasicCRUDService<TDto, TEntity, TId> : ICRUDService<TDto, TDto, TDto, TEntity, TId>
where TDto : class where TEntity : class
{
}
}

View File

@@ -3,8 +3,8 @@ using DigitalData.Core.DTO;
namespace DigitalData.Core.Contracts.Application
{
public interface ICRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class
public interface ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class
{
Task<DataResult<TId>> CreateAsync(TCreateDto createDto);