Files
snapp-cli/Helpers/TypeRegistrar.cs
Fergal Moran 2daa52aa04 Initial commit
2024-07-26 17:01:40 +01:00

32 lines
843 B
C#

using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli;
namespace Snapp.Cli.Helpers;
public sealed class TypeRegistrar : ITypeRegistrar {
private readonly IServiceCollection _builder;
public TypeRegistrar(IServiceCollection builder) {
_builder = builder;
}
public ITypeResolver Build() {
return new TypeResolver(_builder.BuildServiceProvider());
}
public void Register(Type service, Type implementation) {
_builder.AddSingleton(service, implementation);
}
public void RegisterInstance(Type service, object implementation) {
_builder.AddSingleton(service, implementation);
}
public void RegisterLazy(Type service, Func<object> func) {
if (func is null) {
throw new ArgumentNullException(nameof(func));
}
_builder.AddSingleton(service, (provider) => func());
}
}