| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Spectre.Console.Cli; |
| | | 3 | | |
| | | 4 | | namespace Orchestrator.Infrastructure; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Bridges Microsoft.Extensions.DependencyInjection with Spectre.Console.Cli's <see cref="ITypeRegistrar"/>. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Based on the canonical implementation from spectreconsole/examples: |
| | | 11 | | /// <see href="https://github.com/spectreconsole/examples/blob/main/examples/Cli/Logging/Infrastructure/TypeRegistrar.cs |
| | | 12 | | /// </remarks> |
| | | 13 | | public sealed class TypeRegistrar : ITypeRegistrar |
| | | 14 | | { |
| | | 15 | | private readonly IServiceCollection _builder; |
| | | 16 | | |
| | 1 | 17 | | public TypeRegistrar(IServiceCollection builder) |
| | | 18 | | { |
| | 1 | 19 | | _builder = builder; |
| | 1 | 20 | | } |
| | | 21 | | |
| | | 22 | | public ITypeResolver Build() |
| | | 23 | | { |
| | 1 | 24 | | return new TypeResolver(_builder.BuildServiceProvider()); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public void Register(Type service, Type implementation) |
| | | 28 | | { |
| | 1 | 29 | | _builder.AddSingleton(service, implementation); |
| | 1 | 30 | | } |
| | | 31 | | |
| | | 32 | | public void RegisterInstance(Type service, object implementation) |
| | | 33 | | { |
| | 1 | 34 | | _builder.AddSingleton(service, implementation); |
| | 1 | 35 | | } |
| | | 36 | | |
| | | 37 | | public void RegisterLazy(Type service, Func<object> func) |
| | | 38 | | { |
| | 1 | 39 | | ArgumentNullException.ThrowIfNull(func); |
| | 1 | 40 | | _builder.AddSingleton(service, _ => func()); |
| | 1 | 41 | | } |
| | | 42 | | } |