From 00ac10cd63f8c28d0403193af01b9417e08b6d67 Mon Sep 17 00:00:00 2001 From: arenekosreal <17194552+arenekosreal@users.noreply.github.com> Date: Mon, 25 Nov 2024 19:53:28 +0800 Subject: [PATCH] Squash 2 foreach-loops into 1 Also skip modules loading if it is loaded --- E5Renewer/IServiceCollectionExtends.cs | 37 +++++++++++--------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/E5Renewer/IServiceCollectionExtends.cs b/E5Renewer/IServiceCollectionExtends.cs index 376a3be..8bdcdde 100644 --- a/E5Renewer/IServiceCollectionExtends.cs +++ b/E5Renewer/IServiceCollectionExtends.cs @@ -40,30 +40,23 @@ public static IServiceCollection AddUserSecretFile(this IServiceCollection servi public static IServiceCollection AddModules(this IServiceCollection services, params Assembly[] assemblies) { - foreach (Assembly assembly in assemblies) + Type[] knownModulesTypes = [ + typeof(IModulesChecker), + typeof(IUserSecretLoader), + typeof(IGraphAPICaller), + typeof(IAPIFunction) + ]; + foreach (Type t in assemblies.SelectMany((assembly) => assembly.IterE5RenewerModules())) { - foreach (Type t in assembly.IterE5RenewerModules()) + ServiceDescriptor service = ServiceDescriptor.Transient( + knownModulesTypes.FirstOrDefault( + (kt) => t.IsAssignableTo(kt), + typeof(IModule)), + t + ); + if (!services.Contains(service)) { - if (t.IsAssignableTo(typeof(IModulesChecker))) - { - services.AddTransient(typeof(IModulesChecker), t); - } - else if (t.IsAssignableTo(typeof(IUserSecretLoader))) - { - services.AddTransient(typeof(IUserSecretLoader), t); - } - else if (t.IsAssignableTo(typeof(IGraphAPICaller))) - { - services.AddTransient(typeof(IGraphAPICaller), t); - } - else if (t.IsAssignableTo(typeof(IAPIFunction))) - { - services.AddTransient(typeof(IAPIFunction), t); - } - else if (t.IsAssignableTo(typeof(IModule))) - { - services.AddTransient(typeof(IModule), t); - } + services.Add(service); } } return services;