fl-shared-utils-mappers
0.2.1
dotnet add package fl-shared-utils-mappers --version 0.2.1
NuGet\Install-Package fl-shared-utils-mappers -Version 0.2.1
<PackageReference Include="fl-shared-utils-mappers" Version="0.2.1" />
<PackageVersion Include="fl-shared-utils-mappers" Version="0.2.1" />
<PackageReference Include="fl-shared-utils-mappers" />
paket add fl-shared-utils-mappers --version 0.2.1
#r "nuget: fl-shared-utils-mappers, 0.2.1"
#:package fl-shared-utils-mappers@0.2.1
#addin nuget:?package=fl-shared-utils-mappers&version=0.2.1
#tool nuget:?package=fl-shared-utils-mappers&version=0.2.1
Fl.Shared.Utils.Mappers
Shared mapper library for .NET web applications. Provides generic mapping abstractions and a ready-made implementation that converts functional types (Option, Error) into ASP.NET Core IResult HTTP responses.
Project structure
Fl.Shared.Utils.Mappers/
├── IMapper.cs
├── IApiResultMapper.cs
└── DataHttpContentMapper.cs
Interfaces
IMapper<T1, T2>
Generic mapping contract between two types.
public interface IMapper<in T1, out T2>
{
T2 Map(T1 item);
}
Use this interface to define any single-direction mapping between an input type T1 and an output type T2.
IApiResultMapper<T>
Specialisation of IMapper for mapping API results to ASP.NET Core IResult. Combines two mapping contracts:
IMapper<Option<T>, IResult>— maps an optional value to an HTTP responseIMapper<Error, IResult>— maps a functional error to an HTTP response
public interface IApiResultMapper<T> :
IMapper<Option<T>, IResult>,
IMapper<Error, IResult>
{ }
Implementations
DataHttpContentMapper<T>
Default implementation of IApiResultMapper<T>. Maps Option<T> and Error values to JSON IResult responses with appropriate HTTP status codes.
| Input | HTTP status | Response body |
|---|---|---|
Option<T>.Some(value) |
200 OK |
JSON-serialised value |
Option<T>.None |
404 Not Found |
{ "message": "Not Found" } |
Error |
500 Internal Server Error |
{ "message": "Internal Server Error" } |
Error responses use ErrorResponse (from Fl.Shared.Utils.Components.Web.Models) with the reason phrase derived from the HTTP status code.
Usage
// Register in DI
services.AddSingleton<IApiResultMapper<MyDto>, DataHttpContentMapper<MyDto>>();
// Use in a minimal API endpoint
app.MapGet("/items/{id}", async (int id, IApiResultMapper<MyDto> mapper, IMyService service) =>
{
Option<MyDto> result = await service.GetById(id);
return mapper.Map(result);
});
Tests
Unit tests are located in tests/Fl.Shared.Utils.Mappers.Tests and cover:
| Class under test | Scenarios |
|---|---|
DataHttpContentMapper<T> |
Error input → 500 JSON with "Internal Server Error" |
DataHttpContentMapper<T> |
Option.None input → 404 JSON with "Not Found" |
DataHttpContentMapper<T> |
Option.Some(value) input → 200 JSON with the original value |
Main dependencies
- LanguageExt — functional types (
Option,Error) Microsoft.AspNetCore.App—IResult,Results,StatusCodesFl.Functional.Utils— functional utilities (Map, etc.)Fl.Shared.Utils.Components.Web.Models—ErrorResponse
| 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. net9.0 is compatible. 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 is compatible. 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. |
-
net10.0
- fl-functional-utils (>= 0.2.1)
- fl-shared-utils-components-web-models (>= 0.2.1)
-
net8.0
- fl-functional-utils (>= 0.2.1)
- fl-shared-utils-components-web-models (>= 0.2.1)
-
net9.0
- fl-functional-utils (>= 0.2.1)
- fl-shared-utils-components-web-models (>= 0.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.