Class CommandWorker<TInput, TOutput, TCommand>
- Namespace
- Virtufin.Worker.DevKit
- Assembly
- Virtufin.Worker.DevKit.dll
Generic command-dispatching worker. Builds on WorkerBase<TInput, TOutput>: the base class handles codec decode/encode and envelope construction; this class adds strongly-typed command-string extraction and enum dispatch.
Format-agnostic: this class makes no assumption about payload format. The 2-type-param convenience CommandWorker<T> pre-bakes the JsonCloudEventCodec; workers using non-JSON payloads extend the 3-type-param form directly and supply their own codec.
public abstract class CommandWorker<TInput, TOutput, TCommand> : WorkerBase<TInput, TOutput>, IWorker where TCommand : struct, Enum
Type Parameters
TInputCodec-decoded input payload type.
TOutputCodec-encoded output payload type.
TCommandEnum whose identifier names are the wire-level command names (e.g.
WebSocketManagerCommand.createfor the wire string"create").
- Inheritance
-
WorkerBase<TInput, TOutput>CommandWorker<TInput, TOutput, TCommand>
- Implements
-
IWorker
- Derived
- Inherited Members
Constructors
CommandWorker(string, ICloudEventCodec<TInput, TOutput>)
A command worker of type workerType, named by the naming library (see WorkerTypeNames).
protected CommandWorker(string workerType, ICloudEventCodec<TInput, TOutput> codec)
Parameters
workerTypestringcodecICloudEventCodec<TInput, TOutput>
CommandWorker(Uri, string, ICloudEventCodec<TInput, TOutput>)
protected CommandWorker(Uri source, string responseEventType, ICloudEventCodec<TInput, TOutput> codec)
Parameters
sourceUriresponseEventTypestringcodecICloudEventCodec<TInput, TOutput>
Fields
FieldCommand
Field name of the command string in the typed input payload.
public const string FieldCommand = "command"
Field Value
Methods
ErrorPayload(string)
Build a typed error payload. The base class ProcessAsync(CloudEvent) encodes this via the codec and wraps it in a response CloudEvent (with correlationid, timestamp, etc.).
protected virtual TOutput? ErrorPayload(string message)
Parameters
messagestring
Returns
- TOutput
Remarks
Subclasses override this if the codec has a non-trivial error representation (e.g. a dedicated error type).
ExtractCommand(TInput)
Subclasses override to extract the command string from the typed
input payload. Default: not implemented. Override must read the
payload in a payload-format-specific way (JSON: node["command"],
Protobuf: dedicated field, etc.).
protected abstract string? ExtractCommand(TInput payload)
Parameters
payloadTInput
Returns
HandleCommandAsync(CloudEvent, TCommand, TInput)
Subclasses override to implement command-specific worker logic.
Receives the decoded TInput payload and the
parsed command value.
protected abstract Task<TOutput?> HandleCommandAsync(CloudEvent input, TCommand command, TInput payload)
Parameters
inputCloudEventcommandTCommandpayloadTInput
Returns
- Task<TOutput>
ProcessAsync(CloudEvent, TInput)
Sealed dispatch: extract the command string from the typed input, parse the enum, call HandleCommandAsync(CloudEvent, TCommand, TInput). Returns a typed error payload (encoded by the codec) on missing/unknown command.
protected override sealed Task<TOutput?> ProcessAsync(CloudEvent input, TInput payload)
Parameters
inputCloudEventpayloadTInput
Returns
- Task<TOutput>