refactor(Application): Verbesserung der CRUDService zur Steigerung der Typflexibilität und Wartbarkeit
- Entfernen redundanter generischer Einschränkungen für `TUpdateDto` in `CRUDService`. - Aktualisierung der Methode `UpdateAsync`, um einen generischen Parameter für eine bessere Typflexibilität von `TUpdateDto` einzuschließen. - Verbesserung der Typ-Einschränkungen durch Durchsetzung von `IUnique<TId>` direkt im Methodenumfang, wo zutreffend. - Vereinfachung der Klasse zur Einhaltung bewährter Praktiken im Design generischer Dienste.
This commit is contained in:
parent
7dd91c73c4
commit
8d9de4502e
@ -7,7 +7,7 @@ namespace DigitalData.Core.API
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
public class BasicCRUDControllerBase<TCRUDService, TDto, TEntity, TId> : CRUDControllerBase<TCRUDService, TDto, TDto, TDto, 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 TCRUDService : ICRUDService<TDto, TDto, TEntity, TId>
|
||||||
where TDto : class, IUnique<TId>
|
where TDto : class, IUnique<TId>
|
||||||
where TEntity : class, IUnique<TId>
|
where TEntity : class, IUnique<TId>
|
||||||
{
|
{
|
||||||
|
|||||||
@ -11,13 +11,12 @@ namespace DigitalData.Core.API
|
|||||||
/// <typeparam name="TCRUDService">The derived CRUD service type implementing ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>.</typeparam>
|
/// <typeparam name="TCRUDService">The derived CRUD service type implementing ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>.</typeparam>
|
||||||
/// <typeparam name="TCreateDto">The Data Transfer Object type for create operations.</typeparam>
|
/// <typeparam name="TCreateDto">The Data Transfer Object type for create operations.</typeparam>
|
||||||
/// <typeparam name="TReadDto">The Data Transfer Object type for read operations.</typeparam>
|
/// <typeparam name="TReadDto">The Data Transfer Object type for read operations.</typeparam>
|
||||||
/// <typeparam name="TUpdateDto">The Data Transfer Object type for update operations.</typeparam>
|
|
||||||
/// <typeparam name="TEntity">The entity type CRUD operations will be performed on.</typeparam>
|
/// <typeparam name="TEntity">The entity type CRUD operations will be performed on.</typeparam>
|
||||||
/// <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 CRUDControllerBase<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
|
public class CRUDControllerBase<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
|
||||||
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
|
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TEntity, TId>
|
||||||
where TCreateDto : class
|
where TCreateDto : class
|
||||||
where TReadDto : class
|
where TReadDto : class
|
||||||
where TUpdateDto : class, IUnique<TId>
|
where TUpdateDto : class, IUnique<TId>
|
||||||
|
|||||||
@ -12,13 +12,12 @@ namespace DigitalData.Core.API
|
|||||||
/// <typeparam name="TCRUDService">The derived CRUD service type implementing ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>.</typeparam>
|
/// <typeparam name="TCRUDService">The derived CRUD service type implementing ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>.</typeparam>
|
||||||
/// <typeparam name="TCreateDto">The Data Transfer Object type for create operations.</typeparam>
|
/// <typeparam name="TCreateDto">The Data Transfer Object type for create operations.</typeparam>
|
||||||
/// <typeparam name="TReadDto">The Data Transfer Object type for read operations.</typeparam>
|
/// <typeparam name="TReadDto">The Data Transfer Object type for read operations.</typeparam>
|
||||||
/// <typeparam name="TUpdateDto">The Data Transfer Object type for update operations.</typeparam>
|
|
||||||
/// <typeparam name="TEntity">The entity type CRUD operations will be performed on.</typeparam>
|
/// <typeparam name="TEntity">The entity type CRUD operations will be performed on.</typeparam>
|
||||||
/// <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 CRUDControllerBaseWithErrorHandling<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
|
public class CRUDControllerBaseWithErrorHandling<TCRUDService, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
|
||||||
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
|
where TCRUDService : ICRUDService<TCreateDto, TReadDto, TEntity, TId>
|
||||||
where TCreateDto : class
|
where TCreateDto : class
|
||||||
where TReadDto : class
|
where TReadDto : class
|
||||||
where TUpdateDto : class, IUnique<TId>
|
where TUpdateDto : class, IUnique<TId>
|
||||||
|
|||||||
@ -15,7 +15,7 @@ namespace DigitalData.Core.Abstractions.Application
|
|||||||
/// This interface is useful for entities that do not require different DTOs for different operations,
|
/// 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.
|
/// allowing for a more concise and maintainable codebase when implementing services for such entities.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public interface IBasicCRUDService<TDto, TEntity, TId> : ICRUDService<TDto, TDto, TDto, TEntity, TId>
|
public interface IBasicCRUDService<TDto, TEntity, TId> : ICRUDService<TDto, TDto, TEntity, TId>
|
||||||
where TDto : class, IUnique<TId> where TEntity : class, IUnique<TId>
|
where TDto : class, IUnique<TId> where TEntity : class, IUnique<TId>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
namespace DigitalData.Core.Abstractions.Application
|
namespace DigitalData.Core.Abstractions.Application
|
||||||
{
|
{
|
||||||
public interface ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : IReadService<TReadDto, TEntity, TId>
|
public interface ICRUDService<TCreateDto, TReadDto, TEntity, TId> : IReadService<TReadDto, TEntity, TId>
|
||||||
where TCreateDto : class where TReadDto : class where TUpdateDto : IUnique<TId> where TEntity : class, IUnique<TId>
|
where TCreateDto : class where TReadDto : class where TEntity : class, IUnique<TId>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously creates a new entity based on the provided <paramref name="createDto"/> and returns the identifier of the created entity wrapped in a <see cref="DataResult{TId}"/>.
|
/// Asynchronously creates a new entity based on the provided <paramref name="createDto"/> and returns the identifier of the created entity wrapped in a <see cref="DataResult{TId}"/>.
|
||||||
@ -20,6 +20,6 @@ namespace DigitalData.Core.Abstractions.Application
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="updateDto">The updateDTO with updated values for the entity.</param>
|
/// <param name="updateDto">The updateDTO with updated values for the entity.</param>
|
||||||
/// <returns>An Result indicating the outcome of the update operation, with an appropriate message.</returns>
|
/// <returns>An Result indicating the outcome of the update operation, with an appropriate message.</returns>
|
||||||
Task<Result> UpdateAsync(TUpdateDto updateDto);
|
Task<Result> UpdateAsync<TUpdateDto>(TUpdateDto updateDto) where TUpdateDto : IUnique<TId>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -19,7 +19,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.
|
/// and a culture-specific translation service for any necessary text translations, ensuring a versatile and internationalized approach to CRUD operations.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public class BasicCRUDService<TCRUDRepository, TDto, TEntity, TId> :
|
public class BasicCRUDService<TCRUDRepository, TDto, TEntity, TId> :
|
||||||
CRUDService<TCRUDRepository, TDto, TDto, TDto, TEntity, TId>, IBasicCRUDService<TDto, TEntity, TId>
|
CRUDService<TCRUDRepository, TDto, TDto, TEntity, TId>, IBasicCRUDService<TDto, TEntity, TId>
|
||||||
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TDto : class, IUnique<TId> where TEntity : class, IUnique<TId>
|
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TDto : class, IUnique<TId> where TEntity : class, IUnique<TId>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -12,11 +12,10 @@ namespace DigitalData.Core.Application
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TCreateDto">The DTO type for create operations.</typeparam>
|
/// <typeparam name="TCreateDto">The DTO type for create operations.</typeparam>
|
||||||
/// <typeparam name="TReadDto">The DTO type for read operations.</typeparam>
|
/// <typeparam name="TReadDto">The DTO type for read 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> : ReadService<TCRUDRepository, TReadDto, TEntity, TId>, ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
|
public class CRUDService<TCRUDRepository, TCreateDto, TReadDto, TEntity, TId> : ReadService<TCRUDRepository, TReadDto, TEntity, TId>, ICRUDService<TCreateDto, TReadDto, 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 TEntity : class, IUnique<TId>
|
||||||
{
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -45,7 +44,7 @@ namespace DigitalData.Core.Application
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="updateDto">The DTO to update an entity from.</param>
|
/// <param name="updateDto">The DTO to update an entity from.</param>
|
||||||
/// <returns>A service message indicating success or failure.</returns>
|
/// <returns>A service message indicating success or failure.</returns>
|
||||||
public virtual async Task<Result> UpdateAsync(TUpdateDto updateDto)
|
public virtual async Task<Result> UpdateAsync<TUpdateDto>(TUpdateDto updateDto) where TUpdateDto : IUnique<TId>
|
||||||
{
|
{
|
||||||
var currentEntitiy = await _repository.ReadByIdAsync(updateDto.Id);
|
var currentEntitiy = await _repository.ReadByIdAsync(updateDto.Id);
|
||||||
|
|
||||||
|
|||||||
@ -40,7 +40,6 @@ namespace DigitalData.Core.Application
|
|||||||
/// <typeparam name="TCRUDRepository">The repository type that provides CRUD operations for entities of type TEntity.</typeparam>
|
/// <typeparam name="TCRUDRepository">The repository type that provides CRUD operations for entities of type TEntity.</typeparam>
|
||||||
/// <typeparam name="TCreateDto">The DTO type used for create operations.</typeparam>
|
/// <typeparam name="TCreateDto">The DTO type used for create operations.</typeparam>
|
||||||
/// <typeparam name="TReadDto">The DTO type used for read operations.</typeparam>
|
/// <typeparam name="TReadDto">The DTO type used for read operations.</typeparam>
|
||||||
/// <typeparam name="TUpdateDto">The DTO type used for update operations.</typeparam>
|
|
||||||
/// <typeparam name="TEntity">The entity type corresponding to the DTOs.</typeparam>
|
/// <typeparam name="TEntity">The entity type corresponding to the DTOs.</typeparam>
|
||||||
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
|
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
|
||||||
/// <typeparam name="TProfile">The AutoMapper profile type for configuring mappings between the DTOs and the entity.</typeparam>
|
/// <typeparam name="TProfile">The AutoMapper profile type for configuring mappings between the DTOs and the entity.</typeparam>
|
||||||
@ -48,9 +47,9 @@ namespace DigitalData.Core.Application
|
|||||||
/// <param name="configureService">An optional action to configure additional services for the CRUD service.</param>
|
/// <param name="configureService">An optional action to configure additional services for the CRUD service.</param>
|
||||||
/// <returns>The original <see cref="IServiceCollection"/> instance, allowing further configuration.</returns>
|
/// <returns>The original <see cref="IServiceCollection"/> instance, allowing further configuration.</returns>
|
||||||
public static IServiceCollection AddCleanCRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId, TProfile>(this IServiceCollection services, Action<IServiceCollection>? configureService = null)
|
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, IUnique<TId> where TEntity : class, IUnique<TId> where TProfile : Profile
|
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class, IUnique<TId> where TEntity : class, IUnique<TId> where TProfile : Profile
|
||||||
{
|
{
|
||||||
services.AddScoped<ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>, CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>>();
|
services.AddScoped<ICRUDService<TCreateDto, TReadDto, TEntity, TId>, CRUDService<TCRUDRepository, TCreateDto, TReadDto, TEntity, TId>>();
|
||||||
configureService?.Invoke(services);
|
configureService?.Invoke(services);
|
||||||
|
|
||||||
services.AddAutoMapper(typeof(TProfile).Assembly);
|
services.AddAutoMapper(typeof(TProfile).Assembly);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user