Skip to content

Dependency Injection

Matthias Beerens edited this page Dec 28, 2018 · 5 revisions

Use a DI/IoC container

To use a custom DI/IoC container you will need to create a custom resolver. This resolver will pass the work on to the underlying DI/IoC container.

using MatthiWare.CommandLine.Abstractions;

public class CustomResolver : IContainerResolver
{
   private readonly IContainer _container;

    public CustomResolver(IContainer container)
    {
        _container = container;
    }

    public T Resolve<T>()
    {
        return _container.Resolve<T>();
    }
}

Now you can use it as following

private readonly IContainer _container;

public static void Main(string[] args)
{
    var parser = new CommandLineParser<OptionModel>(new CustomResolver(_container));

    var result = parser.Parse(args);
}
Clone this wiki locally