/Utils

Primary LanguageC#

Utils

Set of my helpers

DI utils

AddTypeAndImplementation

Register service both as TService and TImplementation with transient lifetime

IServiceCollection services = new();
services.AddTypeAndImplementation<IInterface, Service>();

NotEndingBackgroundService

Wrapper for BetterHostedServices.CriticalBackgroundService Allows you to forget about IApplicationEnder

class Service : NotEndingCriticalBackgroundService
{
    protected override Task Execute(CancellationToken token)
    {
        // Do stuff
    }
    
    protected override void OnError(Exception exception)
    {
        // Handle exception
    }
}

Logging utils

LogExceptionMessage

Allows you to fluent log exception before throw it. Default LogLevel is Critical

throw new ArgumentException().LogExceptionMessage(logger);

throw new ArgumentException().LogExceptionMessage(logger, LogLevel.Debug);

Options utils

AddOptions

Overload that take Action for more useful calls chaining

services
.AddOptions<CustomizableField.Configuration>(builder =>
    builder
        .BindConfiguration(CustomizableField.Configuration.SectionName)
        .Validate(CustomizableField.Configuration.Validate))
.AddOptions<AiPlayer.AiPlayerBehaviour>(builder =>
    builder
        .BindConfiguration(AiPlayer.AiPlayerBehaviour.SectionName));