|
1 | | -# kernel |
| 1 | +# feature[23] Shared Kernel |
2 | 2 |
|
3 | 3 | [](https://github.com/feature23/kernel/actions/workflows/ci_build.yml) |
| 4 | + |
| 5 | +A library of reusable types for implementing Clean Architecture in .NET and ASP.NET Core applications, based heavily on the work of [Steve Smith](https://github.com/ardalis) in the following open-sourcee projects: |
| 6 | +- [ASP.NET Core Template](https://github.com/ardalis/CleanArchitecture) |
| 7 | +- [Shared Kernel](https://github.com/ardalis/Ardalis.SharedKernel) |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +> **WARNING:** This library is currently in a pre-release state, and breaking changes may occur before reaching version 1.0. |
| 12 | +
|
| 13 | +## NuGet Installation |
| 14 | +### Core Package |
| 15 | +```powershell |
| 16 | +dotnet add package F23.Kernel |
| 17 | +``` |
| 18 | + |
| 19 | +### ASP.NET Core Helper Package |
| 20 | +```powershell |
| 21 | +dotnet add package F23.Kernel.AspNetCore |
| 22 | +``` |
| 23 | + |
| 24 | +## Examples |
| 25 | + |
| 26 | +### Query Handler |
| 27 | +```csharp |
| 28 | +class GetWeatherForecastQueryResult |
| 29 | +{ |
| 30 | + public required IReadOnlyList<WeatherForecast> Forecast { get; init; } |
| 31 | +} |
| 32 | + |
| 33 | +class GetWeatherForecastQuery : IQuery<GetWeatherForecastQueryResult> |
| 34 | +{ |
| 35 | + public int DaysIntoTheFuture { get; init; } = 5; |
| 36 | +} |
| 37 | + |
| 38 | +record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) |
| 39 | +{ |
| 40 | + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); |
| 41 | +} |
| 42 | + |
| 43 | +class GetWeatherForecastQueryHandler(IValidator<GetWeatherForecastQuery> validator, IWeatherForecastRepository repository) |
| 44 | + : IQueryHandler<GetWeatherForecastQuery, GetWeatherForecastQueryResult> |
| 45 | +{ |
| 46 | + public async Task<Result<GetWeatherForecastQueryResult>> Handle(GetWeatherForecastQuery query, CancellationToken cancellationToken = default) |
| 47 | + { |
| 48 | + if (await validator.Validate(query, cancellationToken) is ValidationFailedResult failed) |
| 49 | + { |
| 50 | + return Result<GetWeatherForecastQueryResult>.ValidationFailed(failed.Errors); |
| 51 | + } |
| 52 | + |
| 53 | + var forecast = await repository.GetForecast(query.DaysIntoTheFuture, cancellationToken); |
| 54 | + |
| 55 | + var result = new GetWeatherForecastQueryResult |
| 56 | + { |
| 57 | + Forecast = forecast |
| 58 | + }; |
| 59 | + |
| 60 | + return Result<GetWeatherForecastQueryResult>.Success(result); |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +#### Program.cs |
| 66 | +```csharp |
| 67 | +builder.Services.RegisterQueryHandler<GetWeatherForecastQuery, GetWeatherForecastQueryResult, GetWeatherForecastQueryHandler>(); |
| 68 | + |
| 69 | +// Other code omitted for brevity |
| 70 | +
|
| 71 | +app.MapGet("/weatherforecast", async (IQueryHandler<GetWeatherForecastQuery, GetWeatherForecastQueryResult> queryHandler) => |
| 72 | + { |
| 73 | + var result = await queryHandler.Handle(new GetWeatherForecastQuery()); |
| 74 | + |
| 75 | + return result.ToMinimalApiResult(); |
| 76 | + }) |
| 77 | + .WithName("GetWeatherForecast") |
| 78 | + .WithOpenApi(); |
| 79 | +``` |
| 80 | + |
| 81 | +### Command Handler |
| 82 | +> TODO |
| 83 | +
|
| 84 | +### Event Sourcing |
| 85 | +> TODO |
0 commit comments