Carubbi.ConsoleApp 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Carubbi.ConsoleApp --version 1.1.0                
NuGet\Install-Package Carubbi.ConsoleApp -Version 1.1.0                
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="Carubbi.ConsoleApp" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Carubbi.ConsoleApp --version 1.1.0                
#r "nuget: Carubbi.ConsoleApp, 1.1.0"                
#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 Carubbi.ConsoleApp as a Cake Addin
#addin nuget:?package=Carubbi.ConsoleApp&version=1.1.0

// Install Carubbi.ConsoleApp as a Cake Tool
#tool nuget:?package=Carubbi.ConsoleApp&version=1.1.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

  1. Installation

    • Install the Carubbi.ConsoleApp NuGet package in your project:
      Install-Package Carubbi.ConsoleApp
      
  2. 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
         }
    }
    
  3. 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)]!;
     }
    
  4. 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>();
    

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
1.2.0 128 6/30/2024
1.1.0 121 6/30/2024
1.0.0 140 6/23/2024