Table of Contents

Configuring MediatR

Apizr offers an integration with MediatR, following the Mediator pattern, available only with the extended approach. Mediator pattern ensures to keep all the thing as loosely coupled as we can between our ViewModel/ViewControler and our Data Access Layer. As everything should be loosely coupled between Views and ViewModels (MVVM) or ViewControlers (MVC) thanks to data binding, MediatR offers you to keep it all loosely coupled between your VM/VC and your DAL too. Please read the official documentation to know more about MediatR. The main benefit in using it with Apizr is to offer you a very simple and unified way to send your request, no matter from where or about what. Simple and unified because instead of injecting/resolving each api interface you need to get your data, you just have to use the IMediator interface, everywhere, every time.

Registering

Please first install this integration package:

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

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

options => options.WithMediation()

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

Note

Sending the safe way

We are sometime talking about Safe request meaning that Refit will handle exceptions and return an IApiResponse to Apizr and then Apizr will return it as an IApizrResponse without throwing. Please read the exception handling doc to get more info.

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 result = await _mediator.Send(YOUR_REQUEST_HERE);

Where YOUR_REQUEST_HERE could be:

Classic API:

  • With no result:
    • ExecuteUnitRequest<TWebApi>: execute any method from TWebApi
    • ExecuteSafeUnitRequest<TWebApi>: execute any method from TWebApi, the safe way with IApizrResponse handling
    • ExecuteUnitRequest<TWebApi, TModelData, TApiData>: execute any method from TWebApi with TModelData mapped with TApiData
    • ExecuteSafeUnitRequest<TWebApi, TModelData, TApiData>: execute any method from TWebApi with TModelData mapped with TApiData, the safe way with IApizrResponse handling
  • With result:
    • ExecuteResultRequest<TWebApi, TApiData>: execute any method from TWebApi with a TApiData request/result data
    • ExecuteSafeResultRequest<TWebApi, TApiData>: execute any method from TWebApi with a TApiData request/result data, the safe way with IApizrResponse<TApiData> handling
    • ExecuteResultRequest<TWebApi, TModelData, TApiData>: execute any method from TWebApi with TModelData request/result data mapped with TApiData
    • ExecuteSafeResultRequest<TWebApi, TModelData, TApiData>: execute any method from TWebApi with TModelData request/result data mapped with TApiData, the safe way with IApizrResponse<TModelData> handling
    • ExecuteResultRequest<TWebApi, TModelResultData, TApiResultData, TApiRequestData, TModelRequestData>: execute any method from TWebApi, sending TApiRequestData mapped from TModelRequestData, then returning TModelResultData mapped from TApiResultData
    • ExecuteSafeResultRequest<TWebApi, TModelResultData, TApiResultData, TApiRequestData, TModelRequestData>: execute any method from TWebApi, sending TApiRequestData mapped from TModelRequestData, then returning TModelResultData mapped from TApiResultData, the safe way with IApizrResponse<TModelResultData> handling

CRUD API:

  • Read:
    • ReadQuery<TResultData>: get the TResultData entity matching an int key
    • SafeReadQuery<TResultData>: get the TResultData entity matching an int key, the safe way with IApizrResponse<TApiData> handling
    • ReadQuery<TResultData, TKey>: get the TResultData entity matching a TKey
    • SafeReadQuery<TResultData, TKey>: get the TResultData entity matching a TKey , the safe way with IApizrResponse<TApiData> handling
  • ReadAll:
    • ReadAllQuery<TReadAllResult>: get TReadAllResult with IDictionary<string, object> optional query parameters
    • SafeReadAllQuery<TReadAllResult>: get TReadAllResult with IDictionary<string, object> optional query parameters, the safe way with IApizrResponse<TReadAllResult> handling
    • ReadAllQuery<TReadAllParams, TReadAllResult>: get TReadAllResult with TReadAllParams optional query parameters
    • SafeReadAllQuery<TReadAllParams, TReadAllResult>: get TReadAllResult with TReadAllParams optional query parameters, the safe way with IApizrResponse<TReadAllResult> handling
  • Create:
    • CreateCommand<TModelData>: create a TModelData entity
    • SafeCreateCommand<TModelData>: create a TModelData entity, the safe way with IApizrResponse<TModelData> handling
  • Update:
    • UpdateCommand<TRequestData>: update the TRequestData entity matching an int key
    • SafeUpdateCommand<TRequestData>: update the TRequestData entity matching an int key, the safe way with IApizrResponse handling
    • UpdateCommand<TKey, TRequestData>: update the TRequestData entity matching a TKey
    • SafeUpdateCommand<TKey, TRequestData>: update the TRequestData entity matching a TKey, the safe way with IApizrResponse handling
  • Delete:
    • DeleteCommand<T>: delete the T entity matching an int key
    • SafeDeleteCommand<T>: delete the T entity matching an int key, the safe way with IApizrResponse handling
    • DeleteCommand<T, TKey>: delete the T entity matching a TKey
    • SafeDeleteCommand<T, TKey>: delete the T entity matching a TKey, the safe way with IApizrResponse handling