DotEmilu.Abstractions 2.0.0

dotnet add package DotEmilu.Abstractions --version 2.0.0
                    
NuGet\Install-Package DotEmilu.Abstractions -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DotEmilu.Abstractions" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotEmilu.Abstractions" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DotEmilu.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DotEmilu.Abstractions --version 2.0.0
                    
#r "nuget: DotEmilu.Abstractions, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=DotEmilu.Abstractions&version=2.0.0
                    
Install DotEmilu.Abstractions as a Cake Addin
#tool nuget:?package=DotEmilu.Abstractions&version=2.0.0
                    
Install DotEmilu.Abstractions as a Cake Tool

DotEmilu

A simple, easy-to-use .NET library designed for handling HTTP requests and responses.

Features

  • Supports GET, POST, PUT, and DELETE requests.
  • Handles HTTP responses with status codes and error handling.
  • Built with .NET 8 for cross-platform compatibility (Windows, Linux, macOS).
  • Supports async/await for non-blocking HTTP calls.
  • Includes request contracts and base constraints for verifiers.
  • Provides an HttpHandler to simplify request handling.
  • New extension method to register handlers using reflection for cleaner setup.

How to Use

Follow these simple steps to get started:

  1. Build Your Logic
    Create the logic for your application based on your previously defined request and expected response.

    public class FullUseCase(
        IVerifier<FullDto> verifier,
        IVerifier<InDto> verifierIn,
        IHandler<InDto> handlerIn)
        : Handler<FullDto, FullOutDto>(verifier)
    {
        private readonly IVerifier<FullDto> _verifier = verifier;
    
        protected override async Task<FullOutDto?> HandleUseCaseAsync(FullDto request, CancellationToken cancellationToken)
        {
            Console.WriteLine("Handling my primary use case...");
    
            var requestIn = new InDto(request.Day);
    
            await WorksSecondCaseAsync(requestIn, cancellationToken);
    
            if (!verifierIn.IsValid)
            {
                _verifier.AddErrors(verifierIn.Errors.ToList());
                _verifier.AddError("BehindCase", "Second case has errors");
                return null;
            }
    
            return new FullOutDto();
        }
    
        private async Task WorksSecondCaseAsync(InDto request, CancellationToken cancellationToken)
            => await handlerIn.HandleAsync(request, cancellationToken);
    }
    
  2. Add Your Endpoint
    Define your endpoint using either Minimal API or a Controller, depending on your project's structure.

        private static async Task<IResult> FullCase([FromBody] FullDto dto,
            HttpHandler<FullDto, FullOutDto> handler,
            CancellationToken cancellationToken)
            => await handler.HandleAsync(dto, cancellationToken, _ => Results.NoContent());
    
  3. Register Your Dependencies
    Ensure all required dependencies are registered in your application's dependency injection container.

    builder.Services
        .AddDotEmilu()
        .AddHandlers(Assembly.GetExecutingAssembly())
        .AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
    

For a complete example

Notes:

You need to add the response messages for bad request (400) and server error (500)

{
  "ResultMessage": {
    "ValidationError": {
      "Title": "Bad Request",
      "Detail": "One or more errors"
    },
    "ServerError": {
      "Title": "Server Error",
      "Detail": "Please contact to administrator"
    }
  }
}

And for complex validation use fluent validation

public class SampleValidator : AbstractValidator<SampleRequest>
{
    public SampleValidator()
    {
        RuleFor(s => s.Date)
            .GreaterThanOrEqualTo(DateOnly.FromDateTime(DateTime.Now));

        RuleFor(s => s.Amount)
            .GreaterThan(0);
    }
}
Product 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DotEmilu.Abstractions:

Package Downloads
DotEmilu

Simple .NET library to handle use cases

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 158 25 days ago

Initial release, contains basic contracts to handle general requests