Converts LanguageExt monads to AspNetCore's IResult for minimal APIs or IActionResult
for controller based APIs.
dotnet add package Oneiro.AspNetCore.LanguageExt
- Any monad that has a proper object will return a success code of
200 OKwith a response body - Any monad that returns
Unitwill return a success code of204 No Content - The
Option<T>monad will either be a success of200or204and the none condition is converted to a404 Not Found- Monad with nested
Option<T>such asFin<Option<T>>could return200,204,404, or500asFin<Option<T>>could be a success with an option ofUnit(204)T(200)None(404)Error(500).
- Monad with nested
- The
Trymonad will return either a 200, 204, or 500 - The
TryOptionmonad will return either a 200, 204, 404, or 500 - The
Effmonad will return a 200, 204, or 500 - The
Affmonad will return a 200, 204, or 500
Proper IEFT problem detail responses will be presented with the appropriate
status code based on the exception that is contained within the Fin, Try, TryOption, Eff, and Aff monads.
LanguageExt monads can be converted to Action or IResult. The extensions functions can be used by importing Oneiro.
using Oneiro;
//. Other code
IResult result = Try(() => "Hello, World!").ToResult();using static LanguageExt.Prelude;
using Oneiro;
interface IService { Option<string> GetMessage(); }
class MyService : IService {
Option<string> GetMessage() => "Hello, World!";
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTransient<IService, MyService>();
var app = builder.Build();
app.MapGet("/", (IService service) => service.GetMessage().ToResult());
app.Run();