Developer 02 82da3586a5 Implementierung von AddCookieConsentSettings zum Laden der Cookie-Zustimmungseinstellungen aus der Konfiguration
Eine Methode wurde hinzugefügt, um die CookieConsentSettings aus der appsettings.json als Singleton zu laden und zu registrieren. Diese Einstellungen verwalten die Benutzerzustimmung für Cookies. Beinhaltet Fehlerbehandlung für fehlende oder falsche Konfigurationen.
2024-04-15 14:15:12 +02:00

95 lines
5.5 KiB
C#

using AutoMapper;
using DigitalData.Core.Application.DTO;
using DigitalData.Core.Contracts.Application;
using DigitalData.Core.Contracts.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System.Configuration;
namespace DigitalData.Core.Application
{
/// <summary>
/// Provides extension methods to <see cref="IServiceCollection"/> for registering Clean Architecture CRUD related services and repositories.
/// </summary>
public static class DIExtensions
{
/// <summary>
/// Adds a basic CRUD service for a specific DTO and entity type to the service collection.
/// </summary>
/// <typeparam name="TDto">The DTO type the service operates on.</typeparam>
/// <typeparam name="TEntity">The entity type corresponding to the DTO.</typeparam>
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
/// <typeparam name="TProfile">The AutoMapper profile type for configuring mappings between the DTO and the entity.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</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>
public static IServiceCollection AddCleanBasicCRUDService<TCRUDRepository, TDto, TEntity, TId, TProfile>(this IServiceCollection services, Action<IServiceCollection>? configureService = null)
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TDto : class where TEntity : class where TProfile : Profile
{
services.AddScoped<IBasicCRUDService<TCRUDRepository, TDto, TEntity, TId>, BasicCRUDService<TCRUDRepository, TDto, TEntity, TId>>();
configureService?.Invoke(services);
services.AddAutoMapper(typeof(TProfile).Assembly);
return services;
}
/// <summary>
/// Adds a CRUD service for managing create, read, update, and delete operations for a specific set of DTOs and an entity type to the service collection.
/// </summary>
/// <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="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="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>
/// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</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>
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 where TEntity : class where TProfile : Profile
{
services.AddScoped<ICRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>, CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>>();
configureService?.Invoke(services);
services.AddAutoMapper(typeof(TProfile).Assembly);
return services;
}
public static IServiceCollection AddDirectorySearchService(this IServiceCollection service)
{
service.AddMemoryCache();
service.AddScoped<IDirectorySearchService, DirectorySearchService>();
return service;
}
public static IServiceCollection AddResponseService(this IServiceCollection service)
{
service.AddScoped<IResponseService, ResponseService>();
return service;
}
public static IServiceCollection AddJWTService<TClaimValue>(this IServiceCollection services, Func<TClaimValue, SecurityTokenDescriptor> tokenDescriptorFactory)
{
return services.AddScoped<IJWTService<TClaimValue>, JWTService<TClaimValue>>(provider => new (tokenDescriptorFactory));
}
public static IServiceCollection AddCookieConsentSettings(this IServiceCollection services)
{
services.AddSingleton<CookieConsentSettings>(sp =>
{
var configuration = sp.GetRequiredService<IConfiguration>();
var settings = configuration.GetSection("CookieConsentSettings").Get<CookieConsentSettings>();
if (settings is null)
{
throw new ConfigurationErrorsException("The 'CookieConsentSettings' section is missing or improperly configured in appsettings.json.");
}
return settings;
});
return services;
}
}
}