| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.Injection.Reflection.PluginFactories; |
| | | 5 | | |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Reflection; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.Injection.Reflection; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Builds and configures an <see cref="IServiceCollection"/> using reflection-based plugin discovery. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// This builder uses <see cref="ReflectionPluginFactory"/> for plugin discovery and is not AOT compatible. |
| | | 16 | | /// </remarks> |
| | | 17 | | [DoNotAutoRegister] |
| | | 18 | | [RequiresUnreferencedCode("ReflectionServiceProviderBuilder uses reflection for plugin discovery.")] |
| | | 19 | | public sealed class ReflectionServiceProviderBuilder : IServiceProviderBuilder |
| | | 20 | | { |
| | | 21 | | private readonly IServiceCollectionPopulator _serviceCollectionPopulator; |
| | | 22 | | private readonly Lazy<IReadOnlyList<Assembly>> _lazyCandidateAssemblies; |
| | | 23 | | private readonly ReflectionPluginFactory _pluginFactory; |
| | | 24 | | |
| | | 25 | | public ReflectionServiceProviderBuilder( |
| | | 26 | | IServiceCollectionPopulator serviceCollectionPopulator, |
| | | 27 | | IAssemblyProvider assemblyProvider) : |
| | 0 | 28 | | this( |
| | 0 | 29 | | serviceCollectionPopulator, |
| | 0 | 30 | | assemblyProvider, |
| | 0 | 31 | | additionalAssemblies: []) |
| | | 32 | | { |
| | 0 | 33 | | } |
| | | 34 | | |
| | 288 | 35 | | public ReflectionServiceProviderBuilder( |
| | 288 | 36 | | IServiceCollectionPopulator serviceCollectionPopulator, |
| | 288 | 37 | | IAssemblyProvider assemblyProvider, |
| | 288 | 38 | | IReadOnlyList<Assembly> additionalAssemblies) |
| | | 39 | | { |
| | 288 | 40 | | ArgumentNullException.ThrowIfNull(serviceCollectionPopulator); |
| | 288 | 41 | | ArgumentNullException.ThrowIfNull(assemblyProvider); |
| | 288 | 42 | | ArgumentNullException.ThrowIfNull(additionalAssemblies); |
| | | 43 | | |
| | 288 | 44 | | _serviceCollectionPopulator = serviceCollectionPopulator; |
| | 288 | 45 | | _pluginFactory = new ReflectionPluginFactory(); |
| | 288 | 46 | | _lazyCandidateAssemblies = new(() => |
| | 288 | 47 | | { |
| | 288 | 48 | | var staticAssemblies = assemblyProvider.GetCandidateAssemblies(); |
| | 288 | 49 | | HashSet<string> uniqueAssemblyNames = new(StringComparer.OrdinalIgnoreCase); |
| | 288 | 50 | | List<Assembly> allCandidateAssemblies = new(additionalAssemblies.Count + staticAssemblies.Count); |
| | 288 | 51 | | |
| | 1260 | 52 | | foreach (var assembly in staticAssemblies) |
| | 288 | 53 | | { |
| | 342 | 54 | | var name = assembly.FullName ?? assembly.GetName().Name ?? string.Empty; |
| | 342 | 55 | | if (uniqueAssemblyNames.Add(name)) |
| | 288 | 56 | | { |
| | 342 | 57 | | allCandidateAssemblies.Add(assembly); |
| | 288 | 58 | | } |
| | 288 | 59 | | } |
| | 288 | 60 | | |
| | 590 | 61 | | foreach (var assembly in additionalAssemblies) |
| | 288 | 62 | | { |
| | 7 | 63 | | var name = assembly.FullName ?? assembly.GetName().Name ?? string.Empty; |
| | 7 | 64 | | if (uniqueAssemblyNames.Add(name)) |
| | 288 | 65 | | { |
| | 0 | 66 | | allCandidateAssemblies.Add(assembly); |
| | 288 | 67 | | } |
| | 288 | 68 | | } |
| | 288 | 69 | | |
| | 288 | 70 | | return allCandidateAssemblies.Distinct().ToArray(); |
| | 288 | 71 | | }); |
| | 288 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | 569 | 75 | | public IReadOnlyList<Assembly> GetCandidateAssemblies() => _lazyCandidateAssemblies.Value; |
| | | 76 | | |
| | | 77 | | /// <inheritdoc /> |
| | | 78 | | public IServiceProvider Build( |
| | | 79 | | IConfiguration config) => |
| | 1 | 80 | | Build( |
| | 1 | 81 | | services: new ServiceCollection(), |
| | 1 | 82 | | config: config); |
| | | 83 | | |
| | | 84 | | /// <inheritdoc /> |
| | | 85 | | public IServiceProvider Build( |
| | | 86 | | IServiceCollection services, |
| | | 87 | | IConfiguration config) => |
| | 1 | 88 | | Build( |
| | 1 | 89 | | services: services, |
| | 1 | 90 | | config: config, |
| | 1 | 91 | | postPluginRegistrationCallbacks: []); |
| | | 92 | | |
| | | 93 | | /// <inheritdoc/> |
| | | 94 | | public IServiceProvider Build( |
| | | 95 | | IServiceCollection services, |
| | | 96 | | IConfiguration config, |
| | | 97 | | IReadOnlyList<Action<IServiceCollection>> postPluginRegistrationCallbacks) => |
| | 1 | 98 | | Build(services, config, [], postPluginRegistrationCallbacks); |
| | | 99 | | |
| | | 100 | | /// <inheritdoc/> |
| | | 101 | | public IServiceProvider Build( |
| | | 102 | | IServiceCollection services, |
| | | 103 | | IConfiguration config, |
| | | 104 | | IReadOnlyList<Action<IServiceCollection>> preRegistrationCallbacks, |
| | | 105 | | IReadOnlyList<Action<IServiceCollection>> postPluginRegistrationCallbacks) |
| | | 106 | | { |
| | 248 | 107 | | ArgumentNullException.ThrowIfNull(services); |
| | 248 | 108 | | ArgumentNullException.ThrowIfNull(config); |
| | 248 | 109 | | ArgumentNullException.ThrowIfNull(preRegistrationCallbacks); |
| | 248 | 110 | | ArgumentNullException.ThrowIfNull(postPluginRegistrationCallbacks); |
| | | 111 | | |
| | 248 | 112 | | services.AddSingleton(config); |
| | | 113 | | |
| | 514 | 114 | | foreach (var callback in preRegistrationCallbacks) |
| | | 115 | | { |
| | 9 | 116 | | callback.Invoke(services); |
| | | 117 | | } |
| | | 118 | | |
| | 248 | 119 | | _serviceCollectionPopulator.RegisterToServiceCollection( |
| | 248 | 120 | | services, |
| | 248 | 121 | | config, |
| | 248 | 122 | | GetCandidateAssemblies()); |
| | | 123 | | |
| | 1604 | 124 | | foreach (var callback in postPluginRegistrationCallbacks) |
| | | 125 | | { |
| | 555 | 126 | | callback.Invoke(services); |
| | | 127 | | } |
| | | 128 | | |
| | 246 | 129 | | var provider = services.BuildServiceProvider(); |
| | | 130 | | |
| | 246 | 131 | | ConfigurePostBuildServiceCollectionPlugins(provider, config); |
| | | 132 | | |
| | 246 | 133 | | return provider; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <inheritdoc /> |
| | | 137 | | public void ConfigurePostBuildServiceCollectionPlugins( |
| | | 138 | | IServiceProvider provider, |
| | | 139 | | IConfiguration config) |
| | | 140 | | { |
| | 281 | 141 | | ArgumentNullException.ThrowIfNull(provider); |
| | 281 | 142 | | ArgumentNullException.ThrowIfNull(config); |
| | | 143 | | |
| | 281 | 144 | | var candidateAssemblies = GetCandidateAssemblies(); |
| | 281 | 145 | | PostBuildServiceCollectionPluginOptions options = new( |
| | 281 | 146 | | provider, |
| | 281 | 147 | | config, |
| | 281 | 148 | | candidateAssemblies, |
| | 281 | 149 | | _pluginFactory); |
| | | 150 | | |
| | 562 | 151 | | foreach (var plugin in _pluginFactory.CreatePluginsFromAssemblies<IPostBuildServiceCollectionPlugin>(candidateAs |
| | | 152 | | { |
| | 0 | 153 | | plugin.Configure(options); |
| | | 154 | | } |
| | 281 | 155 | | } |
| | | 156 | | } |