| | | 1 | | using Microsoft.Maui.Hosting; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.Injection; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.Maui; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods that integrate Needlr's discovery into a user-controlled |
| | | 9 | | /// <see cref="MauiAppBuilder"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <para> |
| | | 13 | | /// <c>UseNeedlr</c> is a "reverse integration": Needlr adapts to the MAUI builder you already own |
| | | 14 | | /// rather than taking over its creation. Call it from <c>MauiProgram.CreateMauiApp</c> after |
| | | 15 | | /// <c>UseMauiApp<App>()</c>; every Needlr-discovered service is added to |
| | | 16 | | /// <see cref="MauiAppBuilder.Services"/>, and MAUI builds and owns the single resulting provider. |
| | | 17 | | /// </para> |
| | | 18 | | /// </remarks> |
| | | 19 | | /// <example> |
| | | 20 | | /// <code> |
| | | 21 | | /// public static MauiApp CreateMauiApp() |
| | | 22 | | /// { |
| | | 23 | | /// var builder = MauiApp.CreateBuilder(); |
| | | 24 | | /// builder |
| | | 25 | | /// .UseMauiApp<App>() |
| | | 26 | | /// .UseNeedlr(syringe => syringe.UsingSourceGen()); |
| | | 27 | | /// |
| | | 28 | | /// return builder.Build(); |
| | | 29 | | /// } |
| | | 30 | | /// </code> |
| | | 31 | | /// </example> |
| | | 32 | | public static class MauiAppBuilderNeedlrExtensions |
| | | 33 | | { |
| | | 34 | | /// <summary> |
| | | 35 | | /// Populates the builder's service collection with the registrations discovered by the |
| | | 36 | | /// supplied configured syringe. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="builder">The MAUI application builder to populate.</param> |
| | | 39 | | /// <param name="syringe">A configured syringe (for example <c>new Syringe().UsingSourceGen()</c>).</param> |
| | | 40 | | /// <returns>The same <paramref name="builder"/> instance, to allow chaining.</returns> |
| | | 41 | | /// <exception cref="System.ArgumentNullException">Thrown when any argument is <see langword="null"/>.</exception> |
| | | 42 | | public static MauiAppBuilder UseNeedlr(this MauiAppBuilder builder, ConfiguredSyringe syringe) |
| | | 43 | | { |
| | 1 | 44 | | System.ArgumentNullException.ThrowIfNull(builder); |
| | 1 | 45 | | System.ArgumentNullException.ThrowIfNull(syringe); |
| | 1 | 46 | | return syringe.ForMaui().PopulateInto(builder); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Populates the builder's service collection using a syringe configured by |
| | | 51 | | /// <paramref name="configure"/>. Use this overload to pick the discovery strategy inline, for |
| | | 52 | | /// example <c>builder.UseNeedlr(s => s.UsingSourceGen())</c>. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="builder">The MAUI application builder to populate.</param> |
| | | 55 | | /// <param name="configure"> |
| | | 56 | | /// A callback that turns a fresh <see cref="Syringe"/> into a <see cref="ConfiguredSyringe"/>, |
| | | 57 | | /// typically by calling <c>UsingSourceGen()</c> or <c>UsingReflection()</c>. |
| | | 58 | | /// </param> |
| | | 59 | | /// <returns>The same <paramref name="builder"/> instance, to allow chaining.</returns> |
| | | 60 | | /// <exception cref="System.ArgumentNullException">Thrown when any argument is <see langword="null"/>.</exception> |
| | | 61 | | public static MauiAppBuilder UseNeedlr(this MauiAppBuilder builder, System.Func<Syringe, ConfiguredSyringe> configur |
| | | 62 | | { |
| | 1 | 63 | | System.ArgumentNullException.ThrowIfNull(builder); |
| | 1 | 64 | | System.ArgumentNullException.ThrowIfNull(configure); |
| | 1 | 65 | | return builder.UseNeedlr(configure(new Syringe())); |
| | | 66 | | } |
| | | 67 | | } |