| | | 1 | | using Microsoft.Extensions.Hosting; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.Injection; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.Hosting; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for configuring <see cref="ConfiguredSyringe"/> instances with generic host functionality. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <example> |
| | | 11 | | /// Source-gen first (recommended for AOT/trimming): |
| | | 12 | | /// <code> |
| | | 13 | | /// // With module initializer bootstrap (automatic): |
| | | 14 | | /// var host = new Syringe() |
| | | 15 | | /// .UsingSourceGen() |
| | | 16 | | /// .ForHost() |
| | | 17 | | /// .BuildHost(); |
| | | 18 | | /// |
| | | 19 | | /// await host.RunAsync(); |
| | | 20 | | /// </code> |
| | | 21 | | /// |
| | | 22 | | /// Reflection-based (for dynamic scenarios): |
| | | 23 | | /// <code> |
| | | 24 | | /// var host = new Syringe() |
| | | 25 | | /// .UsingReflection() |
| | | 26 | | /// .ForHost() |
| | | 27 | | /// .BuildHost(); |
| | | 28 | | /// |
| | | 29 | | /// await host.RunAsync(); |
| | | 30 | | /// </code> |
| | | 31 | | /// </example> |
| | | 32 | | public static class SyringeHostingExtensions |
| | | 33 | | { |
| | | 34 | | /// <summary> |
| | | 35 | | /// Transitions the configured syringe to host mode, enabling host-specific configuration. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="syringe">The configured syringe to transition.</param> |
| | | 38 | | /// <returns>A new host syringe instance.</returns> |
| | | 39 | | /// <example> |
| | | 40 | | /// <code> |
| | | 41 | | /// var hostSyringe = new Syringe() |
| | | 42 | | /// .UsingReflection() |
| | | 43 | | /// .ForHost(); // Transition to host mode |
| | | 44 | | /// |
| | | 45 | | /// // Now you can use host-specific methods |
| | | 46 | | /// var host = hostSyringe |
| | | 47 | | /// .UsingOptions(() => CreateHostOptions.Default.UsingArgs(args)) |
| | | 48 | | /// .BuildHost(); |
| | | 49 | | /// </code> |
| | | 50 | | /// </example> |
| | | 51 | | public static HostSyringe ForHost(this ConfiguredSyringe syringe) |
| | | 52 | | { |
| | 15 | 53 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 14 | 54 | | return new HostSyringe(syringe); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Builds a host with the configured settings using the default HostFactory. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="syringe">The configured syringe to build from.</param> |
| | | 61 | | /// <returns>The configured <see cref="IHost"/>.</returns> |
| | | 62 | | /// <example> |
| | | 63 | | /// <code> |
| | | 64 | | /// // Direct build without additional host configuration |
| | | 65 | | /// var host = new Syringe() |
| | | 66 | | /// .UsingReflection() |
| | | 67 | | /// .BuildHost(); |
| | | 68 | | /// |
| | | 69 | | /// await host.RunAsync(); |
| | | 70 | | /// </code> |
| | | 71 | | /// </example> |
| | | 72 | | public static IHost BuildHost(this ConfiguredSyringe syringe) |
| | | 73 | | { |
| | 1 | 74 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 0 | 75 | | return syringe.ForHost().BuildHost(); |
| | | 76 | | } |
| | | 77 | | } |