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 | V-Next |
---|---|---|
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
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
ExecuteUnitRequest<TWebApi, TModelData, TApiData>
: execute any method fromTWebApi
withTModelData
mapped withTApiData
- With result:
ExecuteResultRequest<TWebApi, TApiData>
: execute any method fromTWebApi
with aTApiData
request/result dataExecuteResultRequest<TWebApi, TModelData, TApiData>
: execute any method fromTWebApi
withTModelData
request/result data mapped withTApiData
ExecuteResultRequest<TWebApi, TModelResultData, TApiResultData, TApiRequestData, TModelRequestData>
: execute any method fromTWebApi
, sendingTApiRequestData
mapped fromTModelRequestData
, then returningTModelResultData
mapped fromTApiResultData
CRUD API:
- Read:
ReadQuery<TResultData>
: get theTResultData
entity matching an int keyReadQuery<TResultData, TKey>
: get theTResultData
entity matching aTKey
- ReadAll:
ReadAllQuery<TReadAllResult>
: getTReadAllResult
withIDictionary<string, object>
optional query parametersReadAllQuery<TReadAllParams, TReadAllResult>
: getTReadAllResult
withTReadAllParams
optional query parameters
- Create:
CreateCommand<TModelData>
: create aTModelData
entity
- Update:
UpdateCommand<TRequestData>
: update theTRequestData
entity matching an int keyUpdateCommand<TKey, TRequestData>
: update theTRequestData
entity matching aTKey
- Delete:
DeleteCommand<T>
: delete theT
entity matching an int keyDeleteCommand<T, TKey>
: delete theT
entity matching aTKey