EasyResult 1.0.5

dotnet add package EasyResult --version 1.0.5
                    
NuGet\Install-Package EasyResult -Version 1.0.5
                    
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="EasyResult" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasyResult" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="EasyResult" />
                    
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 EasyResult --version 1.0.5
                    
#r "nuget: EasyResult, 1.0.5"
                    
#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.
#:package EasyResult@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EasyResult&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=EasyResult&version=1.0.5
                    
Install as a Cake Tool

EasyResult

EasyResult is a lightweight library that could be used in your API projects and can handle exceptions
You do not have to pass Result object throughout your whole project, This library is a wrapper on your endpoints and convert your object into Result object for both success and error scenarios.

You can install EasyResult with NuGet

Install-Package EasyResult

Description

This library gives you two options for handling error scenarios:

1. Returning ActionResult methods
2. Throwing Exceptions

Every exception type has only one HttpStatusCode and you have to register it in your code so everytime that exception throws, API knows which HttpStatusCode should return

Instalation

You need to register easy result service as below in ConfigureService method:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddEasyResult();
}

You can also set ResultOption:

public void ConfigureServices(IServiceCollection services)
{
    builder.Services.AddControllers()
    .AddEasyResult(option => 
    {
        option.SuccessDefaultMessage = "My success default message!";
        option.UnhandledExceptionStatusCode = HttpStatusCode.BadGateway;
    });
}

for handling exceptions you have to use exception middleware in Configure method:

public void Configure(IApplicationBuilder app)
{
    app.UseExceptionMiddleware();
}

Configuration

There are two ways to register exceptions in EasyResult:
1. Use IExceptionResult interface (this way can only be used for your customize exceptions).<br/> You have to inherit your customize exception from this generic interface and implement its method and assign your desireable HttpStatusCode

public class BadRequestException : Exception, IExceptionResult<BadRequestException>
{
  public BadRequestException()
  { }

  public BadRequestException(string message)
      : base(message)
  { }

  public BadRequestException(string message, Exception innerException)
      : base(message, innerException)
  { }

  public void Configure(ExceptionResultBuilder<BadRequestException> builder)
  {
      builder.WithHttpStatusCode(HttpStatusCode.BadRequest);
  }
}
  1. Use ExceptionService
    In order to use this way, you have to write a middleware as code below (You have to use this way if you want to register built-in exceptions):
public static void AddExceptions(this IApplicationBuilder app)
{
  using var scope = app.ApplicationServices.GetService<IServiceScopeFactory>()!.CreateScope();
  var exceptionService = scope.ServiceProvider.GetRequiredService<ExceptionService>();
  
  exceptionService.Add(typeof(ApplicationException), HttpStatusCode.Conflict);
  exceptionService.Add(typeof(UnauthorizedAccessException),HttpStatusCode.Unauthorized);
}

If some exception throws and you did not assign its HttpStatusCode, then EasyResult marked it as an unhandled exception and return 500 HttpStatusCode(Or whatever that configured in ResultOption).

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.5 890 12/25/2022
1.0.4 972 8/23/2022
1.0.3.1 972 4/14/2022
1.0.2 985 4/9/2022
1.0.1.1 975 4/5/2022
1.0.0 971 4/3/2022