Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for DI container #9

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Stack/Infrastructure/ServiceCollectionTypeRegistrar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli;

namespace Stack.Infrastructure;

public sealed class ServiceCollectionTypeRegistrar : ITypeRegistrar
{
private readonly IServiceCollection _builder;

public ServiceCollectionTypeRegistrar(IServiceCollection builder)
{
_builder = builder;
}

public ITypeResolver Build()
{
return new ServiceProviderTypeResolver(_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());
}
}
31 changes: 31 additions & 0 deletions src/Stack/Infrastructure/ServiceProviderTypeResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Spectre.Console.Cli;

namespace Stack.Infrastructure;

public sealed class ServiceProviderTypeResolver : ITypeResolver, IDisposable
{
private readonly IServiceProvider _provider;

public ServiceProviderTypeResolver(IServiceProvider provider)
{
_provider = provider ?? throw new ArgumentNullException(nameof(provider));
}

public object? Resolve(Type? type)
{
if (type == null)
{
return null;
}

return _provider.GetService(type);
}

public void Dispose()
{
if (_provider is IDisposable disposable)
{
disposable.Dispose();
}
}
}
7 changes: 6 additions & 1 deletion src/Stack/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@

using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli;
using Stack.Commands;
using Stack.Help;
using Stack.Infrastructure;

var app = new CommandApp();
var services = new ServiceCollection();
var registrar = new ServiceCollectionTypeRegistrar(services);

var app = new CommandApp(registrar);
app.Configure(configure =>
{
configure.SetApplicationName("stack");
Expand Down
1 change: 1 addition & 0 deletions src/Stack/Stack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Octopus.Shellfish" Version="0.2.1180" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
</ItemGroup>
Expand Down
Loading