2024-03-06 16:14:36 +01:00

200 lines
10 KiB
C#

using DigitalData.Core.Contracts.CleanArchitecture.Application;
using DigitalData.Core.Contracts.CleanArchitecture.Infrastructure;
using DigitalData.Core.Contracts.CultureServices;
using AutoMapper;
using DigitalData.Core.Exceptions;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
namespace DigitalData.Core.CleanArchitecture.Application
{
/// <summary>
/// Provides generic CRUD (Create, Read, Update, Delete) operations for a specified type of entity.
/// </summary>
/// <typeparam name="TCreateDto">The DTO type for create 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="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>, IServiceReplier
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class
{
protected readonly TCRUDRepository _repository;
protected readonly IMapper _mapper;
protected readonly IKeyTranslationService _translationService;
protected readonly PropertyInfo _keyPropertyInfo;
/// <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="translationService">The service for translating messages based on culture.</param>
/// <param name="mapper">The AutoMapper instance for mapping between DTOs and entity objects.</param>
public CRUDService(TCRUDRepository repository, IKeyTranslationService translationService, IMapper mapper)
{
_repository = repository;
_translationService = translationService;
_mapper = mapper;
_keyPropertyInfo = typeof(TEntity).GetProperties()
.FirstOrDefault(prop => Attribute.IsDefined(prop, typeof(KeyAttribute)))
?? throw new InvalidOperationException($"No property with [Key] attribute found on {typeof(TEntity).Name} entity.");
}
/// <summary>
/// Asynchronously creates an entity based on the provided create DTO.
/// </summary>
/// <param name="createDto">The DTO to create an entity from.</param>
/// <returns>A service result indicating success or failure, including the entity DTO.</returns>
public virtual async Task<IServiceResult<TId>> CreateAsync(TCreateDto createDto)
{
var entity = MapOrThrow<TCreateDto, TEntity>(createDto);
var createdEntity = await _repository.CreateAsync(entity);
if(createdEntity is null)
return Failed<TId>(default);
else
return Successful(KeyValueOf(createdEntity));
}
/// <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<IServiceResult<TReadDto>> ReadByIdAsync(TId id)
{
var entity = await _repository.ReadByIdAsync(id);
if (entity is null)
{
var translatedMessage = _translationService.Translate(MessageKey.EntityDoesNotExist);
return FailedResult<TReadDto>();
}
else
return Successful(MapOrThrow<TEntity, 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<IServiceResult<IEnumerable<TReadDto>>> ReadAllAsync()
{
var entities = await _repository.ReadAllAsync();
var readDto = MapOrThrow<IEnumerable<TEntity>, IEnumerable<TReadDto>>(entities);
return Successful(readDto);
}
/// <summary>
/// Asynchronously updates an entity based on the provided update DTO.
/// </summary>
/// <param name="updateDto">The DTO to update an entity from.</param>
/// <returns>A service message indicating success or failure.</returns>
public virtual async Task<IServiceMessage> UpdateAsync(TUpdateDto updateDto)
{
var entity = MapOrThrow<TUpdateDto, TEntity>(updateDto);
bool isUpdated = await _repository.UpdateAsync(entity);
if (isUpdated)
return Successful();
else
{
var translatedMessage = _translationService.Translate(MessageKey.UpdateFailed);
return Failed(translatedMessage);
}
}
/// <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<IServiceMessage> DeleteAsyncById(TId id)
{
TEntity? entity = await _repository.ReadByIdAsync(id);
if (entity is null)
{
var deletionFailedMessage = _translationService.Translate(MessageKey.DeletionFailed);
var entityDoesNotExistMessage = _translationService.Translate(MessageKey.EntityDoesNotExist);
return new ServiceMessage(isSuccess: false, deletionFailedMessage, entityDoesNotExistMessage);
}
bool isDeleted = await _repository.DeleteAsync(entity);
if (isDeleted)
return Successful();
else
{
var deletionFailedMessage = _translationService.Translate(MessageKey.DeletionFailed);
return Failed(deletionFailedMessage);
}
}
/// <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)
{
var entity = await _repository.ReadByIdAsync(id);
return entity is not null;
}
/// <summary>
/// Retrieves the ID value of an entity based on the defined [Key] attribute.
/// </summary>
/// <param name="entity">The entity from which to extract the ID.</param>
/// <returns>The ID of the entity.</returns>
protected virtual TId KeyValueOf(TEntity entity)
{
object idObj = _keyPropertyInfo.GetValue(entity) ?? throw new InvalidOperationException($"The ID property of {typeof(TEntity).Name} entity cannot be null.");
if (idObj is TId id)
return id;
else
throw new InvalidCastException($"The ID of {typeof(TEntity).Name} entity must be type of {typeof(TId).Name}, but it is type of {idObj.GetType().Name}.");
}
/// <summary>
/// Handles exceptions that occur during CRUD operations, providing a structured string.
/// </summary>
/// <param name="ex">The exception that was caught during CRUD operations.</param>
/// <returns>A <see cref="IServiceMessage"/> containing information about the failure, including a user-friendly error message and additional error details.</returns>
public virtual string HandleException(Exception ex)
{
return $"An unexpected error occurred on the server side. Please inform the IT support team.\n{ex.GetType().Name}\n{ex.Message}";
}
/// <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>
protected TDestination MapOrThrow<TSource, TDestination>(TSource source)
{
return _mapper.Map<TDestination>(source) ?? throw new MappingResultNullException(typeof(TSource), typeof(TDestination));
}
public virtual IServiceMessage CreateMessage(bool isSuccess, params string[] messages)
{
return new ServiceMessage(isSuccess, messages);
}
public virtual IServiceResult<TData> CreateResult<TData>(TData? data, bool isSuccess = true, params string[] messages)
{
return new ServiceResult<TData>(data, isSuccess, messages);
}
public virtual IServiceMessage Successful() => CreateMessage(true);
public virtual IServiceMessage Failed(params string[] messages) => CreateMessage(false, messages);
public virtual IServiceResult<T> Successful<T>(T data) => CreateResult(data);
public virtual IServiceResult<T> Failed<T>(T? data, params string[] messages) => CreateResult(data, false, messages);
public virtual IServiceResult<T> FailedResult<T>(params string[] messages) => Failed<T>(default, messages);
}
}