UserPointManagement/src/UserPointManagement.Persist.../RepositoryInjectExtensions.cs

27 lines
939 B
C#

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace UserPointManagement.Persistence
{
public static class RepositoryInjectExtensions
{
public static IServiceCollection AddRepositories(this IServiceCollection services)
{
var assembly = typeof(RepositoryInjectExtensions).Assembly;
var types = assembly
.GetTypes()
.Where(t =>
!t.IsGenericType &&
!t.IsAbstract &&
t.IsClass &&
t.Name.EndsWith("Repository"))
.ToList();
foreach (var type in types)
{
var baseType = type.GetInterfaces().FirstOrDefault(t => t.Name == $"I{type.Name}");
services.TryAddScoped(baseType, type);
}
return services;
}
}
}