| | | 1 | | using Microsoft.Maui.Hosting; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.Injection; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.Maui; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides a fluent API for populating a .NET MAUI application's service collection with |
| | | 9 | | /// Needlr-discovered services. Wraps a <see cref="ConfiguredSyringe"/> with MAUI-specific behavior. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <para> |
| | | 13 | | /// A MAUI head project owns a single dependency-injection container created by |
| | | 14 | | /// <c>MauiApp.CreateBuilder()</c>. <see cref="MauiSyringe"/> applies every Needlr-discovered |
| | | 15 | | /// registration to that container's <see cref="MauiAppBuilder.Services"/>, so MAUI resolves your |
| | | 16 | | /// <c>App</c>, pages, and view models — and every Needlr service — from one provider with no |
| | | 17 | | /// per-type manual registration. |
| | | 18 | | /// </para> |
| | | 19 | | /// <para> |
| | | 20 | | /// Obtain a <see cref="MauiSyringe"/> by calling <c>ForMaui()</c> on a configured syringe. |
| | | 21 | | /// </para> |
| | | 22 | | /// </remarks> |
| | | 23 | | /// <example> |
| | | 24 | | /// <code> |
| | | 25 | | /// var builder = MauiApp.CreateBuilder(); |
| | | 26 | | /// builder.UseMauiApp<App>(); |
| | | 27 | | /// |
| | | 28 | | /// new Syringe() |
| | | 29 | | /// .UsingSourceGen() |
| | | 30 | | /// .ForMaui() |
| | | 31 | | /// .PopulateInto(builder); |
| | | 32 | | /// |
| | | 33 | | /// return builder.Build(); |
| | | 34 | | /// </code> |
| | | 35 | | /// </example> |
| | | 36 | | public sealed record MauiSyringe |
| | | 37 | | { |
| | 27 | 38 | | internal ConfiguredSyringe BaseSyringe { get; init; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Initializes a new instance of the <see cref="MauiSyringe"/> class. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="baseSyringe">The configured syringe to wrap.</param> |
| | | 44 | | /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="baseSyringe"/> is <see langword="null |
| | 3 | 45 | | public MauiSyringe(ConfiguredSyringe baseSyringe) |
| | | 46 | | { |
| | 3 | 47 | | System.ArgumentNullException.ThrowIfNull(baseSyringe); |
| | 3 | 48 | | BaseSyringe = baseSyringe; |
| | 3 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Applies the Needlr-discovered registrations to the supplied <see cref="MauiAppBuilder"/>'s |
| | | 53 | | /// service collection, using <see cref="MauiAppBuilder.Configuration"/> for options binding. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <remarks> |
| | | 56 | | /// <para> |
| | | 57 | | /// The builder's <see cref="MauiAppBuilder.Configuration"/> is used to bind any |
| | | 58 | | /// <c>[Options]</c>- and <c>[HttpClientOptions]</c>-decorated types, matching the behavior of |
| | | 59 | | /// the ASP.NET and host integrations. Services already present on the builder (for example MAUI's |
| | | 60 | | /// own registrations, or your own <c>builder.Services.Add...</c> calls) are preserved. |
| | | 61 | | /// </para> |
| | | 62 | | /// </remarks> |
| | | 63 | | /// <param name="builder">The MAUI application builder to populate.</param> |
| | | 64 | | /// <returns>The same <paramref name="builder"/> instance, to allow chaining.</returns> |
| | | 65 | | /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="builder"/> is <see langword="null"/>. |
| | | 66 | | public MauiAppBuilder PopulateInto(MauiAppBuilder builder) |
| | | 67 | | { |
| | 3 | 68 | | System.ArgumentNullException.ThrowIfNull(builder); |
| | | 69 | | |
| | 3 | 70 | | var typeRegistrar = BaseSyringe.GetOrCreateTypeRegistrar(); |
| | 3 | 71 | | var typeFilterer = BaseSyringe.GetOrCreateTypeFilterer(); |
| | 3 | 72 | | var pluginFactory = BaseSyringe.GetOrCreatePluginFactory(); |
| | 3 | 73 | | var serviceCollectionPopulator = BaseSyringe.GetOrCreateServiceCollectionPopulator(typeRegistrar, typeFilterer, |
| | 3 | 74 | | var assemblyProvider = BaseSyringe.GetOrCreateAssemblyProvider(); |
| | 3 | 75 | | var additionalAssemblies = BaseSyringe.GetAdditionalAssemblies(); |
| | | 76 | | |
| | 3 | 77 | | var serviceProviderBuilder = BaseSyringe.GetOrCreateServiceProviderBuilder( |
| | 3 | 78 | | serviceCollectionPopulator, |
| | 3 | 79 | | assemblyProvider, |
| | 3 | 80 | | additionalAssemblies); |
| | | 81 | | |
| | 3 | 82 | | var candidateAssemblies = serviceProviderBuilder.GetCandidateAssemblies(); |
| | | 83 | | |
| | 3 | 84 | | serviceCollectionPopulator.RegisterToServiceCollection( |
| | 3 | 85 | | builder.Services, |
| | 3 | 86 | | builder.Configuration, |
| | 3 | 87 | | candidateAssemblies); |
| | | 88 | | |
| | | 89 | | // User-registered post-plugin callbacks run first, then the source-generated |
| | | 90 | | // options/extension registrars — the same ordering ConfiguredSyringe and the ASP.NET |
| | | 91 | | // integration use. Without these, [Options]-decorated types bound by the generator would |
| | | 92 | | // never have their IConfigureOptions<T> registered. |
| | 6 | 93 | | foreach (var callback in BaseSyringe.GetPostPluginRegistrationCallbacks()) |
| | | 94 | | { |
| | 0 | 95 | | callback(builder.Services); |
| | | 96 | | } |
| | | 97 | | |
| | 3 | 98 | | if (SourceGenRegistry.TryGetOptionsRegistrar(out var optionsRegistrar) && optionsRegistrar is not null) |
| | | 99 | | { |
| | 0 | 100 | | optionsRegistrar(builder.Services, builder.Configuration); |
| | | 101 | | } |
| | | 102 | | |
| | 3 | 103 | | if (SourceGenRegistry.TryGetExtensionRegistrar(out var extensionRegistrar) && extensionRegistrar is not null) |
| | | 104 | | { |
| | 0 | 105 | | extensionRegistrar(builder.Services, builder.Configuration); |
| | | 106 | | } |
| | | 107 | | |
| | 3 | 108 | | return builder; |
| | | 109 | | } |
| | | 110 | | } |