Skip to content

Dependency Injection with LightIoC Container

Tarik Guney edited this page Jul 12, 2020 · 1 revision

CommandCore comes with its in-house IoC Container which is simple to use. Service can be registered through ConfigureServices method as shown below:

public static int Main(string[] args)
{
    var commandCoreApp = new CommandCoreApp();
    commandCoreApp.ConfigureServices(sp =>
    {
        sp.Register<IOutputWriter, OutputWriter>();
    });
    return commandCoreApp.Parse(args);
}

The IOutputWriter service can be later injected into the verb classes like below:

[VerbName("add", Description = "Adds a new person to the system.")]
public class Add : VerbBase<AddOptions>
{
    private readonly IOutputWriter _outputWriter;
    public Add(IOutputWriter outputWriter)
    {
        _outputWriter = outputWriter;
    }
    
    public override VerbViewBase Run()
    {
        return new AddView(Options, _outputWriter);
    }
}

LightIoC offers various methods to register and resolve types for different use cases.

Clone this wiki locally