Delegates
Handlers.Delegate(delegate) converts a delegate into IHandlerBase.
// Create sum calculator
Delegate sumCalculator = (IQuery<(int, int), int> q) => q.Response.SetValue(q.Request.Item1 + q.Request.Item2);
// Cast to handler
IHandlerBase handler = Handlers.Delegate(sumCalculator);
// Create service
IService<(int, int), int> service = Services.Create<(int, int), int>(handler);
Handlers.Action(delegate) converts an Action<IQuery<Request, Response>> into IHandler.
// Create sum calculator
Action<IQuery<(int, int), int>> sumCalculator = q => q.Response.SetValue(q.Request.Item1 + q.Request.Item2);
// Cast to handler
IHandler<(int, int), int> handler = Handlers.Action(sumCalculator);
// Create service
IService<(int, int), int> service = Services.Create<(int, int), int>(handler);
[Order(value)] can be placed on delegate for evaluation order.
// Create sum calculator
Delegate sumCalculator = [Order(100)] (IQuery<(int, int), int> q) => q.Response.SetValue(q.Request.Item1 + q.Request.Item2);
// Cast to handler
IHandlerBase handler = Handlers.Delegate(sumCalculator);
Order can also be passed as second parameter 'order:'.
// Create sum calculator
Delegate sumCalculator = (IQuery<(int, int), int> q) => q.Response.SetValue(q.Request.Item1 + q.Request.Item2);
// Cast to handler
IHandlerBase handler = Handlers.Delegate(sumCalculator, order: 100);
Action<IQuery> handles all request types.
// Create query logger
Action<IQuery> queryLogger = q => Console.WriteLine(q.Response);
// Cast to handler
IHandler loggerHandler = Handlers.Action(queryLogger, order: long.MaxValue);
Func<IQuery<Request, Response>, Task> and Func<IQuery, Task> handles asynchronously.
// Create async handler
Func<IQuery<string, string>, Task> urlDownloader =
async q => q.Response.SetValue(await new WebClient().DownloadStringTaskAsync(q.Request));
// Cast to handler
IHandlerAsync<string, string> handler = Handlers.FuncAsync(urlDownloader, order: 250);
// Create service
IService<string, string> service = Services.Create<string, string>(handler);
// Download
string content = await service.GetRequiredAsync("http://avalanche.fi");
// Print html
Console.WriteLine(content);
If delegate is provided to service construction as is, it's automatically casted into IHandlerBase.
// Create async handler
Func<IQuery<string, string>, Task> urlDownloader =
async q => q.Response.SetValue(await new WebClient().DownloadStringTaskAsync(q.Request));
// Create service
IService<string, string> service = Services.Create<string, string>(urlDownloader);
// Download
string content = await service.GetRequiredAsync("http://avalanche.fi");
// Print html
Console.WriteLine(content);
Full Example
Full example
#pragma warning disable SYSLIB0014
using System;
using System.Net;
using System.Threading.Tasks;
using Avalanche.Module;
using Avalanche.Service;
using Avalanche.Service.Internal;
using static System.Console;
public class handler_delegates
{
public static async Task Run()
{
{
// <01>
// Create sum calculator
Delegate sumCalculator = (IQuery<(int, int), int> q) => q.Response.SetValue(q.Request.Item1 + q.Request.Item2);
// Cast to handler
IHandlerBase handler = Handlers.Delegate(sumCalculator);
// Create service
IService<(int, int), int> service = Services.Create<(int, int), int>(handler);
// </01>
}
{
// <02>
// Create sum calculator
Action<IQuery<(int, int), int>> sumCalculator = q => q.Response.SetValue(q.Request.Item1 + q.Request.Item2);
// Cast to handler
IHandler<(int, int), int> handler = Handlers.Action(sumCalculator);
// Create service
IService<(int, int), int> service = Services.Create<(int, int), int>(handler);
// </02>
}
{
// <03A>
// Create sum calculator
Delegate sumCalculator = [Order(100)] (IQuery<(int, int), int> q) => q.Response.SetValue(q.Request.Item1 + q.Request.Item2);
// Cast to handler
IHandlerBase handler = Handlers.Delegate(sumCalculator);
// </03A>
// Print order
WriteLine(((DelegateHandler)handler).Order);
// Create service
IService<(int, int), int> service = Services.Create<(int, int), int>(handler);
}
{
// <03B>
// Create sum calculator
Delegate sumCalculator = (IQuery<(int, int), int> q) => q.Response.SetValue(q.Request.Item1 + q.Request.Item2);
// Cast to handler
IHandlerBase handler = Handlers.Delegate(sumCalculator, order: 100);
// </03B>
// Create service
IService<(int, int), int> service = Services.Create<(int, int), int>(handler);
}
{
// <04>
// Create query logger
Action<IQuery> queryLogger = q => Console.WriteLine(q.Response);
// Cast to handler
IHandler loggerHandler = Handlers.Action(queryLogger, order: long.MaxValue);
// </04>
// Create service
IService service = Services.Create((ServiceHandlers.Instance, loggerHandler));
// Get type
Type? type = service.Get<TypeRequest, Type>("System.String");
// Logger handler prints "Entry<Type>(Value, Cached, System.String, , Default)" on query
}
{
// <05>
// Create async handler
Func<IQuery<string, string>, Task> urlDownloader =
async q => q.Response.SetValue(await new WebClient().DownloadStringTaskAsync(q.Request));
// Cast to handler
IHandlerAsync<string, string> handler = Handlers.FuncAsync(urlDownloader, order: 250);
// Create service
IService<string, string> service = Services.Create<string, string>(handler);
// Download
string content = await service.GetRequiredAsync("http://avalanche.fi");
// Print html
Console.WriteLine(content);
// </05>
}
{
// <06>
// Create async handler
Func<IQuery<string, string>, Task> urlDownloader =
async q => q.Response.SetValue(await new WebClient().DownloadStringTaskAsync(q.Request));
// Create service
IService<string, string> service = Services.Create<string, string>(urlDownloader);
// Download
string content = await service.GetRequiredAsync("http://avalanche.fi");
// Print html
Console.WriteLine(content);
// </06>
}
}
}
#pragma warning restore SYSLIB0014