| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Reflection; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.Injection; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Default implementation of <see cref="IServiceCollectionPopulator"/> that registers discovered types into a service c |
| | | 11 | | /// Handles type registration, plugin execution, and built-in service factory registration. |
| | | 12 | | /// </summary> |
| | | 13 | | [DoNotAutoRegister] |
| | | 14 | | public sealed class ServiceCollectionPopulator : IServiceCollectionPopulator |
| | | 15 | | { |
| | | 16 | | private readonly ITypeRegistrar _typeRegistrar; |
| | | 17 | | private readonly ITypeFilterer _typeFilterer; |
| | | 18 | | private readonly IPluginFactory _pluginFactory; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="ServiceCollectionPopulator"/> class. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="typeRegistrar">The type registrar to use for service registration.</param> |
| | | 24 | | /// <param name="typeFilterer">The type filterer to determine service lifetimes.</param> |
| | | 25 | | /// <param name="pluginFactory">The plugin factory to use for plugin discovery and instantiation.</param> |
| | 535 | 26 | | public ServiceCollectionPopulator( |
| | 535 | 27 | | ITypeRegistrar typeRegistrar, |
| | 535 | 28 | | ITypeFilterer typeFilterer, |
| | 535 | 29 | | IPluginFactory pluginFactory) |
| | | 30 | | { |
| | 535 | 31 | | ArgumentNullException.ThrowIfNull(typeRegistrar); |
| | 535 | 32 | | ArgumentNullException.ThrowIfNull(typeFilterer); |
| | 535 | 33 | | ArgumentNullException.ThrowIfNull(pluginFactory); |
| | | 34 | | |
| | 535 | 35 | | _typeRegistrar = typeRegistrar; |
| | 535 | 36 | | _typeFilterer = typeFilterer; |
| | 535 | 37 | | _pluginFactory = pluginFactory; |
| | 535 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public IServiceCollection RegisterToServiceCollection( |
| | | 42 | | IServiceCollection services, |
| | | 43 | | IConfiguration config, |
| | | 44 | | IReadOnlyList<Assembly> candidateAssemblies) |
| | | 45 | | { |
| | 535 | 46 | | ArgumentNullException.ThrowIfNull(services); |
| | 535 | 47 | | ArgumentNullException.ThrowIfNull(config); |
| | 535 | 48 | | ArgumentNullException.ThrowIfNull(candidateAssemblies); |
| | | 49 | | |
| | 535 | 50 | | services.AddSingleton(services); |
| | 535 | 51 | | services.AddSingleton(provider => provider); |
| | 535 | 52 | | services.AddSingleton(typeof(Lazy<>), typeof(LazyFactory<>)); |
| | 535 | 53 | | services.AddSingleton(typeof(IReadOnlyList<>), typeof(ReadOnlyListFactory<>)); |
| | 535 | 54 | | services.AddSingleton(typeof(IReadOnlyCollection<>), typeof(ReadOnlyListFactory<>)); |
| | 535 | 55 | | services.AddSingleton(_pluginFactory); |
| | | 56 | | |
| | 4126 | 57 | | foreach (var assembly in candidateAssemblies) |
| | | 58 | | { |
| | 1528 | 59 | | services.AddSingleton(assembly); |
| | | 60 | | } |
| | | 61 | | |
| | 535 | 62 | | _typeRegistrar.RegisterTypesFromAssemblies( |
| | 535 | 63 | | services, |
| | 535 | 64 | | _typeFilterer, |
| | 535 | 65 | | candidateAssemblies); |
| | | 66 | | |
| | 535 | 67 | | ServiceCollectionPluginOptions options = new( |
| | 535 | 68 | | services, |
| | 535 | 69 | | config, |
| | 535 | 70 | | candidateAssemblies, |
| | 535 | 71 | | _pluginFactory); |
| | | 72 | | |
| | 535 | 73 | | var executedPluginTypes = new HashSet<Type>(); |
| | 7500 | 74 | | foreach (var serviceCollectionPlugin in _pluginFactory.CreatePluginsFromAssemblies<IServiceCollectionPlugin>(can |
| | | 75 | | { |
| | 3215 | 76 | | var pluginType = serviceCollectionPlugin.GetType(); |
| | 3215 | 77 | | if (!executedPluginTypes.Add(pluginType)) |
| | | 78 | | { |
| | | 79 | | // Skip duplicate plugin instances of the same type |
| | | 80 | | continue; |
| | | 81 | | } |
| | | 82 | | |
| | 3215 | 83 | | serviceCollectionPlugin.Configure(options); |
| | | 84 | | } |
| | | 85 | | |
| | 535 | 86 | | return services; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private sealed class LazyFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConst |
| | 20 | 90 | | Lazy<T>(() => provider.GetRequiredService<T>()) |
| | | 91 | | where T : notnull |
| | | 92 | | { |
| | | 93 | | } |
| | | 94 | | |
| | 33 | 95 | | private sealed class ReadOnlyListFactory<T>(IServiceProvider provider) : IReadOnlyList<T> |
| | | 96 | | where T : notnull |
| | | 97 | | { |
| | 66 | 98 | | private readonly Lazy<IReadOnlyList<T>> _lazyItems = new(() => provider |
| | 66 | 99 | | .GetServices<T>() |
| | 66 | 100 | | .ToArray()); |
| | | 101 | | |
| | 0 | 102 | | public T this[int index] => _lazyItems.Value[index]; |
| | | 103 | | |
| | 0 | 104 | | public int Count => _lazyItems.Value.Count; |
| | | 105 | | |
| | 42 | 106 | | public IEnumerator<T> GetEnumerator() => _lazyItems.Value.GetEnumerator(); |
| | | 107 | | |
| | 4 | 108 | | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 109 | | } |
| | | 110 | | } |