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
{
///
/// 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;
}
///
/// Adds the directory search service to the .
///
/// The to add services to.
///
/// Optional. An instance of to configure the directory search service.
/// If not provided, the options need to be configured separately.
///
/// The updated .
///
/// This method adds the necessary services for directory search functionality, including memory caching.
/// If is not provided, ensure to configure the options separately
/// using the pattern.
///
public static IServiceCollection AddDirectorySearchService(this IServiceCollection service, DirectorySearchOptions? directorySearchOptions = null)
{
if(directorySearchOptions is not null)
service.AddSingleton(Options.Create(directorySearchOptions));
return service
.AddMemoryCache()
.AddScoped();
}
public static IServiceCollection AddJWTService(this IServiceCollection services, Func tokenDescriptorFactory)
{
return services.AddScoped, JWTService>(provider => new (tokenDescriptorFactory));
}
}
}