- shit: Cleanup this mess

This commit is contained in:
2025-01-14 22:10:16 +03:00
parent a0b2cfd677
commit 08e518602b
71 changed files with 1022 additions and 943 deletions

View File

@@ -7,47 +7,39 @@ public static class ServiceExt
{
public static void AddServices(this IServiceCollection services)
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
AddServices(services, assembly);
}
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) AddServices(services, assembly);
}
public static void AddServices(this IServiceCollection services, Assembly assembly)
{
foreach (var (type, inference) in GetServicesWithHelpAttribute(assembly))
{
Console.WriteLine("[ServiceMng] ADD SERVICE " + type);
if (inference is null)
{
services.AddSingleton(type);
}
else
{
services.AddSingleton(inference, type);
}
}
}
private static IEnumerable<(Type,Type?)> GetServicesWithHelpAttribute(Assembly assembly) {
foreach(Type type in assembly.GetTypes())
private static IEnumerable<(Type, Type?)> GetServicesWithHelpAttribute(Assembly assembly)
{
foreach (var type in assembly.GetTypes())
{
var attr = type.GetCustomAttribute<ServiceRegisterAttribute>();
if (attr is not null) {
yield return (type, attr.Inference);
}
if (attr is not null) yield return (type, attr.Inference);
}
}
}
public sealed class ServiceRegisterAttribute : Attribute
{
public Type? Inference { get; }
public bool IsSingleton { get; }
public ServiceRegisterAttribute(Type? inference = null, bool isSingleton = true)
{
IsSingleton = isSingleton;
Inference = inference;
}
public Type? Inference { get; }
public bool IsSingleton { get; }
}