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 { /// /// Provides extension methods to for registering Clean Architecture CRUD related services and repositories. /// public static class DIExtensions { /// /// Adds a basic CRUD service for a specific DTO and entity type to the service collection. /// /// The DTO type the service operates on. /// The entity type corresponding to the DTO. /// The type of the entity's identifier. /// The AutoMapper profile type for configuring mappings between the DTO and the entity. /// The to add the service to. /// An optional action to configure additional services for the CRUD service. /// The original instance, allowing further configuration. public static IServiceCollection AddCleanBasicCRUDService(this IServiceCollection services, Action? configureService = null) where TCRUDRepository : ICRUDRepository where TDto : class where TEntity : class where TProfile : Profile { services.AddScoped, BasicCRUDService>(); configureService?.Invoke(services); services.AddAutoMapper(typeof(TProfile).Assembly); return services; } /// /// 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. /// /// The repository type that provides CRUD operations for entities of type TEntity. /// The DTO type used for create operations. /// The DTO type used for read operations. /// The DTO type used for update operations. /// The entity type corresponding to the DTOs. /// The type of the entity's identifier. /// The AutoMapper profile type for configuring mappings between the DTOs and the entity. /// The to add the service to. /// An optional action to configure additional services for the CRUD service. /// The original instance, allowing further configuration. public static IServiceCollection AddCleanCRUDService(this IServiceCollection services, Action? configureService = null) where TCRUDRepository : ICRUDRepository where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class where TProfile : Profile { services.AddScoped, CRUDService>(); configureService?.Invoke(services); services.AddAutoMapper(typeof(TProfile).Assembly); return services; } public static IServiceCollection AddDirectorySearchService(this IServiceCollection service) { service.AddMemoryCache(); service.AddScoped(); return service; } public static IServiceCollection AddResponseService(this IServiceCollection service) { service.AddScoped(); return service; } public static IServiceCollection AddJWTService(this IServiceCollection services, Func tokenDescriptorFactory) { return services.AddScoped, JWTService>(provider => new (tokenDescriptorFactory)); } public static IServiceCollection AddCookieConsentSettings(this IServiceCollection services) { services.AddSingleton(sp => { var configuration = sp.GetRequiredService(); var settings = configuration.GetSection("CookieConsentSettings").Get(); if (settings is null) { throw new ConfigurationErrorsException("The 'CookieConsentSettings' section is missing or improperly configured in appsettings.json."); } return settings; }); return services; } } }