IQuery
IQuery carries query request and response, query specific context information and cancellation token.
/// <summary>Contains query information such as request and response.</summary>
/// <remarks>The resolver implementation that constructs query may add initial <see cref="Context"/> instance that contains session related information.</remarks>
public interface IQuery : IServiceContainer
{
/// <summary>Constraint to request type</summary>
Type RequestType { get; }
/// <summary>Constraint to result type</summary>
Type ResponseType { get; }
/// <summary>Request</summary>
object Request { get; }
/// <summary>Response</summary>
IEntry Response { get; }
/// <summary>Signal whether to break query. If assigned true, then resolver should not pass query to any further handler. The last remaining entry state is to be marked completed.</summary>
bool BreakQuery { get; set; }
/// <summary>Signal that indicates that service's scope or call has been aborted.</summary>
/// <remarks>
/// Most <see cref="IHandlerBase"/> implementations don't need to check cancel request as the calling resolver checks them.
/// However, if <see cref="IHandlerBase"/> does long, async or blocking operations, then periodical checking is suggested.
/// </remarks>
CancellationToken CancellationToken { get; }
/// <summary>Is <see cref="Context"/> initialized.</summary>
bool HasContextData { get; }
/// <summary>Lazy initialized context data.</summary>
IDictionary<string, object>? Context { get; set; }
}
IQuery<Request, Response> is type-casted version of query.
/// <summary>Query interface with strongly typed request and result.</summary>
/// <typeparam name="_Request"></typeparam>
/// <typeparam name="_Response"></typeparam>
public interface IQuery<out _Request, in _Response> : IQuery<_Request> where _Request : notnull
{
/// <summary>Result container</summary>
new IEntryWritable<_Response> Response { get; }
}
Usage
Query<Request, Response> is the default implementation.
// Create service
IService service = Services.Create(ServiceHandlers.Instance);
// Create request
string request = "Request";
// Create response
IEntry<string> entry = new Entry<string>();
// Create query
IQuery<string, string> query = new Query<string, string>(
service: service,
context: new Dictionary<string, object>(),
cancelToken: default,
request: request,
response: entry);
.Request contains the request object.
string _request = query.Request;
.Response contains the response entry.
query.Response.SetValue("Result");
.Service contains the service object for consecutive queries.
query.Service.Get<string, string>("Request");
.Context contains the dictionary for contextual data from caller.
query.Context!["Key"] = "Value";
Full Example
Full example
using System;
using System.Collections.Generic;
using Avalanche.Service;
using static System.Console;
public class query_query
{
public static void Run()
{
{
// <01>
// Create service
IService service = Services.Create(ServiceHandlers.Instance);
// Create request
string request = "Request";
// Create response
IEntry<string> entry = new Entry<string>();
// Create query
IQuery<string, string> query = new Query<string, string>(
service: service,
context: new Dictionary<string, object>(),
cancelToken: default,
request: request,
response: entry);
// </01>
// <02>
string _request = query.Request;
// </02>
// <03>
query.Response.SetValue("Result");
// </03>
// <04>
query.Service.Get<string, string>("Request");
// </04>
// <05>
query.Context!["Key"] = "Value";
// </05>
}
}
}