Erweiterung der DTOs und Implementierung der Lokalisierungsdienste
- Neue DTO-Extension-Methoden hinzugefügt, um die Verarbeitung und Zuweisung von Nachrichten und Benachrichtigungen in Ergebnisobjekten zu vereinfachen. - Lokalisierungsunterstützung in der API-Schicht implementiert, einschließlich Cookie-basierter Lokalisierung und Konfiguration unterstützter Kulturen. - Die Integration von StringLocalizer in die API-Schicht wurde durchgeführt, um eine nahtlose Mehrsprachigkeit zu ermöglichen. - Fehlerbehandlung für fehlende Konfigurationseinstellungen verbessert. Die Änderungen verbessern die Flexibilität und Wartbarkeit des Codes und unterstützen eine effizientere Internationalisierung der Anwendung.
This commit is contained in:
@@ -4,6 +4,7 @@ using AutoMapper;
|
||||
using System.Reflection;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using DigitalData.Core.DTO;
|
||||
|
||||
namespace DigitalData.Core.Application
|
||||
{
|
||||
@@ -15,26 +16,29 @@ 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> : ServiceBase, ICRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>, IServiceBase
|
||||
public class CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ICRUDService<TCRUDRepository, 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;
|
||||
protected readonly IMapper _mapper;
|
||||
protected readonly PropertyInfo? _keyPropertyInfo;
|
||||
protected readonly IStringLocalizer _localizer;
|
||||
|
||||
/// <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="localizer">The localizer 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, IStringLocalizer defaultLocalizer, IMapper mapper) : base(defaultLocalizer)
|
||||
public CRUDService(TCRUDRepository repository, IStringLocalizer localizer, IMapper mapper)
|
||||
{
|
||||
_repository = repository;
|
||||
_mapper = mapper;
|
||||
|
||||
_keyPropertyInfo = typeof(TEntity).GetProperties()
|
||||
.FirstOrDefault(prop => Attribute.IsDefined(prop, typeof(KeyAttribute)));
|
||||
|
||||
_localizer = localizer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -42,14 +46,11 @@ namespace DigitalData.Core.Application
|
||||
/// </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)
|
||||
public virtual async Task<DataResult<TId>> CreateAsync(TCreateDto createDto)
|
||||
{
|
||||
var entity = _mapper.MapOrThrow<TEntity>(createDto);
|
||||
var createdEntity = await _repository.CreateAsync(entity);
|
||||
if (createdEntity is null)
|
||||
return Failed<TId>();
|
||||
else
|
||||
return Successful(KeyValueOf(createdEntity));
|
||||
return createdEntity is null ? Result.Fail<TId>() : Result.Success(KeyValueOf(createdEntity));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -57,27 +58,23 @@ namespace DigitalData.Core.Application
|
||||
/// </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)
|
||||
public virtual async Task<DataResult<TReadDto>> ReadByIdAsync(TId id)
|
||||
{
|
||||
var entity = await _repository.ReadByIdAsync(id);
|
||||
if (entity is null)
|
||||
{
|
||||
var translatedMessage = MessageKey.EntityDoesNotExist.LocalizedBy(_localizer);
|
||||
return Failed<TReadDto>();
|
||||
}
|
||||
else
|
||||
return Successful(_mapper.MapOrThrow<TReadDto>(entity));
|
||||
return entity is null
|
||||
? Result.Fail<TReadDto>().Message(_localizer[Key.EntityDoesNotExist])
|
||||
: Result.Success(_mapper.MapOrThrow<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()
|
||||
public virtual async Task<DataResult<IEnumerable<TReadDto>>> ReadAllAsync()
|
||||
{
|
||||
var entities = await _repository.ReadAllAsync();
|
||||
var readDto = _mapper.MapOrThrow<IEnumerable<TReadDto>>(entities);
|
||||
return Successful(readDto);
|
||||
return Result.Success(readDto);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -85,17 +82,11 @@ namespace DigitalData.Core.Application
|
||||
/// </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)
|
||||
public virtual async Task<Result> UpdateAsync(TUpdateDto updateDto)
|
||||
{
|
||||
var entity = _mapper.MapOrThrow<TEntity>(updateDto);
|
||||
bool isUpdated = await _repository.UpdateAsync(entity);
|
||||
if (isUpdated)
|
||||
return Successful();
|
||||
else
|
||||
{
|
||||
var translatedMessage = MessageKey.UpdateFailed.LocalizedBy(_localizer);
|
||||
return Failed();
|
||||
}
|
||||
return isUpdated ? Result.Success() : Result.Fail().Message(_localizer[Key.UpdateFailed]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -103,26 +94,15 @@ namespace DigitalData.Core.Application
|
||||
/// </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)
|
||||
public virtual async Task<Result> DeleteAsyncById(TId id)
|
||||
{
|
||||
TEntity? entity = await _repository.ReadByIdAsync(id);
|
||||
|
||||
if (entity is null)
|
||||
{
|
||||
var deletionFailedMessage = MessageKey.DeletionFailed.LocalizedBy(_localizer);
|
||||
var entityDoesNotExistMessage = MessageKey.EntityDoesNotExist.LocalizedBy(_localizer);
|
||||
return new ServiceMessage(isSuccess: false, deletionFailedMessage, entityDoesNotExistMessage);
|
||||
}
|
||||
return Result.Fail().Message(_localizer[Key.DeletionFailed], _localizer[Key.EntityDoesNotExist]);
|
||||
|
||||
bool isDeleted = await _repository.DeleteAsync(entity);
|
||||
|
||||
if (isDeleted)
|
||||
return Successful();
|
||||
else
|
||||
{
|
||||
var deletionFailedMessage = MessageKey.DeletionFailed.LocalizedBy(_localizer);
|
||||
return Failed(deletionFailedMessage);
|
||||
}
|
||||
return isDeleted ? Result.Success() : Result.Fail().Message(_localizer[Key.DeletionFailed]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user