shoc-backend/SeaHaven.Services/DependencyInjection/ServicesModule.cs
2026-05-14 11:00:12 -05:00

38 lines
1.2 KiB
C#

using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
namespace SeaHaven.Services.DependencyInjection
{
public static class ServicesModule
{
private static readonly List<Type> ExcludedTypes = new();
public static IServiceCollection AddBusinessServices(this IServiceCollection services)
{
var assembly = Assembly.GetExecutingAssembly();
var allClasses = assembly.GetTypes()
.Where(t => t.IsClass &&
!t.IsAbstract &&
!t.IsGenericType &&
!ExcludedTypes.Contains(t))
.ToList();
foreach (var implementationType in allClasses)
{
var defaultInterface = implementationType.GetInterfaces()
.FirstOrDefault(i => i.Name == $"I{implementationType.Name}");
if (defaultInterface != null)
{
services.AddScoped(defaultInterface, implementationType);
}
}
services.AddValidatorsFromAssembly(assembly);
return services;
}
}
}