| | | 1 | | namespace ZhEaIsNsAaBn.DbSeeder.Design; |
| | | 2 | | |
| | | 3 | | /// Spectre's command DI over Microsoft.Extensions.DependencyInjection, so commands get the <see cref="DesignSession"/>. |
| | | 4 | | internal sealed class DesignTypeRegistrar : ITypeRegistrar |
| | | 5 | | { |
| | 411 | 6 | | private readonly ServiceCollection _services = new(); |
| | | 7 | | |
| | | 8 | | |
| | 822 | 9 | | public DesignTypeRegistrar( DesignSession session ) => _services.AddSingleton( session ); |
| | | 10 | | |
| | | 11 | | |
| | 411 | 12 | | public ITypeResolver Build() => new Resolver( _services.BuildServiceProvider() ); |
| | | 13 | | |
| | 13563 | 14 | | public void Register( Type service, Type implementation ) => _services.AddSingleton( service, implementation ); |
| | | 15 | | |
| | 1644 | 16 | | public void RegisterInstance( Type service, object implementation ) => _services.AddSingleton( service, implementati |
| | | 17 | | |
| | 411 | 18 | | public void RegisterLazy( Type service, Func<object> factory ) => _services.AddSingleton( service, _ => factory() ); |
| | | 19 | | |
| | | 20 | | |
| | | 21 | | private sealed class Resolver : ITypeResolver, IDisposable |
| | | 22 | | { |
| | | 23 | | private readonly ServiceProvider _provider; |
| | | 24 | | |
| | | 25 | | |
| | 822 | 26 | | public Resolver( ServiceProvider provider ) => _provider = provider; |
| | | 27 | | |
| | | 28 | | |
| | 1644 | 29 | | public object? Resolve( Type? type ) => type is null ? null : _provider.GetService( type ); |
| | | 30 | | |
| | 411 | 31 | | public void Dispose() => _provider.Dispose(); |
| | | 32 | | } |
| | | 33 | | } |