Tyrrrz / CliFx

Class-first framework for building command-line interfaces
MIT License
1.5k stars 61 forks source link

Add IConsole to DI #73

Closed adambajguz closed 4 years ago

adambajguz commented 4 years ago

Is it possible to inject IConsole instance to some service using Microsoft.Extensions.DependencyInjection?

Tyrrrz commented 4 years ago

Yes, you would do it like this:

var services = new ServiceCollection();

services.AddSingleton<IConsole>(new SystemConsole()); // or `new VirtualConsole();`, depending on what you need

var serviceProvider = services.BuildServiceProvider();

// ...

var console = serviceProvider.GetService<IConsole>();

var cli = new CliApplicationBuilder()
            .AddCommandsFromThisAssembly()
            .UseConsole(console) // make sure CliFx is using the same instance of IConsole
            .Build()
adambajguz commented 4 years ago

Thanks, I think this can be added to the README.