forked from elsa-workflows/elsa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkflowBase.cs
81 lines (68 loc) · 2.71 KB
/
WorkflowBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Core.Models;
namespace Elsa.Workflows.Core.Abstractions;
/// <summary>
/// A base class for implementing workflow definitions using the pipelineBuilder API.
/// </summary>
public abstract class WorkflowBase : IWorkflow
{
/// <summary>
/// Invokes the the specified <see cref="IWorkflowBuilder"/>.
/// </summary>
protected virtual ValueTask BuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken = default)
{
Build(builder);
return ValueTask.CompletedTask;
}
/// <summary>
/// Invokes the specified <see cref="IWorkflowBuilder"/>.
/// </summary>
/// <param name="builder"></param>
protected virtual void Build(IWorkflowBuilder builder)
{
}
/// <summary>
/// Gives derived types a chance to setup the <see cref="IWorkflowBuilder"/> before the BuildAsync method is invoked.
/// </summary>
protected virtual ValueTask BeforeBuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken = default) => ValueTask.CompletedTask;
/// <summary>
/// Gives derived types a chance to setup the <see cref="IWorkflowBuilder"/> after the BuildAsync method was invoked.
/// </summary>
protected virtual ValueTask AfterBuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken = default) => ValueTask.CompletedTask;
async ValueTask IWorkflow.BuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken)
{
await BeforeBuildAsync(builder, cancellationToken);
await BuildAsync(builder, cancellationToken);
await AfterBuildAsync(builder, cancellationToken);
}
}
/// <summary>
/// A base class for implementing workflow definitions that can return a result using the pipelineBuilder API.
/// </summary>
public abstract class WorkflowBase<TResult> : WorkflowBase
{
/// <inheritdoc />
protected WorkflowBase()
{
Result = new Variable<TResult>();
}
/// <summary>
/// Use this variable from your workflow to assign a result value.
/// </summary>
protected Variable<TResult> Result { get; }
/// <inheritdoc />
protected override ValueTask BeforeBuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken = default)
{
builder.Result = Result;
return ValueTask.CompletedTask;
}
/// <inheritdoc />
protected override ValueTask AfterBuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken = default)
{
var variables = builder.Variables;
if(!variables.Contains(Result))
variables.Add(Result);
builder.Variables = variables;
return ValueTask.CompletedTask;
}
}