Introduction
Request is an intermediary type that is used between handler producers and service consumers.
Class, struct and record can be used as a request. It can be named as a verb for invoking action, or noun for query.
/// <summary>Request to print record-like object as json-string</summary>
public record PrintAsJson(Object record);
Requests should be immutable, or used as such.
record PrintAsJson
{
public object? Record { get; init; }
}
If request type is class or struct then Hash-equals comparability must be implemented. If request is record then .GetHashCode and .Equals are implemented by compiler.
/// <summary>Request to print record-like object as json-string</summary>
public class PrintAsJson
{
/// <summary>Record to print</summary>
public readonly object record;
/// <summary>Create request to print <paramref name="record"/></summary>
public PrintAsJson(object record) => this.record = record;
/// <summary>Compare to <paramref name="obj"/></summary>
public override bool Equals(object? obj) => obj is PrintAsJson casted ? casted.record.Equals(this.record) : false;
/// <summary>Request specific hashcode</summary>
public override int GetHashCode() => record.GetHashCode() ^ 213;
}
}
/// <summary>Request to print record-like object as json-string</summary>
public struct PrintAsJson
{
/// <summary>Record to print</summary>
public readonly object record;
/// <summary>Create request to print <paramref name="record"/></summary>
public PrintAsJson(object record) => this.record = record;
/// <summary>Compare to <paramref name="obj"/></summary>
public override bool Equals(object? obj) => obj is PrintAsJson casted ? casted.record.Equals(this.record) : false;
/// <summary>Request specific hashcode</summary>
public override int GetHashCode() => record.GetHashCode() ^ 213;
}
Any type can be used for a request. However, usually suitable only for narrow-purposed services as the type name doesn't indicate the requested target.
// Handler that doubles value
Action<IQuery<int, int>> h = q => q.Response.SetValue(q.Request * 2);
// Create service
IService<int, int> doubleService = Services.Create<int, int>(h);
// Double value
int x = doubleService.GetRequired(10);
[IgnoreDataMember] indicates that field or property shouls not be considered as field of request.
/// <summary>Request to print record-like object as json-string</summary>
public record PrintAsJson
{
/// <summary></summary>
public object? Record { get; init; }
/// <summary>Print info</summary>
[IgnoreDataMember]
public String Info => $"Prints {Record} as json.";
}
[DataMember] instructs explicit order and name.
public record MyRequest
{
[DataMember(Name = "FieldName", Order = 1)]
public String? SomeField { get; init; }
[DataMember(Name = "FieldName2", Order = 2)]
public object? AnotherField { get; init; }
}
readonly struct are very efficient request types as they are pointer referenced in sync service calls.
/// <summary>Request to print record-like object as json-string</summary>
public readonly record struct PrintAsJson(Object record);
sealed classes have more efficient serialization if request is used over network or file.
/// <summary>Request to print record-like object as json-string</summary>
public sealed record PrintAsJson(Object record);
Full Example
Full example
#pragma warning disable CS0649
using System;
using System.Runtime.Serialization;
using Avalanche.Service;
using Avalanche.Utilities.Record;
public class request_index
{
public static void Run()
{
{
// <05>
// Handler that doubles value
Action<IQuery<int, int>> h = q => q.Response.SetValue(q.Request * 2);
// Create service
IService<int, int> doubleService = Services.Create<int, int>(h);
// Double value
int x = doubleService.GetRequired(10);
// </05>
}
}
static class _01
{
// <01>
/// <summary>Request to print record-like object as json-string</summary>
public record PrintAsJson(Object record);
// </01>
}
static class _02
{
// <02>
record PrintAsJson
{
public object? Record { get; init; }
}
// </02>
}
static class _03
{
// <03>
/// <summary>Request to print record-like object as json-string</summary>
public class PrintAsJson
{
/// <summary>Record to print</summary>
public readonly object record;
/// <summary>Create request to print <paramref name="record"/></summary>
public PrintAsJson(object record) => this.record = record;
/// <summary>Compare to <paramref name="obj"/></summary>
public override bool Equals(object? obj) => obj is PrintAsJson casted ? casted.record.Equals(this.record) : false;
/// <summary>Request specific hashcode</summary>
public override int GetHashCode() => record.GetHashCode() ^ 213;
}
}
// </03>
static class _04
{
// <04>
/// <summary>Request to print record-like object as json-string</summary>
public struct PrintAsJson
{
/// <summary>Record to print</summary>
public readonly object record;
/// <summary>Create request to print <paramref name="record"/></summary>
public PrintAsJson(object record) => this.record = record;
/// <summary>Compare to <paramref name="obj"/></summary>
public override bool Equals(object? obj) => obj is PrintAsJson casted ? casted.record.Equals(this.record) : false;
/// <summary>Request specific hashcode</summary>
public override int GetHashCode() => record.GetHashCode() ^ 213;
}
// </04>
}
static class _06
{
// <06>
/// <summary>Request to print record-like object as json-string</summary>
public record PrintAsJson
{
/// <summary></summary>
public object? Record { get; init; }
/// <summary>Print info</summary>
[IgnoreDataMember]
public String Info => $"Prints {Record} as json.";
}
// </06>
}
static class _07
{
// <07>
public record MyRequest
{
[DataMember(Name = "FieldName", Order = 1)]
public String? SomeField { get; init; }
[DataMember(Name = "FieldName2", Order = 2)]
public object? AnotherField { get; init; }
}
// </07>
}
static class _08
{
// <08>
/// <summary>Request to print record-like object as json-string</summary>
public readonly record struct PrintAsJson(Object record);
// </08>
}
static class _09
{
// <09>
/// <summary>Request to print record-like object as json-string</summary>
public sealed record PrintAsJson(Object record);
// </09>
}
}
#pragma warning restore CS0649