FunctionalDdd.Asp 1.1.1

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package FunctionalDdd.Asp --version 1.1.1
NuGet\Install-Package FunctionalDdd.Asp -Version 1.1.1
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="FunctionalDdd.Asp" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FunctionalDdd.Asp --version 1.1.1
#r "nuget: FunctionalDdd.Asp, 1.1.1"
#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.
// Install FunctionalDdd.Asp as a Cake Addin
#addin nuget:?package=FunctionalDdd.Asp&version=1.1.1

// Install FunctionalDdd.Asp as a Cake Tool
#tool nuget:?package=FunctionalDdd.Asp&version=1.1.1

ASP Extension

This library will help convert Error objects to ASP.NET Core ActionResult.

  • ToOkActionResult
  • ToErrorActionResult

ToOkActionResult

Use this method to convert Result to OkObjectResult or various failed results.

ToErrorActionResult

Use this method to convert Error to various failed results. The mapping is as follows

    NotFoundError => (ActionResult<T>)controllerBase.NotFound(error),
    ValidationError validation => ValidationErrors<T>(validation, controllerBase),
    ConflictError => (ActionResult<T>)controllerBase.Conflict(error),
    UnauthorizedError => (ActionResult<T>)controllerBase.Unauthorized(error),
    ForbiddenError => (ActionResult<T>)controllerBase.StatusCode(StatusCodes.Status403Forbidden, error),
    UnexpectedError => (ActionResult<TValue>)controllerBase.StatusCode(StatusCodes.Status500InternalServerError, error),
    _ => (ActionResult<TValue>)controllerBase.StatusCode(StatusCodes.Status500InternalServerError, error),

Example

Simple case.

[HttpPost("[action]")]
public ActionResult<User> Register([FromBody] RegisterRequest request) =>
    FirstName.TryCreate(request.firstName)
    .Combine(LastName.TryCreate(request.lastName))
    .Combine(EmailAddress.TryCreate(request.email))
    .Bind((firstName, lastName, email) => SampleWebApplication.User.TryCreate(firstName, lastName, email, request.password))
    .ToOkActionResult(this);

To control the return type

[HttpPost("[action]")]
public ActionResult<User> RegisterCreated2([FromBody] RegisterRequest request) =>
    FirstName.TryCreate(request.firstName)
    .Combine(LastName.TryCreate(request.lastName))
    .Combine(EmailAddress.TryCreate(request.email))
    .Bind((firstName, lastName, email) => SampleWebApplication.User.TryCreate(firstName, lastName, email, request.password))
    .Finally(
        ok => CreatedAtAction("Get", new { name = ok.FirstName }, ok),
        err => err.ToErrorActionResult<User>(this));

ToPartialOrOkActionResult

ToPartialOrOkActionResult can be used to support pagination. The function takes in three parameters to, from and length and based on the values will return PartialContent (206) or Okay(200) per RFC9110

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
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
2.0.0-alpha.38 40 4/24/2024
2.0.0-alpha.33 48 4/17/2024
2.0.0-alpha.26 58 4/9/2024
2.0.0-alpha.21 66 4/1/2024
2.0.0-alpha.19 55 3/5/2024
2.0.0-alpha.18 50 2/28/2024
2.0.0-alpha.17 50 2/26/2024
2.0.0-alpha.15 74 1/30/2024
2.0.0-alpha.8 50 1/27/2024
2.0.0-alpha.6 90 1/5/2024
1.1.1 536 11/15/2023
1.1.0-alpha.32 96 11/2/2023
1.1.0-alpha.30 190 10/31/2023
1.1.0-alpha.28 74 10/28/2023
1.1.0-alpha.27 69 10/28/2023
1.1.0-alpha.24 65 10/20/2023
1.1.0-alpha.23 70 10/13/2023
1.1.0-alpha.22 67 10/13/2023
1.1.0-alpha.21 79 10/1/2023
1.1.0-alpha.20 66 9/30/2023
1.1.0-alpha.19 73 9/30/2023
1.1.0-alpha.18 80 9/29/2023
1.1.0-alpha.17 62 9/22/2023
1.1.0-alpha.13 68 9/16/2023
1.1.0-alpha.4 113 6/9/2023
1.1.0-alpha.3 66 6/8/2023
1.0.1 553 5/12/2023
0.1.0-alpha.40 104 4/6/2023
0.1.0-alpha.39 98 4/3/2023
0.1.0-alpha.38 126 4/2/2023
0.1.0-alpha.37 105 3/31/2023
0.1.0-alpha.35 102 3/29/2023
0.1.0-alpha.34 92 3/28/2023
0.1.0-alpha.32 119 3/18/2023
0.1.0-alpha.30 116 3/11/2023
0.1.0-alpha.27 117 3/7/2023
0.1.0-alpha.24 115 2/15/2023
0.1.0-alpha.22 106 2/15/2023
0.1.0-alpha.20 109 2/13/2023
0.1.0-alpha.19 77 2/13/2023
0.0.1-alpha.14 133 1/4/2023
0.0.1-alpha.4 115 12/30/2022
0.0.1-alpha.3 129 12/23/2022
0.0.1-alpha 510 12/21/2022