Carubbi.ConsoleApp
1.2.0
dotnet add package Carubbi.ConsoleApp --version 1.2.0
NuGet\Install-Package Carubbi.ConsoleApp -Version 1.2.0
<PackageReference Include="Carubbi.ConsoleApp" Version="1.2.0" />
paket add Carubbi.ConsoleApp --version 1.2.0
#r "nuget: Carubbi.ConsoleApp, 1.2.0"
// Install Carubbi.ConsoleApp as a Cake Addin #addin nuget:?package=Carubbi.ConsoleApp&version=1.2.0 // Install Carubbi.ConsoleApp as a Cake Tool #tool nuget:?package=Carubbi.ConsoleApp&version=1.2.0
Carubbi.ConsoleApp
Overview
Carubbi.ConsoleApp is a .NET library designed to facilitate the creation of console applications using a builder pattern similar to that used in WebApplication and HostApplications. The primary goal is to move the logic typically found in Program.cs into a class that can receive dependency injections through ServiceCollection, thereby enhancing the testability and maintainability of console applications.
Features
- Builder Pattern: Structured approach for configuring and building console applications.
- Dependency Injection: Supports dependency injection via ServiceCollection, allowing for loosely coupled components and easier testing.
- Enhanced Testability: Separation of concerns by moving application logic into manageable classes.
Getting Started
Installation
- Install the
Carubbi.ConsoleApp
NuGet package in your project:Install-Package Carubbi.ConsoleApp
- Install the
Usage
- Define your console application logic in a dedicated class, following a builder pattern similar to setting up a web or host application.
public class DemoConsoleApp(Arguments arguments, ILoggerFactory loggerFactory, IDateTimeWrapper dateTimeWrapper, IClientA clientA, IClientB clientB) : IConsoleApp { public async Task<ExitCodes> RunAsync() { // your logic goes here } }
Switch mapping
- You can map the arguments to the IConfiguration adding a dictionary to the ConsoleApplicationBuilder as shown in the next section. For easier consumption add properties to map the configuration to them. To acheive this, you can create an Arguments class as shown below:
public class Arguments(IConfiguration configuration) { public static IDictionary<string, string>? SwitchMap { get; internal set; } = new Dictionary<string, string>() { {"--v", nameof(Version)} }; public string Version { get; init; } = configuration![nameof(Version)]!; }
Dependency Injection
- Register your console application class and services in Startup.cs or equivalent. The
args
argument is the built-in one provided for console apps in .net
var builder = ConsoleApplication.CreateBuilder(args, Arguments.SwitchMap); builder.Services.AddConsoleApp<DemoConsoleApp>();
- Register your console application class and services in Startup.cs or equivalent. The
Additional Information
For more detailed instructions and updates, visit the This demo GitHub repository.
Contributions and issues can be reported on the repository.
License
This library is licensed under the MIT License. See the LICENSE file for more details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.CommandLine (>= 8.0.0)
- Microsoft.Extensions.Configuration.Json (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Carubbi.ConsoleApp:
Package | Downloads |
---|---|
Carubbi.Tests.Common
Base class to do unit tests with xUnit, Moq and FluentAssertions |
GitHub repositories
This package is not used by any popular GitHub repositories.