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 |
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 fromTWebApiExecuteSafeUnitRequest<TWebApi>: execute any method fromTWebApi, the safe way withIApizrResponsehandlingExecuteUnitRequest<TWebApi, TModelData, TApiData>: execute any method fromTWebApiwithTModelDatamapped withTApiDataExecuteSafeUnitRequest<TWebApi, TModelData, TApiData>: execute any method fromTWebApiwithTModelDatamapped withTApiData, the safe way withIApizrResponsehandling
- With result:
ExecuteResultRequest<TWebApi, TApiData>: execute any method fromTWebApiwith aTApiDatarequest/result dataExecuteSafeResultRequest<TWebApi, TApiData>: execute any method fromTWebApiwith aTApiDatarequest/result data, the safe way withIApizrResponse<TApiData>handlingExecuteResultRequest<TWebApi, TModelData, TApiData>: execute any method fromTWebApiwithTModelDatarequest/result data mapped withTApiDataExecuteSafeResultRequest<TWebApi, TModelData, TApiData>: execute any method fromTWebApiwithTModelDatarequest/result data mapped withTApiData, the safe way withIApizrResponse<TModelData>handlingExecuteResultRequest<TWebApi, TModelResultData, TApiResultData, TApiRequestData, TModelRequestData>: execute any method fromTWebApi, sendingTApiRequestDatamapped fromTModelRequestData, then returningTModelResultDatamapped fromTApiResultDataExecuteSafeResultRequest<TWebApi, TModelResultData, TApiResultData, TApiRequestData, TModelRequestData>: execute any method fromTWebApi, sendingTApiRequestDatamapped fromTModelRequestData, then returningTModelResultDatamapped fromTApiResultData, the safe way withIApizrResponse<TModelResultData>handling
CRUD API:
- Read:
ReadQuery<TResultData>: get theTResultDataentity matching an int keySafeReadQuery<TResultData>: get theTResultDataentity matching an int key, the safe way withIApizrResponse<TApiData>handlingReadQuery<TResultData, TKey>: get theTResultDataentity matching aTKeySafeReadQuery<TResultData, TKey>: get theTResultDataentity matching aTKey, the safe way withIApizrResponse<TApiData>handling
- ReadAll:
ReadAllQuery<TReadAllResult>: getTReadAllResultwithIDictionary<string, object>optional query parametersSafeReadAllQuery<TReadAllResult>: getTReadAllResultwithIDictionary<string, object>optional query parameters, the safe way withIApizrResponse<TReadAllResult>handlingReadAllQuery<TReadAllParams, TReadAllResult>: getTReadAllResultwithTReadAllParamsoptional query parametersSafeReadAllQuery<TReadAllParams, TReadAllResult>: getTReadAllResultwithTReadAllParamsoptional query parameters, the safe way withIApizrResponse<TReadAllResult>handling
- Create:
CreateCommand<TModelData>: create aTModelDataentitySafeCreateCommand<TModelData>: create aTModelDataentity, the safe way withIApizrResponse<TModelData>handling
- Update:
UpdateCommand<TRequestData>: update theTRequestDataentity matching an int keySafeUpdateCommand<TRequestData>: update theTRequestDataentity matching an int key, the safe way withIApizrResponsehandlingUpdateCommand<TKey, TRequestData>: update theTRequestDataentity matching aTKeySafeUpdateCommand<TKey, TRequestData>: update theTRequestDataentity matching aTKey, the safe way withIApizrResponsehandling
- Delete:
DeleteCommand<T>: delete theTentity matching an int keySafeDeleteCommand<T>: delete theTentity matching an int key, the safe way withIApizrResponsehandlingDeleteCommand<T, TKey>: delete theTentity matching aTKeySafeDeleteCommand<T, TKey>: delete theTentity matching aTKey, the safe way withIApizrResponsehandling