Hinzugefügt wurden die JWTService-Klasse und das IJWTService-Interface zur Unterstützung der Generierung und Validierung von JWTs basierend auf Anspruchstypen. Enthält Methoden zum Erstellen von Sicherheitsschlüsseln, Generieren von Tokens und Lesen von Tokens mit ausführlicher Dokumentation.
77 lines
4.7 KiB
C#
77 lines
4.7 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Contracts.Application;
|
|
using DigitalData.Core.Contracts.Infrastructure;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
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));
|
|
}
|
|
}
|
|
} |