|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace Microsoft.DurableTask; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Base class for an orchestration. |
| 8 | +/// </summary> |
| 9 | +/// <typeparam name="TInput">The orchestration input type.</typeparam> |
| 10 | +/// <typeparam name="TOutput">The orchestration output type.</typeparam> |
| 11 | +public abstract class Orchestrator<TInput, TOutput> : ITaskOrchestrator |
| 12 | + where TInput : IOrchestrationRequest<TOutput> |
| 13 | +{ |
| 14 | + /// <inheritdoc/> |
| 15 | + Type ITaskOrchestrator.InputType => typeof(TInput); |
| 16 | + |
| 17 | + /// <inheritdoc/> |
| 18 | + Type ITaskOrchestrator.OutputType => typeof(TOutput); |
| 19 | + |
| 20 | + /// <inheritdoc/> |
| 21 | + async Task<object?> ITaskOrchestrator.RunAsync(TaskOrchestrationContext context, object? input) |
| 22 | + { |
| 23 | + Check.NotNull(context, nameof(context)); |
| 24 | + OrchestratorHelper.ValidateInput(input, out TInput typedInput); |
| 25 | + |
| 26 | + return await this.RunAsync(context, typedInput); |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Override to implement task orchestrator logic. |
| 31 | + /// </summary> |
| 32 | + /// <param name="context">The task orchestrator's context.</param> |
| 33 | + /// <param name="input">The deserialized orchestration input.</param> |
| 34 | + /// <returns>The output of the orchestration as a task.</returns> |
| 35 | + public abstract Task<TOutput> RunAsync(TaskOrchestrationContext context, TInput input); |
| 36 | +} |
| 37 | + |
| 38 | +/// <summary> |
| 39 | +/// Base class for an orchestration. |
| 40 | +/// </summary> |
| 41 | +/// <typeparam name="TInput">The orchestration input type.</typeparam> |
| 42 | +public abstract class Orchestrator<TInput> : ITaskOrchestrator |
| 43 | + where TInput : IOrchestrationRequest<Unit> |
| 44 | +{ |
| 45 | + /// <inheritdoc/> |
| 46 | + Type ITaskOrchestrator.InputType => typeof(TInput); |
| 47 | + |
| 48 | + /// <inheritdoc/> |
| 49 | + Type ITaskOrchestrator.OutputType => typeof(Unit); |
| 50 | + |
| 51 | + /// <inheritdoc/> |
| 52 | + async Task<object?> ITaskOrchestrator.RunAsync(TaskOrchestrationContext context, object? input) |
| 53 | + { |
| 54 | + Check.NotNull(context, nameof(context)); |
| 55 | + OrchestratorHelper.ValidateInput(input, out TInput typedInput); |
| 56 | + |
| 57 | + await this.RunAsync(context, typedInput); |
| 58 | + return Unit.Value; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Override to implement task orchestrator logic. |
| 63 | + /// </summary> |
| 64 | + /// <param name="context">The task orchestrator's context.</param> |
| 65 | + /// <param name="input">The deserialized orchestration input.</param> |
| 66 | + /// <returns>The output of the orchestration as a task.</returns> |
| 67 | + public abstract Task RunAsync(TaskOrchestrationContext context, TInput input); |
| 68 | +} |
| 69 | + |
| 70 | +/// <summary> |
| 71 | +/// Orchestration implementation helpers. |
| 72 | +/// </summary> |
| 73 | +static class OrchestratorHelper |
| 74 | +{ |
| 75 | + /// <summary> |
| 76 | + /// Due to nullable reference types being static analysis only, we need to do our best efforts for validating the |
| 77 | + /// input type, but also give control of nullability to the implementation. It is not ideal, but we do not want to |
| 78 | + /// force 'TInput?' on the RunAsync implementation. |
| 79 | + /// </summary> |
| 80 | + /// <typeparam name="TInput">The input type of the orchestration.</typeparam> |
| 81 | + /// <param name="input">The input object.</param> |
| 82 | + /// <param name="typedInput">The input converted to the desired type.</param> |
| 83 | + public static void ValidateInput<TInput>(object? input, out TInput typedInput) |
| 84 | + { |
| 85 | + if (input is TInput typed) |
| 86 | + { |
| 87 | + // Quick pattern check. |
| 88 | + typedInput = typed; |
| 89 | + return; |
| 90 | + } |
| 91 | + else if (input is not null && typeof(TInput) != input.GetType()) |
| 92 | + { |
| 93 | + throw new ArgumentException($"Input type '{input?.GetType()}' does not match expected type '{typeof(TInput)}'."); |
| 94 | + } |
| 95 | + |
| 96 | + // Input is null and did not match a nullable value type. We do not have enough information to tell if it is |
| 97 | + // valid or not. We will have to defer this decision to the implementation. Additionally, we will coerce a null |
| 98 | + // input to a default value type here. This is to keep the two RunAsync(context, default) overloads to have |
| 99 | + // identical behavior. |
| 100 | + typedInput = default!; |
| 101 | + return; |
| 102 | + } |
| 103 | +} |
0 commit comments