Giter Site home page Giter Site logo

plumbr's Introduction

PlumbR

PlumbR is a minimal 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}."
        };
    }
}

Validator

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.

plumbr's People

Contributors

jesse-black avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.