IService
IService interface is the default interface for issuing queries of all types.
/// <summary>Provides services for any request and response type.</summary>
public interface IService : IServiceBase
{
/// <summary>Get possible value.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns><typeparamref name="Response"/> value or null</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
Response? Get<Request, Response>(in Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null) where Request : notnull;
/// <summary>Get value and expect to get value.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns><typeparamref name="Response"/> value</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
Response GetRequired<Request, Response>(in Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null) where Request : notnull;
/// <summary>Try get <typeparamref name="Response"/> value.</summary>
/// <param name="request"></param>
/// <param name="response"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>true if was handled by <paramref name="service"/> and has <paramref name="response"/>.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
bool TryGet<Request, Response>(in Request request, [NotNullWhen(true)] out Response response, CancellationToken cancelToken = default, IDictionary<string, object>? context = null) where Request : notnull;
/// <summary>Get possible value</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>Task for <typeparamref name="Response"/> value.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
Task<Response?> GetAsync<Request, Response>(Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null) where Request : notnull;
/// <summary>Get value and expect to get value.</summary>
/// <typeparam name="Request">The highest level of type the caller wants to provide keys with.</typeparam>
/// <typeparam name="Response">The highest level of type the caller wants read responses as.</typeparam>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>Task for <typeparamref name="Response"/> value.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
Task<Response> GetRequiredAsync<Request, Response>(Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null) where Request : notnull;
/// <param name="request"></param>
/// <param name="response"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>true if was handled by <paramref name="service"/> and has <paramref name="response"/>.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
bool TryGetInitialized<Request, Response>(in Request request, [NotNullWhen(true)] out Response response, CancellationToken cancelToken = default, IDictionary<string, object>? context = null) where Request : notnull;
/// <summary>Get initialized value. Return value once it has been assigned to <see cref="IEntry"/>. It may not be completed when returned.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>Task for <typeparamref name="Response"/> value.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
Task<Response?> GetInitializedAsync<Request, Response>(Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null) where Request : notnull;
/// <summary>Invoke <paramref name="request"/> but do not expect return value.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>true if was handled</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException"></exception>
bool TryInvoke<Request>(in Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null) where Request : notnull;
/// <summary>Invoke <paramref name="request"/> but do not expect return value.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>true if was handled.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
Task<bool> InvokeAsync<Request>(Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null) where Request : notnull;
}
Type parameters are provided to query methods, e.g. GetRequired<Request, Response>().
// Create service
IService service = Services.Create(ServiceHandlers.Instance);
// Create request
IRequestFor<Type> typeRequest = new TypeRequest("System.String");
// Query type
Type type = service.GetRequired<IRequest, Type>(typeRequest);
Any compatible Request and Response types work.
// Create service
IService service = Services.Create(ServiceHandlers.Instance);
// Create request
IRequestFor<Type> typeRequest = new TypeRequest("System.String");
// Query type
IReflect type = service.GetRequired<IRequest, IReflect>(typeRequest);
IService<Request,Response>
IService<Request, Response> have Request and Response types assigned.
/// <summary>Indicates that service has specific <typeparamref name="Request"/>.</summary>
public interface IService<Request> : IServiceT where Request : notnull
{
/// <summary>Invoke <paramref name="request"/> but do not expect any return value.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>true if was handled</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException"></exception>
bool TryInvoke(in Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null);
/// <summary>Invoke <paramref name="request"/> but do not expect any return value.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>true if was handled.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
Task<bool> InvokeAsync(Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null);
}
/// <summary>Indicates that service has specific <typeparamref name="Request"/> and <typeparamref name="Response"/> type.</summary>
public interface IService<Request, Response> : IService<Request>, IServiceOf<Response> where Request : notnull
{
/// <summary>Get possible value.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns><typeparamref name="Response"/> value or null</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
Response? Get(in Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null);
/// <summary>Get value and expect to get value.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns><typeparamref name="Response"/> value</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
Response GetRequired(in Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null);
/// <summary>Try get <typeparamref name="Response"/> value.</summary>
/// <param name="request"></param>
/// <param name="response"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>true if was handled by <paramref name="service"/> and has <paramref name="response"/>.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
bool TryGet(in Request request, out Response? response, CancellationToken cancelToken = default, IDictionary<string, object>? context = null);
/// <summary>Get possible value</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>Task for <typeparamref name="Response"/> value.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
Task<Response?> GetAsync(Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null);
/// <summary>Get value and expect to get value.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>Task for <typeparamref name="Response"/> value.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
Task<Response> GetRequiredAsync(Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null);
/// <summary>Get initialized value. Return value once it has been assigned to <see cref="IEntry"/>. It may not be completed when returned.</summary>
/// <param name="request"></param>
/// <param name="response"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>true if was handled by <paramref name="service"/> and has <paramref name="response"/>.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
bool TryGetInitialized(in Request request, [NotNullWhen(true)] out Response response, CancellationToken cancelToken = default, IDictionary<string, object>? context = null);
/// <summary>Get initialized value. Return value once it has been assigned to <see cref="IEntry"/>. It may not be completed when returned.</summary>
/// <param name="request"></param>
/// <param name="cancelToken">call abort signal</param>
/// <param name="context">optional context parameters. If context parameters match to request, then query is not cached.</param>
/// <returns>Task for <typeparamref name="Response"/> value.</returns>
/// <exception cref="ServiceException"></exception>
/// <exception cref="OperationCanceledException">if token is cancelled</exception>
/// <exception cref="ObjectDisposedException">Service is disposed</exception>
/// <exception cref="InvalidCastException">If result was not <typeparamref name="Response"/> type.</exception>
Task<Response?> GetInitializedAsync(Request request, CancellationToken cancelToken = default, IDictionary<string, object>? context = null);
}
IService<Request, Response> has assigned type parameters and doesn't need them in query methods, e.g. GetRequired().
// Create service
IService<TypeRequest, Type> service = Services.Create<TypeRequest, Type>(ServiceHandlers.Instance);
// Query type
Type type = service.GetRequired("System.String");
Any compatible Request and Response types work.
// Create service
IService<object, object> service = Services.Create<object, object>(ServiceHandlers.Instance);
// Query type
object type = service.GetRequired(new TypeRequest("System.String"));
Full Example
Full example
using System;
using System.Reflection;
using Avalanche.Service;
public class service_service
{
public static void Run()
{
{
// <01>
// Create service
IService service = Services.Create(ServiceHandlers.Instance);
// Create request
IRequestFor<Type> typeRequest = new TypeRequest("System.String");
// Query type
Type type = service.GetRequired<IRequest, Type>(typeRequest);
// </01>
// Print service
Console.WriteLine(service.ToString());
}
{
// <02>
// Create service
IService service = Services.Create(ServiceHandlers.Instance);
// Create request
IRequestFor<Type> typeRequest = new TypeRequest("System.String");
// Query type
IReflect type = service.GetRequired<IRequest, IReflect>(typeRequest);
// </02>
// Print service
Console.WriteLine(service.ToString());
}
{
// <11>
// Create service
IService<TypeRequest, Type> service = Services.Create<TypeRequest, Type>(ServiceHandlers.Instance);
// Query type
Type type = service.GetRequired("System.String");
// </11>
// Print service
Console.WriteLine(service.ToString());
}
{
// <12>
// Create service
IService<object, object> service = Services.Create<object, object>(ServiceHandlers.Instance);
// Query type
object type = service.GetRequired(new TypeRequest("System.String"));
// </12>
// Print service
Console.WriteLine(service.ToString());
}
}
}