IHandlerCast
IHandlerCast indicates that handler is a type-cast of an underlying handler.
/// <summary>Indicates that the service is a decoration that type-casts <see cref="IHandlerBase"/> interfaces, and decorates no other aspect of the source.</summary>
public interface IHandlerCast : IHandlerDecoration, IHandlerBase { }
IHandlerCastable is interface for handlers implementations that can override type-cast behaviour.
/// <summary>Interface for handlers that can override type-cast behaviour.</summary>
public interface IHandlerCastable
{
/// <summary>Try cast the handler into <see cref="IHandler"/>. </summary>
bool TryCast([NotNullWhen(true)] out IHandler casted);
/// <summary>Try cast the handler into <see cref="IHandler"/>. </summary>
bool TryCastAsync([NotNullWhen(true)] out IHandlerAsync casted);
/// <summary>Try cast the handler into <see cref="IHandler{Request, Response}"/>. </summary>
bool TryCast<Request, Response>([NotNullWhen(true)] out IHandler<Request, Response> casted) where Request : notnull;
/// <summary>Try cast the handler into <see cref="IHandlerAsync{Request, Response}"/>. </summary>
bool TryCastAsync<Request, Response>([NotNullWhen(true)] out IHandlerAsync<Request, Response> casted) where Request : notnull;
}
Handler can be casted into other request response generic type parameters.
// Create generics handler
IHandler consoleLogger = new ConsoleLogger();
// Cast to IHandler<object, object>
consoleLogger.TryCast(out IHandler<object, object>? castedHandler);
IServiceBase.AsHandler() casts a service into IHandlerBase.
// Create service
IService service = Services.Create(ConverterHandlers.Instance);
// Cast to handler
IHandler handler = service.AsHandler();
IEntryProviderBase.AsHandler() casts entry provider into IHandlerBase.
// Create entry provider
IEntryProvider entryProvider = (IEntryProvider)Services.Create(ConverterHandlers.Instance);
// Cast to handler
IHandlerBase handler = Handlers.EntryProvider(entryProvider);
If handler implements IHandlerCastable, it can override in casting behaviour.
public class Handler21 : IHandler, IHandlerCastable
{
public void Handle<Request, Response>(IQuery<Request, Response> query) where Request : notnull
=> Console.WriteLine(query.Response);
public bool TryCast([NotNullWhen(true)] out IHandler casted) { casted = null!; return false; }
public bool TryCast<Request, Response>([NotNullWhen(true)] out IHandler<Request, Response> casted) where Request : notnull { casted = null!; return false; }
public bool TryCastAsync([NotNullWhen(true)] out IHandlerAsync casted) { casted = null!; return false; }
public bool TryCastAsync<Request, Response>([NotNullWhen(true)] out IHandlerAsync<Request, Response> casted) where Request : notnull { casted = null!; return false; }
}
Full Example
Full example
using System;
using System.Diagnostics.CodeAnalysis;
using Avalanche.Converter;
using Avalanche.Service;
public class handler_cast
{
public static void Run()
{
{
// <01>
// Create generics handler
IHandler consoleLogger = new ConsoleLogger();
// Cast to IHandler<object, object>
consoleLogger.TryCast(out IHandler<object, object>? castedHandler);
// </01>
}
{
// <02>
// Create service
IService service = Services.Create(ConverterHandlers.Instance);
// Cast to handler
IHandlerBase handler = Handlers.Service(service);
// </02>
}
{
// <03>
// Create service
IService service = Services.Create(ConverterHandlers.Instance);
// Cast to handler
IHandler handler = service.AsHandler();
// </03>
}
{
// <04>
// Create entry provider
IEntryProvider entryProvider = (IEntryProvider)Services.Create(ConverterHandlers.Instance);
// Cast to handler
IHandlerBase handler = Handlers.EntryProvider(entryProvider);
// </04>
}
{
// <05>
// Create entry provider
IEntryProvider entryProvider = (IEntryProvider)Services.Create(ConverterHandlers.Instance);
// Cast to handler
IHandler handler = entryProvider.AsHandler();
// </05>
}
}
// <98>
public class ConsoleLogger : IHandler
{
public void Handle<Request, Response>(IQuery<Request, Response> query) where Request : notnull
=> Console.WriteLine(query.Response);
}
// </98>
// <99>
public class Handler21 : IHandler, IHandlerCastable
{
public void Handle<Request, Response>(IQuery<Request, Response> query) where Request : notnull
=> Console.WriteLine(query.Response);
public bool TryCast([NotNullWhen(true)] out IHandler casted) { casted = null!; return false; }
public bool TryCast<Request, Response>([NotNullWhen(true)] out IHandler<Request, Response> casted) where Request : notnull { casted = null!; return false; }
public bool TryCastAsync([NotNullWhen(true)] out IHandlerAsync casted) { casted = null!; return false; }
public bool TryCastAsync<Request, Response>([NotNullWhen(true)] out IHandlerAsync<Request, Response> casted) where Request : notnull { casted = null!; return false; }
}
// </99>
}