Kiwify.Kiwi.CLI.DependencyInjection 1.0.0

Prefix Reserved
dotnet add package Kiwify.Kiwi.CLI.DependencyInjection --version 1.0.0
                    
NuGet\Install-Package Kiwify.Kiwi.CLI.DependencyInjection -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Kiwify.Kiwi.CLI.DependencyInjection" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Kiwify.Kiwi.CLI.DependencyInjection" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Kiwify.Kiwi.CLI.DependencyInjection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Kiwify.Kiwi.CLI.DependencyInjection --version 1.0.0
                    
#r "nuget: Kiwify.Kiwi.CLI.DependencyInjection, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Kiwify.Kiwi.CLI.DependencyInjection@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Kiwify.Kiwi.CLI.DependencyInjection&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Kiwify.Kiwi.CLI.DependencyInjection&version=1.0.0
                    
Install as a Cake Tool

Kiwify.Kiwi.CLI.DependencyInjection

ServiceProviderCommandResolver - wires a standard IServiceProvider into Kiwi CLI so that command objects are constructed via dependency injection.

Target: netstandard2.1 Dependencies: Kiwify.Kiwi.CLI, Microsoft.Extensions.DependencyInjection.Abstractions


What it provides

Type Purpose
ServiceProviderCommandResolver ICommandResolver implementation that resolves command types from an IServiceProvider

The core Kiwify.Kiwi.CLI library defines only ICommandResolver - a single-method interface with no external dependencies. This project provides the concrete implementation that bridges ICommandResolver to Microsoft's standard DI container.


Usage

1. Register services and command types

using Microsoft.Extensions.DependencyInjection;
using Kiwify.Kiwi.CLI.DependencyInjection;

var services = new ServiceCollection();

// Application services
services.AddSingleton<IEmailService, SmtpEmailService>();
services.AddSingleton<IUserRepository, SqlUserRepository>();

// Command types - registered so the resolver can construct them
services.AddTransient<SendOptions>();
services.AddTransient<UserInfoOptions>();

var provider = services.BuildServiceProvider();

2. Attach the resolver

using Kiwify.Kiwi.CLI;

var app = new CommandLineApplication("mytool", "1.0.0", "My CLI tool.");

app.AddCommand(sendCommand);
app.LoadCommandFrom<UserInfoOptions>();

// Attach before Execute - propagates to all registered commands
app.AttachCommandResolver(new ServiceProviderCommandResolver(provider));

app.Execute(args);

3. Accept injected services in the command object

public class SendOptions
{
    private readonly IEmailService? _email;

    public SendOptions() { }  // fallback when no resolver is attached

    public SendOptions(IEmailService email)
    {
        _email = email;
    }

    public string To      { get; set; } = "";
    public string Subject { get; set; } = "";

    // Fluent handler - opts is DI-constructed, so _email is populated
    public void Execute(IOutput output)
    {
        _email?.Send(To, Subject);
        output.WriteResult("Sent.");
    }
}

For declarative commands, the [CliHandler] method is public static. The injected value arrives via the first parameter:

[CliCommand("user-info")]
public class UserInfoOptions
{
    private readonly IUserRepository? _users;

    public UserInfoOptions() { }
    public UserInfoOptions(IUserRepository users) { _users = users; }

    [CliOption("--id", "-i", IsRequired = true)]
    public int UserId { get; set; }

    [CliHandler]
    public static void Handle(UserInfoOptions opts, IOutput output)
    {
        var user = opts._users?.GetById(opts.UserId);
        // ...
    }
}

Container-agnostic design

The framework never calls IServiceCollection, BuildServiceProvider, or any container-specific API. It calls only resolver.Resolve(Type commandType), which returns object?.

To use a different container (Autofac, Lamar, Simple Injector, etc.), implement ICommandResolver directly:

public class AutofacCommandResolver : ICommandResolver
{
    private readonly IContainer _container;
    public AutofacCommandResolver(IContainer container) => _container = container;
    public object? Resolve(Type commandType) => _container.ResolveOptional(commandType);
}

Fallback behaviour

If the resolver returns null (the type is not registered), the framework falls back to Activator.CreateInstance<TCommand>(). If no parameterless constructor exists, an InvalidOperationException is thrown with a clear message indicating the type must be registered with the resolver.


See also

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 98 5/28/2026