89 lines
5.6 KiB
C#
89 lines
5.6 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Contracts.Application;
|
|
using DigitalData.Core.Contracts.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 where TEntity : class 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 where TEntity : class 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>();
|
|
}
|
|
|
|
public static IServiceCollection AddJWTService<TClaimValue>(this IServiceCollection services, Func<TClaimValue, SecurityTokenDescriptor> tokenDescriptorFactory)
|
|
{
|
|
return services.AddScoped<IJWTService<TClaimValue>, JWTService<TClaimValue>>(provider => new (tokenDescriptorFactory));
|
|
}
|
|
}
|
|
} |