PlumbR 1.0.0
See the version list below for details.
dotnet add package PlumbR --version 1.0.0
NuGet\Install-Package PlumbR -Version 1.0.0
<PackageReference Include="PlumbR" Version="1.0.0" />
paket add PlumbR --version 1.0.0
#r "nuget: PlumbR, 1.0.0"
// Install PlumbR as a Cake Addin #addin nuget:?package=PlumbR&version=1.0.0 // Install PlumbR as a Cake Tool #tool nuget:?package=PlumbR&version=1.0.0
PlumbR
PlumbR is a minimal api microservices framework that leverages MediatR to provide handlers for ASP.NET Core Minimal APIs, using FluentValidation to validate the MediatR requests, along with OneOf discriminated unions to allow the pipelines to return success values or problem details from the handlers.
Installation
Install the package via NuGet:
dotnet add package PlumbR
Configuration
To set up PlumbR, first configure MediatR and FluentValidation as usual, then
add cfg.AddValidationBehaviorForAssemblyContaining<Startup>()
to AddMediatR
to wire up the behavior that runs FluentValidation validators before the
pipeline handlers.
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblyContaining<Startup>();
cfg.AddValidationBehaviorForAssemblyContaining<Startup>();
});
services.AddValidatorsFromAssemblyContaining<Startup>();
To add other behaviors implementing IPipelineBehavior<TRequest, PipelineResult<TResponse>>
, you can use
AddPipelineBehaviorForAssemblyContaining<T>()
. Normally MediatR's
AddOpenBehavior
would be used here, but it has trouble wiring up open
behaviors when the result is another generic type like
PipelineRequest<TResponse>
.
services.AddMediatR(cfg =>
{
// ...
cfg.AddPipelineBehaviorForAssemblyContaining<Startup>(typeof(LoggerBehavior<>))
});
Usage
Handler, Request, and Result
Request and Handler classes use IPipelineRequest<TResult>
and
IPipelineHandler<TRequest, TResult>
interfaces to match up with the
validators. the response PipelineResult<TResult>
can be one of TResult
or
ProblemDetails
.
public class BodyRequest : IPipelineRequest<BodyResult>
{
public required int Id { get; init; }
public required string Name { get; set; }
}
public class BodyResult
{
public required string Message { get; init; }
}
public class BodyHandler : IPipelineHandler<BodyRequest, BodyResult>
{
public async Task<PipelineResult<BodyResult>> Handle(BodyRequest request, CancellationToken cancellationToken)
{
return new BodyResult
{
Message = $"Hello, {request.Name}! Your ID is {request.Id}."
};
}
}
Validation
Write validators as you normally would on the request types.
public class BodyRequestValidator : AbstractValidator<BodyRequest>
{
public BodyRequestValidator()
{
RuleFor(x => x.Id).GreaterThan(0);
}
}
Endpoints
- Pass
Pipeline.HandleBody<TRequest, TResult>
as the delegate to the endpoint mapping to bind the request using[FromBody]
. - Pass
Pipeline.HandleParameters<TRequest, TResult>
as the delegate to bind the request using[AsParameters]
. This will allow binding each property on the request model from different sources including[FromRoute]
,[FromQuery]
,[FromBody]
, etc.
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/parameters/{Id:int}", Pipeline.HandleParameters<ParameterRequest, ParametersResult>);
endpoints.MapPost("/body", Pipeline.HandleBody<BodyRequest, BodyResult>);
});
API Sample
See the TestApi project for a full sample API.
License
This project is licensed under the Apache License Version 2.0 - see the LICENSE file for details.
Product | Versions 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. |
-
net8.0
- FluentValidation (>= 11.9.0 && < 12.0.0)
- MediatR (>= 12.2.0 && < 13.0.0)
- OneOf (>= 3.0.263 && < 4.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.1 | 49 | 7/23/2024 |
1.0.0 | 581 | 4/22/2024 |
1.0.0-beta002 | 79 | 4/18/2024 |
1.0.0-beta001 | 74 | 4/7/2024 |