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>>
andTask<Option<T, TException>>
without having to use await
Registering
Please first install this integration package:
Project | Current | Upcoming |
---|---|---|
Apizr.Integrations.Optional |
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 fromTWebApi
ExecuteOptionalUnitRequest<TWebApi, TModelData, TApiData>
: execute any method fromTWebApi
withTModelData
mapped withTApiData
- With result:
ExecuteOptionalResultRequest<TWebApi, TApiData>
: execute any method fromTWebApi
with aTApiData
request/result dataExecuteOptionalResultRequest<TWebApi, TModelData, TApiData>
: execute any method fromTWebApi
withTModelData
request/result data mapped withTApiData
ExecuteOptionalResultRequest<TWebApi, TModelResultData, TApiResultData, TApiRequestData, TModelRequestData>
: execute any method fromTWebApi
, sendingTApiRequestData
mapped fromTModelRequestData
, then returningTModelResultData
mapped fromTApiResultData
CRUD API:
- Read:
ReadOptionalQuery<TResultData>
: get theTResultData
entity matching an int keyReadOptionalQuery<TResultData, TKey>
: get theTResultData
entity matching aTKey
- ReadAll:
ReadAllOptionalQuery<TReadAllResult>
: getTReadAllResult
withIDictionary<string, object>
optional query parametersReadAllOptionalQuery<TReadAllParams, TReadAllResult>
: getTReadAllResult
withTReadAllParams
optional query parameters
- Create:
CreateOptionalCommand<TModelData>
: create aTModelData
entity
- Update:
UpdateOptionalCommand<TRequestData>
: update theTRequestData
entity matching an int keyUpdateOptionalCommand<TKey, TRequestData>
: update theTRequestData
entity matching aTKey
- Delete:
DeleteOptionalCommand<T>
: delete theT
entity matching an int keyDeleteOptionalCommand<T, TKey>
: delete theT
entity matching aTKey
You should finaly end with something like:
optionalResult.Match(result =>
{
// Oh yeah, you get a result!
}, e =>
{
// Oh no, something went wrong!
});