mirror of
https://github.com/fergalmoran/snapp-cli.git
synced 2025-12-22 10:00:52 +00:00
32 lines
843 B
C#
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());
|
|
}
|
|
} |