feat: ReadService hinzugefügt und in ReadController integriert
- Generischen ReadService erstellt, um Lese- (ReadById, ReadAll) und Löschoperationen zu verwalten. - ReadService in den ReadController integriert.
This commit is contained in:
parent
2c739fbf02
commit
993d407a48
@ -1,3 +1,4 @@
|
|||||||
|
using DigitalData.Core.Abstractions;
|
||||||
using DigitalData.Core.Abstractions.Application;
|
using DigitalData.Core.Abstractions.Application;
|
||||||
using DigitalData.Core.DTO;
|
using DigitalData.Core.DTO;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@ -19,8 +20,8 @@ namespace DigitalData.Core.API
|
|||||||
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
|
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
|
||||||
where TCreateDto : class
|
where TCreateDto : class
|
||||||
where TReadDto : class
|
where TReadDto : class
|
||||||
where TUpdateDto : class
|
where TUpdateDto : class, IUnique<TId>
|
||||||
where TEntity : class
|
where TEntity : class, IUnique<TId>
|
||||||
{
|
{
|
||||||
protected readonly ILogger _logger;
|
protected readonly ILogger _logger;
|
||||||
protected readonly TCRUDService _service;
|
protected readonly TCRUDService _service;
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
using DigitalData.Core.Abstractions;
|
||||||
using DigitalData.Core.Abstractions.Application;
|
using DigitalData.Core.Abstractions.Application;
|
||||||
using DigitalData.Core.DTO;
|
using DigitalData.Core.DTO;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@ -20,8 +21,8 @@ namespace DigitalData.Core.API
|
|||||||
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
|
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
|
||||||
where TCreateDto : class
|
where TCreateDto : class
|
||||||
where TReadDto : class
|
where TReadDto : class
|
||||||
where TUpdateDto : class
|
where TUpdateDto : class, IUnique<TId>
|
||||||
where TEntity : class
|
where TEntity : class, IUnique<TId>
|
||||||
{
|
{
|
||||||
protected readonly ILogger _logger;
|
protected readonly ILogger _logger;
|
||||||
protected readonly TCRUDService _service;
|
protected readonly TCRUDService _service;
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
using DigitalData.Core.Abstractions;
|
|
||||||
using DigitalData.Core.Abstractions.Application;
|
using DigitalData.Core.Abstractions.Application;
|
||||||
using DigitalData.Core.DTO;
|
using DigitalData.Core.DTO;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@ -13,13 +12,13 @@ namespace DigitalData.Core.API
|
|||||||
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
|
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
public class ReadControllerBase<TBasicCRUDService, TReadDto, TEntity, TId> : ControllerBase
|
public class ReadControllerBase<TReadService, TReadDto, TEntity, TId> : ControllerBase
|
||||||
where TBasicCRUDService : IBasicCRUDService<TReadDto, TEntity, TId>
|
where TReadService : IReadService<TReadDto, TEntity, TId>
|
||||||
where TReadDto : class
|
where TReadDto : class
|
||||||
where TEntity : class, IUnique<TId>
|
where TEntity : class
|
||||||
{
|
{
|
||||||
protected readonly ILogger _logger;
|
protected readonly ILogger _logger;
|
||||||
protected readonly TBasicCRUDService _service;
|
protected readonly TReadService _service;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the CRUDControllerBase class with specified logger and CRUD service.
|
/// Initializes a new instance of the CRUDControllerBase class with specified logger and CRUD service.
|
||||||
@ -28,7 +27,7 @@ namespace DigitalData.Core.API
|
|||||||
/// <param name="service">The CRUD service handling business logic for the entity.</param>
|
/// <param name="service">The CRUD service handling business logic for the entity.</param>
|
||||||
public ReadControllerBase(
|
public ReadControllerBase(
|
||||||
ILogger logger,
|
ILogger logger,
|
||||||
TBasicCRUDService service)
|
TReadService service)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_service = service;
|
_service = service;
|
||||||
|
|||||||
@ -13,13 +13,13 @@ namespace DigitalData.Core.API
|
|||||||
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
|
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
public class ReadControllerBaseWithErrorHandling<TBasicCRUDService, TReadDto, TEntity, TId> : ControllerBase
|
public class ReadControllerBaseWithErrorHandling<TReadService, TReadDto, TEntity, TId> : ControllerBase
|
||||||
where TBasicCRUDService : IBasicCRUDService<TReadDto, TEntity, TId>
|
where TReadService : IReadService<TReadDto, TEntity, TId>
|
||||||
where TReadDto : class
|
where TReadDto : class
|
||||||
where TEntity : class
|
where TEntity : class
|
||||||
{
|
{
|
||||||
protected readonly ILogger _logger;
|
protected readonly ILogger _logger;
|
||||||
protected readonly TBasicCRUDService _service;
|
protected readonly TReadService _service;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the CRUDControllerBase class with specified logger and CRUD service.
|
/// Initializes a new instance of the CRUDControllerBase class with specified logger and CRUD service.
|
||||||
@ -28,7 +28,7 @@ namespace DigitalData.Core.API
|
|||||||
/// <param name="service">The CRUD service handling business logic for the entity.</param>
|
/// <param name="service">The CRUD service handling business logic for the entity.</param>
|
||||||
public ReadControllerBaseWithErrorHandling(
|
public ReadControllerBaseWithErrorHandling(
|
||||||
ILogger logger,
|
ILogger logger,
|
||||||
TBasicCRUDService service)
|
TReadService service)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_service = service;
|
_service = service;
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
namespace DigitalData.Core.Abstractions.Application
|
namespace DigitalData.Core.Abstractions.Application
|
||||||
{
|
{
|
||||||
public interface IReadService<TReadDto, TEntity, TId>
|
public interface IReadService<TReadDto, TEntity, TId>
|
||||||
where TReadDto : class where TEntity : class, IUnique<TId>
|
where TReadDto : class where TEntity : class
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieves an entity by its identifier and returns its readDTO representation wrapped in an IServiceResult,
|
/// Retrieves an entity by its identifier and returns its readDTO representation wrapped in an IServiceResult,
|
||||||
|
|||||||
@ -15,21 +15,17 @@ namespace DigitalData.Core.Application
|
|||||||
/// <typeparam name="TUpdateDto">The DTO type for update operations.</typeparam>
|
/// <typeparam name="TUpdateDto">The DTO type for update operations.</typeparam>
|
||||||
/// <typeparam name="TEntity">The entity type.</typeparam>
|
/// <typeparam name="TEntity">The entity type.</typeparam>
|
||||||
/// <typeparam name="TId">The type of the identifier for the entity.</typeparam>
|
/// <typeparam name="TId">The type of the identifier for the entity.</typeparam>
|
||||||
public class CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
|
public class CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ReadService<TCRUDRepository, TReadDto, TEntity, TId>, ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
|
||||||
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TUpdateDto : IUnique<TId> where TEntity : class, IUnique<TId>
|
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TUpdateDto : IUnique<TId> where TEntity : class, IUnique<TId>
|
||||||
{
|
{
|
||||||
protected readonly TCRUDRepository _repository;
|
|
||||||
protected readonly IMapper _mapper;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the CRUDService class with the specified repository, translation service, and mapper.
|
/// Initializes a new instance of the CRUDService class with the specified repository, translation service, and mapper.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="repository">The CRUD repository for accessing the database.</param>
|
/// <param name="repository">The CRUD repository for accessing the database.</param>
|
||||||
/// <param name="mapper">The AutoMapper instance for mapping between DTOs and entity objects.</param>
|
/// <param name="mapper">The AutoMapper instance for mapping between DTOs and entity objects.</param>
|
||||||
public CRUDService(TCRUDRepository repository, IMapper mapper)
|
public CRUDService(TCRUDRepository repository, IMapper mapper) : base(repository: repository, mapper: mapper)
|
||||||
{
|
{
|
||||||
_repository = repository;
|
|
||||||
_mapper = mapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -44,30 +40,6 @@ namespace DigitalData.Core.Application
|
|||||||
return createdEntity is null ? Result.Fail<TId>() : Result.Success(createdEntity.Id);
|
return createdEntity is null ? Result.Fail<TId>() : Result.Success(createdEntity.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously reads an entity by its identifier and maps it to a read DTO.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="id">The identifier of the entity to read.</param>
|
|
||||||
/// <returns>A service result indicating success or failure, including the read DTO if successful.</returns>
|
|
||||||
public virtual async Task<DataResult<TReadDto>> ReadByIdAsync(TId id)
|
|
||||||
{
|
|
||||||
var entity = await _repository.ReadByIdAsync(id);
|
|
||||||
return entity is null
|
|
||||||
? Result.Fail<TReadDto>()
|
|
||||||
: Result.Success(_mapper.Map<TReadDto>(entity));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously reads all entities and maps them to read DTOs.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>A service result including a collection of read DTOs.</returns>
|
|
||||||
public virtual async Task<DataResult<IEnumerable<TReadDto>>> ReadAllAsync()
|
|
||||||
{
|
|
||||||
var entities = await _repository.ReadAllAsync();
|
|
||||||
var readDto = _mapper.Map<IEnumerable<TReadDto>>(entities);
|
|
||||||
return Result.Success(readDto);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously updates an entity based on the provided update DTO.
|
/// Asynchronously updates an entity based on the provided update DTO.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -86,28 +58,5 @@ namespace DigitalData.Core.Application
|
|||||||
? Result.Success()
|
? Result.Success()
|
||||||
: Result.Fail();
|
: Result.Fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously deletes an entity by its identifier.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="id">The identifier of the entity to delete.</param>
|
|
||||||
/// <returns>A service message indicating success or failure.</returns>
|
|
||||||
public virtual async Task<Result> DeleteAsyncById(TId id)
|
|
||||||
{
|
|
||||||
TEntity? entity = await _repository.ReadByIdAsync(id);
|
|
||||||
|
|
||||||
if (entity is null)
|
|
||||||
return Result.Fail();
|
|
||||||
|
|
||||||
bool isDeleted = await _repository.DeleteAsync(entity);
|
|
||||||
return isDeleted ? Result.Success() : Result.Fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Asynchronously checks if an entity with the specified identifier exists.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="id">The identifier of the entity to check.</param>
|
|
||||||
/// <returns>A Task that represents the asynchronous operation. The task result contains a boolean value indicating whether the entity exists.</returns>
|
|
||||||
public virtual async Task<bool> HasEntity(TId id) => await _repository.CountAsync(id) > 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
79
DigitalData.Core.Application/ReadService.cs
Normal file
79
DigitalData.Core.Application/ReadService.cs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
using DigitalData.Core.Abstractions.Application;
|
||||||
|
using DigitalData.Core.Abstractions.Infrastructure;
|
||||||
|
using AutoMapper;
|
||||||
|
using DigitalData.Core.DTO;
|
||||||
|
using DigitalData.Core.Abstractions;
|
||||||
|
|
||||||
|
namespace DigitalData.Core.Application
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Provides generic Read (Read and Delete) operations for a specified type of entity.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TReadDto">The DTO type for read operations.</typeparam>
|
||||||
|
/// <typeparam name="TEntity">The entity type.</typeparam>
|
||||||
|
/// <typeparam name="TId">The type of the identifier for the entity.</typeparam>
|
||||||
|
public class ReadService<TCRUDRepository, TReadDto, TEntity, TId> : IReadService<TReadDto, TEntity, TId>
|
||||||
|
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TReadDto : class where TEntity : class, IUnique<TId>
|
||||||
|
{
|
||||||
|
protected readonly TCRUDRepository _repository;
|
||||||
|
protected readonly IMapper _mapper;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the CRUDService class with the specified repository, translation service, and mapper.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="repository">The CRUD repository for accessing the database.</param>
|
||||||
|
/// <param name="mapper">The AutoMapper instance for mapping between DTOs and entity objects.</param>
|
||||||
|
public ReadService(TCRUDRepository repository, IMapper mapper)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously reads an entity by its identifier and maps it to a read DTO.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The identifier of the entity to read.</param>
|
||||||
|
/// <returns>A service result indicating success or failure, including the read DTO if successful.</returns>
|
||||||
|
public virtual async Task<DataResult<TReadDto>> ReadByIdAsync(TId id)
|
||||||
|
{
|
||||||
|
var entity = await _repository.ReadByIdAsync(id);
|
||||||
|
return entity is null
|
||||||
|
? Result.Fail<TReadDto>()
|
||||||
|
: Result.Success(_mapper.Map<TReadDto>(entity));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously reads all entities and maps them to read DTOs.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A service result including a collection of read DTOs.</returns>
|
||||||
|
public virtual async Task<DataResult<IEnumerable<TReadDto>>> ReadAllAsync()
|
||||||
|
{
|
||||||
|
var entities = await _repository.ReadAllAsync();
|
||||||
|
var readDto = _mapper.Map<IEnumerable<TReadDto>>(entities);
|
||||||
|
return Result.Success(readDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously deletes an entity by its identifier.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The identifier of the entity to delete.</param>
|
||||||
|
/// <returns>A service message indicating success or failure.</returns>
|
||||||
|
public virtual async Task<Result> DeleteAsyncById(TId id)
|
||||||
|
{
|
||||||
|
TEntity? entity = await _repository.ReadByIdAsync(id);
|
||||||
|
|
||||||
|
if (entity is null)
|
||||||
|
return Result.Fail();
|
||||||
|
|
||||||
|
bool isDeleted = await _repository.DeleteAsync(entity);
|
||||||
|
return isDeleted ? Result.Success() : Result.Fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously checks if an entity with the specified identifier exists.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The identifier of the entity to check.</param>
|
||||||
|
/// <returns>A Task that represents the asynchronous operation. The task result contains a boolean value indicating whether the entity exists.</returns>
|
||||||
|
public virtual async Task<bool> HasEntity(TId id) => await _repository.CountAsync(id) > 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,23 +0,0 @@
|
|||||||
using AutoMapper;
|
|
||||||
|
|
||||||
namespace DigitalData.Core.DTO
|
|
||||||
{
|
|
||||||
public static class AutoMapperExtension
|
|
||||||
{
|
|
||||||
[Obsolete("use mapper.Map<T>")]
|
|
||||||
/// <summary>
|
|
||||||
/// Maps a source object to a destination object, or throws an exception if the mapping result is null.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TSource">The source object type.</typeparam>
|
|
||||||
/// <typeparam name="TDestination">The destination object type.</typeparam>
|
|
||||||
/// <param name="source">The source object to map from.</param>
|
|
||||||
/// <returns>The mapped destination object.</returns>
|
|
||||||
/// <exception cref="MappingResultNullException">Thrown when the mapping result is null.</exception>
|
|
||||||
public static TDestination MapOrThrow<TDestination>(this IMapper mapper, object source)
|
|
||||||
{
|
|
||||||
return mapper.Map<TDestination>(source) ?? throw new AutoMapperMappingException(
|
|
||||||
$"Mapping to {typeof(TDestination).FullName} resulted in a null object. " +
|
|
||||||
"Hint: Ensure that the AutoMapper profile configuration for this mapping is correct.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user