Developer 02 0bf8979a09 refactor: Einführung des IUnique<TId>-Interfaces für Entitäten und DTOs
- Aktualisiertes IBasicCRUDService-Interface, um Konsistenz bei einzigartigen Identifikatoren mit IUnique<TId> durchzusetzen
- DIExtensions-Methoden angepasst, um IUnique<TId>-Einschränkungen für DTOs und Entitäten einzuschließen
2024-09-13 16:30:52 +02:00

100 lines
6.6 KiB
C#

using AutoMapper;
using DigitalData.Core.Abstractions;
using DigitalData.Core.Abstractions.Application;
using DigitalData.Core.Abstractions.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
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, IUnique<TId> where TEntity : class, IUnique<TId> where TProfile : Profile
{
services.AddScoped<IBasicCRUDService<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, IUnique<TId> where TEntity : class, IUnique<TId> where TProfile : Profile
{
services.AddScoped<ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>, CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>>();
configureService?.Invoke(services);
services.AddAutoMapper(typeof(TProfile).Assembly);
return services;
}
/// <summary>
/// Adds the directory search service to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="service">The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="directorySearchOptions">
/// Optional. An instance of <see cref="DirectorySearchOptions"/> to configure the directory search service.
/// If not provided, the options need to be configured separately.
/// </param>
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
/// <remarks>
/// This method adds the necessary services for directory search functionality, including memory caching.
/// If <paramref name="directorySearchOptions"/> is not provided, ensure to configure the options separately
/// using the <see cref="IOptions{TOptions}"/> pattern.
/// </remarks>
public static IServiceCollection AddDirectorySearchService(this IServiceCollection service, DirectorySearchOptions? directorySearchOptions = null)
{
if(directorySearchOptions is not null)
service.AddSingleton(Options.Create(directorySearchOptions));
return service
.AddMemoryCache()
.AddScoped<IDirectorySearchService, DirectorySearchService>();
}
/// <summary>
/// Adds the JWT service to the <see cref="IServiceCollection"/>.
/// </summary>
/// <typeparam name="TClaimValue">The type of the claim value used in the JWT token.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the service to.</param>
/// <param name="tokenDescriptorFactory">A function that takes a claim value of type <typeparamref name="TClaimValue"/> and returns a <see cref="SecurityTokenDescriptor"/> used to configure the JWT token.</param>
/// <returns>The original <see cref="IServiceCollection"/> instance, allowing further configuration.</returns>
/// <remarks>
/// This method adds the necessary services for handling JWT tokens. The <paramref name="tokenDescriptorFactory"/> function is used to generate the <see cref="SecurityTokenDescriptor"/> which is essential for creating the JWT tokens.
/// </remarks>
public static IServiceCollection AddJWTService<TClaimValue>(this IServiceCollection services, Func<TClaimValue, SecurityTokenDescriptor> tokenDescriptorFactory)
{
return services.AddScoped<IJWTService<TClaimValue>, JWTService<TClaimValue>>(provider => new (tokenDescriptorFactory));
}
}
}