Class QueryDispatcher
Provides a default implementation of of the IQueryDispatcher interface.
public class QueryDispatcher : RequestReplyDispatcher, IQueryDispatcher, IDispatcher
- Inheritance
-
QueryDispatcher
- Implements
- Inherited Members
Examples
This example shows how to route a request-reply query through the built-in query dispatcher. The handler returns a computed order total, which gives the caller an observable outcome from the dispatcher workflow.
using Savvyio;
using Savvyio.Dispatchers;
using Savvyio.Handlers;
using Savvyio.Queries;
namespace ExampleApp;
public sealed class QueryDispatcherExample
{
public decimal Query()
{
var handler = new GetOrderTotalHandler();
var dispatcher = new QueryDispatcher(new ServiceLocator(serviceType => serviceType == typeof(IQueryHandler) ? new object[] { handler } : []));
return dispatcher.Query(new GetOrderTotalQuery("ORD-42"));
}
}
public sealed class GetOrderTotalHandler : IQueryHandler
{
public IRequestReplyActivator<IQuery> Delegates => HandlerFactory.CreateRequestReply<IQuery>(registry => registry.Register<GetOrderTotalQuery, decimal>(query => query.OrderId.Length * 10m));
}
public sealed record GetOrderTotalQuery(string OrderId) : Request, IQuery<decimal>;
Constructors
QueryDispatcher(IServiceLocator)
Initializes a new instance of the QueryDispatcher class.
public QueryDispatcher(IServiceLocator serviceLocator)
Parameters
serviceLocatorIServiceLocatorThe provider of service implementations.
Methods
QueryAsync<TResult>(IQuery<TResult>, Action<AsyncOptions>)
Queries the specified request asynchronous using Request-Reply/In-Out MEP.
public Task<TResult> QueryAsync<TResult>(IQuery<TResult> request, Action<AsyncOptions> setup = null)
Parameters
requestIQuery<TResult>The IQuery<TResult> to request.
setupAction<AsyncOptions>The AsyncOptions which may be configured.
Returns
- Task<TResult>
A Task<TResult> that represents the asynchronous operation. The task result contains the outcome of the query operation.
Type Parameters
TResultThe type of the result to return.
Query<TResult>(IQuery<TResult>)
Queries the specified request using Request-Reply/In-Out MEP.
public TResult Query<TResult>(IQuery<TResult> request)
Parameters
requestIQuery<TResult>The IQuery<TResult> to request.
Returns
- TResult
TResult.
Type Parameters
TResultThe type of the result to return.