/console-application-service-library

Class library meant to ease configuration and registration of services, database contexts and option files in .NET 5.0 projects.

Primary LanguageC#

ConsoleApplicationServiceLibrary

Publish Packages package

This class library is meant to ease configuration and registration of services, database contexts and option files in .NET 5.0 projects.

Installation and configuration

Install package from nuget gallery using either dotnet CLI

dotnet add package ServiceLibrary.ConsoleApp

or the package manager

Install-Package ServiceLibrary.ConsoleApp

All the services will be registered the first time one service is invoked. Since there's no native support for Dependency Injection in Console Application, I've provided the BaseController class which contains a ServiceProvider property. The ServiceProvider returns a singleton of a ServiceProvider configured by the ServiceProviderConfigurator.

public class MyController : BaseController
{
    private readonly IOtherService _service;

    public MyController(IOtherService service = null)
    {
        _service = service ?? ServiceProvider.GetService<IOtherService>();
    }
}

Same goes for Services - inheriting from BaseService will make an instance of ServiceProvider available.

public class MyService : BaseService, IMyService
{
    private readonly IOtherService _service;

    public MyService(IOtherService service = null)
    {
        _service = service ?? ServiceProvider.GetService<IOtherService>();
    }
}

Configuring services

Services registered with DI are usually interface's implementations. Starting froma a generic interface IService, the implementation would be

public interface Service : IService

To have this automatic registered by this class library, we just add one attribute

[Service(typeof(IService), ServiceLifetime.Scoped)]
public interface Service : IService

This is the equivalent of adding

services.AddScoped<IService, Service>();

in the startup.cs. By changing the values of the second parameter we can select the lifetime of the service (scoped, singleton, ...). If the Service does not implement any interface, we can simply leave first attribute null.

Configuring databases

Databases can be easily configured adding the attribute

[Database("ConnectionString")]
public class MyDbContext : DbContext

This is the equivalent of adding

services.AddDbContext<MyDbContext>(options => options.UseSqlite("Data Source=<connection_string>"));

in the startup.cs.

The string parameter is the name of the json attribute in the appsettings.json file which holds the value for the connection string.

Configuring options

Options can be easily configured adding the attribute

[Option("JsonSections")]
public class MySettings

This is the equivalent of adding

// Add functionality to inject IOptions<T>
services.AddOptions();

// Add our Config object so it can be injected
services.Configure<MySettings>(Configuration.GetSection("JsonSections"));

in the startup.cs.

The string parameter is the name of the json section in the appsettings.json file which we want to map to our model. Every attribute in the json object needs to have a correspondent property in the model.