RequestAttribute
[Request] attribute indicates that if a field contains a value that implements IRequest or IRecord, it will be resolved before processing the query further.
public record Sum([Request] object a, [Request] object b) : IRequestFor<int>;
[RequestArray] indicates that if a field contains a value that implements IEnumerable of IRequest or IRecord, it will be resolved before processing the query further.
public record Sum([RequestArray] object values) : IRequestFor<int>;
Using both attributes together indicates that field may contain a request for an array whose elements are to be resolved.
public record Sum([Request, RequestArray] object values) : IRequestFor<int>;
Service must have ServiceHandlers or its subcomponent RequestHandlers for resolve to work.
IService service = Services.Create((new StringJoinHandler(), RequestHandlers.Instance, RequestResolverAsync.Instance));
IService service = Services.Create((new StringJoinHandler(), ServiceHandlers.Instance));
In the following example, a handler concatenates strings with separator. Field-requests are resolved before handler is invoked.
// Create service
IService service = Services.Create((RequestHandlers.Instance, new StringJoinHandler()));
// Create request
IRequestFor<string> joinRequest = new StringJoinRequest(", ", "Hello", new StringJoinRequest("+", "World", "Sphere"));
// Print request
WriteLine(joinRequest.PrintTree());
// StringJoinRequest
// ├── Separator = ", "
// ├── Strings[0] = "Hello"
// └── Strings[1] = StringJoinRequest
// ├── Separator = "+"
// ├── Strings[0] = "World"
// └── Strings[1] = "Sphere"
// Execute request
string joinText = service.GetRequired<IRequestFor<string>, string>(joinRequest);
// Print join
Console.WriteLine(joinText); // "Hello, World+Sphere"
/// <summary>Join strings with <see cref="String.Join"/></summary>
public record StringJoinRequest : IRequestFor<string>
{
/// <summary>Separator string, <see cref="String"/>, <![CDATA[IRequestFor<string>]]></summary>
[Request]
public object Separator { get; init; }
/// <summary>Strings to join as array</summary>
[Request, RequestArray]
public object Strings { get; init; }
/// <summary>Create request</summary>
public StringJoinRequest(object separator, params object[] strings)
{
Separator = separator;
Strings = strings;
}
}
/// <summary>Join strings</summary>
public class StringJoinHandler : IHandler<StringJoinRequest, string>
{
/// <summary>Join strings</summary>
public void Handle(IQuery<StringJoinRequest, string> query)
{
// Handled
if (query.Handled()) return;
// Get separator
if (query.Request.Separator is not string separator) return;
// Get strings
if (query.Request.Strings is not Array strings) return;
// Create builder
StringBuilder sb = new StringBuilder();
//
for (int i = 0; i < strings.Length; i++)
{
// Append separator
if (i > 0) sb.Append(separator);
// Get value
string? value = strings.GetValue(i)?.ToString();
// Append string
if (value != null) sb.Append(value);
}
// Build text
string text = sb.ToString();
// Assign result
query.Response.SetValue(text);
}
}
Full Example
Full example
using System;
using System.Text;
using Avalanche.Service;
using Avalanche.Utilities.Record;
using static System.Console;
public class request_requestattribute
{
public static void Run()
{
{
// <01>
IService service = Services.Create((new StringJoinHandler(), RequestHandlers.Instance, RequestResolverAsync.Instance));
// </01>
}
{
// <02>
IService service = Services.Create((new StringJoinHandler(), ServiceHandlers.Instance));
// </02>
}
{
// <03>
// Create service
IService service = Services.Create((RequestHandlers.Instance, new StringJoinHandler()));
// Create request
IRequestFor<string> joinRequest = new StringJoinRequest(", ", "Hello", new StringJoinRequest("+", "World", "Sphere"));
// Print request
WriteLine(joinRequest.PrintTree());
// StringJoinRequest
// ├── Separator = ", "
// ├── Strings[0] = "Hello"
// └── Strings[1] = StringJoinRequest
// ├── Separator = "+"
// ├── Strings[0] = "World"
// └── Strings[1] = "Sphere"
// Execute request
string joinText = service.GetRequired<IRequestFor<string>, string>(joinRequest);
// Print join
Console.WriteLine(joinText); // "Hello, World+Sphere"
// </03>
}
}
static class _10
{
// <10>
public record Sum([Request] object a, [Request] object b) : IRequestFor<int>;
// </10>
}
static class _11
{
// <11>
public record Sum([RequestArray] object values) : IRequestFor<int>;
// </11>
}
static class _12
{
// <12>
public record Sum([Request, RequestArray] object values) : IRequestFor<int>;
// </12>
}
// <13>
/// <summary>Join strings with <see cref="String.Join"/></summary>
public record StringJoinRequest : IRequestFor<string>
{
/// <summary>Separator string, <see cref="String"/>, <![CDATA[IRequestFor<string>]]></summary>
[Request]
public object Separator { get; init; }
/// <summary>Strings to join as array</summary>
[Request, RequestArray]
public object Strings { get; init; }
/// <summary>Create request</summary>
public StringJoinRequest(object separator, params object[] strings)
{
Separator = separator;
Strings = strings;
}
}
// </13>
// <14>
/// <summary>Join strings</summary>
public class StringJoinHandler : IHandler<StringJoinRequest, string>
{
/// <summary>Join strings</summary>
public void Handle(IQuery<StringJoinRequest, string> query)
{
// Handled
if (query.Handled()) return;
// Get separator
if (query.Request.Separator is not string separator) return;
// Get strings
if (query.Request.Strings is not Array strings) return;
// Create builder
StringBuilder sb = new StringBuilder();
//
for (int i = 0; i < strings.Length; i++)
{
// Append separator
if (i > 0) sb.Append(separator);
// Get value
string? value = strings.GetValue(i)?.ToString();
// Append string
if (value != null) sb.Append(value);
}
// Build text
string text = sb.ToString();
// Assign result
query.Response.SetValue(text);
}
}
// </14>
}