Table of Contents

Configuring Optional.Async

Apizr offers an integration with Optional.Async, following the Optional pattern, available only with the extended approach with MediatR integration activated. Optional.Async offers a strongly typed alternative to null values that lets you:

  • Avoid those pesky null-reference exceptions
  • Signal intent and model your data more explicitly
  • Cut down on manual null checks and focus on your domain
  • It allows you to chain Task<Option<T>> and Task<Option<T, TException>> without having to use await

Registering

Please first install this integration package:

Project Current Upcoming
Apizr.Integrations.Optional NuGet NuGet Pre Release

Then you'll be able to register with this option:

options => options.WithOptionalMediation()

And don't forget to register MediatR itself as usual:

services.AddMediatR(YOUR_REQUESTHANDLER_ASSEMBLIES);

Where YOUR_REQUESTHANDLER_ASSEMBLIES should be the assemblies containing your custom request handlers, if you get some (Apizr MediatR request handlers will be auto registered).

Using

When registered, you don't have to inject/resolve anything else than IMediator, in order to play with your api services (both classic and crud). Everything you need to do then, is sending your request by calling:

var optionalResult = await _mediator.Send(YOUR_REQUEST_HERE);

Where YOUR_REQUEST_HERE could be:

Classic API:

  • With no result:
    • ExecuteOptionalUnitRequest<TWebApi>: execute any method from TWebApi
    • ExecuteOptionalUnitRequest<TWebApi, TModelData, TApiData>: execute any method from TWebApi with TModelData mapped with TApiData
  • With result:
    • ExecuteOptionalResultRequest<TWebApi, TApiData>: execute any method from TWebApi with a TApiData request/result data
    • ExecuteOptionalResultRequest<TWebApi, TModelData, TApiData>: execute any method from TWebApi with TModelData request/result data mapped with TApiData
    • ExecuteOptionalResultRequest<TWebApi, TModelResultData, TApiResultData, TApiRequestData, TModelRequestData>: execute any method from TWebApi, sending TApiRequestData mapped from TModelRequestData, then returning TModelResultData mapped from TApiResultData

CRUD API:

  • Read:
    • ReadOptionalQuery<TResultData>: get the TResultData entity matching an int key
    • ReadOptionalQuery<TResultData, TKey>: get the TResultData entity matching a TKey
  • ReadAll:
    • ReadAllOptionalQuery<TReadAllResult>: get TReadAllResult with IDictionary<string, object> optional query parameters
    • ReadAllOptionalQuery<TReadAllParams, TReadAllResult>: get TReadAllResult with TReadAllParams optional query parameters
  • Create:
    • CreateOptionalCommand<TModelData>: create a TModelData entity
  • Update:
    • UpdateOptionalCommand<TRequestData>: update the TRequestData entity matching an int key
    • UpdateOptionalCommand<TKey, TRequestData>: update the TRequestData entity matching a TKey
  • Delete:
    • DeleteOptionalCommand<T>: delete the T entity matching an int key
    • DeleteOptionalCommand<T, TKey>: delete the T entity matching a TKey

You should finaly end with something like:

optionalResult.Match(result =>
{
    // Oh yeah, you get a result!
}, e =>
{
    // Oh no, something went wrong!
});