27 lines
1.5 KiB
C#

using DigitalData.Core.Abstractions.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.DirectoryServices;
namespace DigitalData.Core.Infrastructure
{
public static class DIExtensions
{
/// <summary>
/// Adds a CRUD repository for a specific entity type to the service collection.
/// </summary>
/// <typeparam name="TEntity">The entity type for which the repository is registered.</typeparam>
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
/// <typeparam name="TDbContext">The DbContext type used by the repository.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the repository to.</param>
/// <param name="configureRepository">An optional action to configure additional services for the repository.</param>
/// <returns>The original <see cref="IServiceCollection"/> instance, allowing further configuration.</returns>
public static IServiceCollection AddCleanCRUDRepository<TEntity, TId, TDbContext, TCRUDRepository>(this IServiceCollection services, Action<IServiceCollection>? configureRepository = null)
where TCRUDRepository : CRUDRepository<TEntity, TId, TDbContext> where TEntity : class where TDbContext : DbContext
{
services.AddScoped<ICRUDRepository<TEntity, TId>, TCRUDRepository>();
configureRepository?.Invoke(services);
return services;
}
}
}