Developer 02 4615205aa5 refactor: Aktualisierung der Anwendungs- und Infrastrukturebenen, so dass die Infrastruktur von der Anwendung abhängig ist.
- Repository-Schnittstellen wurden in die Anwendungsschicht verschoben.
 - Erweiterungsmethoden für die Injektion von Repository-Abhängigkeiten wurden in die Infrastruktur verschoben.
2025-02-12 19:31:13 +01:00

59 lines
2.4 KiB
C#

using AutoMapper;
using DigitalData.Core.Application;
using DigitalData.Core.DTO;
using EnvelopeGenerator.Application.Contracts.Services;
using EnvelopeGenerator.Application.DTOs;
using EnvelopeGenerator.Domain.Entities;
using EnvelopeGenerator.Application.Contracts.Repositories;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
namespace EnvelopeGenerator.Application.Services;
public class ConfigService : ReadService<IConfigRepository, ConfigDto, Config, int>, IConfigService
{
private static readonly Guid DefaultConfigCacheId = Guid.NewGuid();
private readonly IMemoryCache _cache;
private readonly ILogger<ConfigService> _logger;
public ConfigService(IConfigRepository repository, IMapper mapper, IMemoryCache memoryCache, ILogger<ConfigService> logger) : base(repository, mapper)
{
_cache = memoryCache;
_logger = logger;
}
public async Task<DataResult<ConfigDto>> ReadFirstAsync()
{
var config = await _repository.ReadFirstAsync();
return config is null
? Result.Fail<ConfigDto>().Notice(LogLevel.Error, Flag.DataIntegrityIssue, "There is no configuration in DB.")
: Result.Success(_mapper.Map<ConfigDto>(config));
}
/// <summary>
/// Reads the default configuration asynchronously.
/// </summary>
/// <remarks>
/// The configuration is cached in memory upon the first retrieval. If the configuration is updated,
/// the application needs to be restarted for the changes to take effect as the memory cache will not be updated automatically.
/// </remarks>
/// <returns>
/// A task that represents the asynchronous read operation. The task result contains the default configuration as a <see cref="ConfigDto"/>.
/// </returns>
/// <exception cref="InvalidOperationException">
/// Thrown when the default configuration cannot be found.
/// </exception>
public async Task<ConfigDto> ReadDefaultAsync()
{
var config = await _cache.GetOrCreateAsync(DefaultConfigCacheId, _ => ReadFirstAsync().ThenAsync(
Success: config => config,
Fail: (mssg, ntc) =>
{
_logger.LogNotice(ntc);
throw new InvalidOperationException("Default configuration cannot find.");
}));
return config!;
}
public async Task<string> ReadDefaultSignatureHost() => (await ReadDefaultAsync()).SignatureHost;
}