/// <summary> /// Registers the first discovered occurrence of /// </summary> /// <param name="collection">Service collection</param> /// <param name="classFinder">EasyIoC finder</param> /// <param name="Environment">Environment to register instances for</param> public static void RegisterDependencies( this IServiceCollection collection, IClassFinder classFinder, string Environment = null) { var assemblyFinder = new AssemblyFinder(); var assemblies = assemblyFinder.FindAssemblies(null); var classesToRegister = classFinder.FindRegisteredClasses(assemblies); var serviceCollection = new ServiceContainer(collection); List <Discoverable> environmentBasedClasses = new List <Discoverable>(); // Pushing onto this list gives us a way to use the non-environment // based classes first, if none was specified foreach (var item in classesToRegister) { if (Environment == null && item.Environment != null) { environmentBasedClasses.Add(item); } else { classFinder.RegisterClass(item.Type, serviceCollection, Environment); } } foreach (var item in classesToRegister) { classFinder.RegisterClass(item.Type, serviceCollection, Environment); } }
public static void RegisterDependencies( this IServiceCollection collection, IClassFinder classFinder, Func <string, bool> canRegister) { var assemblyFinder = new AssemblyFinder(); var assemblies = assemblyFinder.FindAssemblies(null); Dictionary <string, bool> registeredResponses = new Dictionary <string, bool>(); var classesToRegister = classFinder.FindRegisteredClasses(assemblies); var serviceCollection = new ServiceContainer(collection); foreach (var item in classesToRegister) { if (!registeredResponses.ContainsKey(item.Environment)) { var envCanRegister = canRegister(item.Environment); registeredResponses.Add(item.Environment, envCanRegister); } if (registeredResponses[item.Environment]) { classFinder.RegisterClass(item.Type, serviceCollection, null); } } }