Skip to content

feature23/kernel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

feature[23] Shared Kernel

build

A library of reusable types for implementing Clean Architecture in .NET and ASP.NET Core applications, based heavily on the work of Steve Smith in the following open-sourcee projects:

For the core functionality, only the F23.Kernel library is needed. This library provides types for events, results, query and command handlers, validation, and messaging. For smoother integration with ASP.NET Core, the F23.Kernel.AspNetCore library can be used for easily mapping between core result types and ASP.NET Core IActionResult and model state.

WARNING: This library is currently in a pre-release state, and breaking changes may occur before reaching version 1.0.

NuGet Installation

Core Package

dotnet add package F23.Kernel

ASP.NET Core Helper Package

dotnet add package F23.Kernel.AspNetCore

Examples

Query Handler

class GetWeatherForecastQueryResult
{
    public required IReadOnlyList<WeatherForecast> Forecast { get; init; }
}

class GetWeatherForecastQuery : IQuery<GetWeatherForecastQueryResult>
{
    public int DaysIntoTheFuture { get; init; } = 5;
}

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

class GetWeatherForecastQueryHandler(IValidator<GetWeatherForecastQuery> validator, IWeatherForecastRepository repository)
    : IQueryHandler<GetWeatherForecastQuery, GetWeatherForecastQueryResult>
{
    public async Task<Result<GetWeatherForecastQueryResult>> Handle(GetWeatherForecastQuery query, CancellationToken cancellationToken = default)
    {
        if (await validator.Validate(query, cancellationToken) is ValidationFailedResult failed)
        {
            return Result<GetWeatherForecastQueryResult>.ValidationFailed(failed.Errors);
        }

        var forecast = await repository.GetForecast(query.DaysIntoTheFuture, cancellationToken);

        var result = new GetWeatherForecastQueryResult
        {
            Forecast = forecast
        };

        return Result<GetWeatherForecastQueryResult>.Success(result);
    }
}

Program.cs

builder.Services.RegisterQueryHandler<GetWeatherForecastQuery, GetWeatherForecastQueryResult, GetWeatherForecastQueryHandler>();

// Other code omitted for brevity

app.MapGet("/weatherforecast", async (IQueryHandler<GetWeatherForecastQuery, GetWeatherForecastQueryResult> queryHandler) =>
    {
        var result = await queryHandler.Handle(new GetWeatherForecastQuery());

        return result.ToMinimalApiResult();
    })
    .WithName("GetWeatherForecast")
    .WithOpenApi();

Command Handler

TODO

Event Sourcing

TODO

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages