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 fromTWebApi
ExecuteSafeUnitRequest<TWebApi>
: execute any method fromTWebApi
, the safe way withIApizrResponse
handlingExecuteUnitRequest<TWebApi, TModelData, TApiData>
: execute any method fromTWebApi
withTModelData
mapped withTApiData
ExecuteSafeUnitRequest<TWebApi, TModelData, TApiData>
: execute any method fromTWebApi
withTModelData
mapped withTApiData
, the safe way withIApizrResponse
handling
- With result:
ExecuteResultRequest<TWebApi, TApiData>
: execute any method fromTWebApi
with aTApiData
request/result dataExecuteSafeResultRequest<TWebApi, TApiData>
: execute any method fromTWebApi
with aTApiData
request/result data, the safe way withIApizrResponse<TApiData>
handlingExecuteResultRequest<TWebApi, TModelData, TApiData>
: execute any method fromTWebApi
withTModelData
request/result data mapped withTApiData
ExecuteSafeResultRequest<TWebApi, TModelData, TApiData>
: execute any method fromTWebApi
withTModelData
request/result data mapped withTApiData
, the safe way withIApizrResponse<TModelData>
handlingExecuteResultRequest<TWebApi, TModelResultData, TApiResultData, TApiRequestData, TModelRequestData>
: execute any method fromTWebApi
, sendingTApiRequestData
mapped fromTModelRequestData
, then returningTModelResultData
mapped fromTApiResultData
ExecuteSafeResultRequest<TWebApi, TModelResultData, TApiResultData, TApiRequestData, TModelRequestData>
: execute any method fromTWebApi
, sendingTApiRequestData
mapped fromTModelRequestData
, then returningTModelResultData
mapped fromTApiResultData
, the safe way withIApizrResponse<TModelResultData>
handling
CRUD API:
- Read:
ReadQuery<TResultData>
: get theTResultData
entity matching an int keySafeReadQuery<TResultData>
: get theTResultData
entity matching an int key, the safe way withIApizrResponse<TApiData>
handlingReadQuery<TResultData, TKey>
: get theTResultData
entity matching aTKey
SafeReadQuery<TResultData, TKey>
: get theTResultData
entity matching aTKey
, the safe way withIApizrResponse<TApiData>
handling
- ReadAll:
ReadAllQuery<TReadAllResult>
: getTReadAllResult
withIDictionary<string, object>
optional query parametersSafeReadAllQuery<TReadAllResult>
: getTReadAllResult
withIDictionary<string, object>
optional query parameters, the safe way withIApizrResponse<TReadAllResult>
handlingReadAllQuery<TReadAllParams, TReadAllResult>
: getTReadAllResult
withTReadAllParams
optional query parametersSafeReadAllQuery<TReadAllParams, TReadAllResult>
: getTReadAllResult
withTReadAllParams
optional query parameters, the safe way withIApizrResponse<TReadAllResult>
handling
- Create:
CreateCommand<TModelData>
: create aTModelData
entitySafeCreateCommand<TModelData>
: create aTModelData
entity, the safe way withIApizrResponse<TModelData>
handling
- Update:
UpdateCommand<TRequestData>
: update theTRequestData
entity matching an int keySafeUpdateCommand<TRequestData>
: update theTRequestData
entity matching an int key, the safe way withIApizrResponse
handlingUpdateCommand<TKey, TRequestData>
: update theTRequestData
entity matching aTKey
SafeUpdateCommand<TKey, TRequestData>
: update theTRequestData
entity matching aTKey
, the safe way withIApizrResponse
handling
- Delete:
DeleteCommand<T>
: delete theT
entity matching an int keySafeDeleteCommand<T>
: delete theT
entity matching an int key, the safe way withIApizrResponse
handlingDeleteCommand<T, TKey>
: delete theT
entity matching aTKey
SafeDeleteCommand<T, TKey>
: delete theT
entity matching aTKey
, the safe way withIApizrResponse
handling