Delegates
Delegate Action<IQuery<Request, Response>>) can be converted into IHandler with Handlers.Func().
// 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.Func(sumCalculator);
// Create service
IService<(int, int), int> service = Services.Create<(int, int), int>(handler);
Evaluation order can be passed as second parameter 'order:'.
// Cast to handler
IHandler<(int, int), int> handler = Handlers.Func(sumCalculator, order: 100);
Generic handler with delegate Action<IQuery>.
// Create query logger
Action<IQuery> queryLogger = q => Console.WriteLine(q.Response);
// Cast to handler
IHandler handler = Handlers.Func(queryLogger, order: long.MaxValue);
Async handler with Func<IQuery<Request, Response>, Task> and Func<IQuery, Task>.
// 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);
If delegate is provided to service construction as is, it's automatically casted into IHandler with evaluation order 0.
// 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);
Full Example
Full example
#pragma warning disable SYSLIB0014
using System;
using System.Net;
using System.Threading.Tasks;
using Avalanche.Service;
public class handler_delegates
{
public static void Run()
{
{
// <0>
// 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.Func(sumCalculator);
// Create service
IService<(int, int), int> service = Services.Create<(int, int), int>(handler);
// </0>
}
{
// Create sum calculator
Action<IQuery<(int, int), int>> sumCalculator = q => q.Response.SetValue(q.Request.Item1 + q.Request.Item2);
// <1>
// Cast to handler
IHandler<(int, int), int> handler = Handlers.Func(sumCalculator, order: 100);
// </1>
// Create service
IService<(int, int), int> service = Services.Create<(int, int), int>(handler);
}
{
// <2>
// Create query logger
Action<IQuery> queryLogger = q => Console.WriteLine(q.Response);
// Cast to handler
IHandler handler = Handlers.Func(queryLogger, order: long.MaxValue);
// </2>
}
{
// <3>
// 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);
// </3>
// Download
string content = service.GetRequired("http://avalanche.fi");
//
Console.WriteLine(content);
}
{
// <4>
// 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);
// </4>
}
}
}
#pragma warning restore SYSLIB0014