TGC.ConsoleBuilder 1.1.3

dotnet add package TGC.ConsoleBuilder --version 1.1.3                
NuGet\Install-Package TGC.ConsoleBuilder -Version 1.1.3                
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="TGC.ConsoleBuilder" Version="1.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TGC.ConsoleBuilder --version 1.1.3                
#r "nuget: TGC.ConsoleBuilder, 1.1.3"                
#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.
// Install TGC.ConsoleBuilder as a Cake Addin
#addin nuget:?package=TGC.ConsoleBuilder&version=1.1.3

// Install TGC.ConsoleBuilder as a Cake Tool
#tool nuget:?package=TGC.ConsoleBuilder&version=1.1.3                

TGC.ConsoleBuilder

Using the ConsoleBuilder

There are two ways of using the ConsoleBuilder:

  1. You run it from a manually defined injected service
  2. You run it via Run()/RunAsync();

Running via injected service

var consoleAppBuilder = ConsoleBuilder.CreateApp(args);
var consoleApp = consoleAppBuilder.Build();

consoleApp.RunFromServiceProvider(s => s.GetRequiredService<ITestService>().DoStuff());

If you run it via an injected service, this service needs to be injected. This is done via implementing the interface IConsoleInjectionBuilder. It is in this implementation you specify your dependencies. It could look like the following:

internal class ConsoleInjectionBuilder : IConsoleInjectionBuilder
{
	public void Configure(IServiceCollection serviceCollection)
	{
		serviceCollection.AddScoped<ITestService, TestService>();
	}
}

Running via Run()/RunAsync()

var consoleAppBuilder = ConsoleBuilder.CreateApp(args);
var consoleApp = consoleAppBuilder.Build();

await consoleApp.RunAsync();
// OR
await consoleApp.Run();

To be able to use Run or RunAsync you need to have implemented the interface IAsyncStartService or ISyncStartService. They are both executed via dependency injection so you are able to access your dependencies registered in IConsoleInjectionBuilder directly from your implementation.

Example of setup:

IConsoleInjectionBuilder

internal class ConsoleInjectionBuilder : IConsoleInjectionBuilder
{
	public void Configure(IServiceCollection serviceCollection)
	{
		serviceCollection.AddScoped<ITestService, TestService>();
	}
}

IAsyncStartService

internal class AsyncRunner : IAsyncStartService
{
	private readonly ITestService _testService;

	public AsyncRunner(ITestService testService)
	{
		_testService = testService;
	}

	public Task RunAsync()
	{
		_testService.ResolveImportantStuff();
		_testService.StartBatchHandling();
		//etc...
	}
}

Roadmap

Implement Host Builder

At the moment, things has been manually wired. This is not ideal. Instead, it should wrap IHost.

Changelog

1.1.3

Added source code reference.

1.1.2

Added project url and updated README.md

0.1.0

Initial version - Not tested and ready for usage.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
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.1.3 199 10/31/2023
1.1.2 125 10/31/2023
1.1.1 164 7/16/2023