Skip to content

Commit 4ca5d1e

Browse files
committed
Add Mediator pattern via request objects
1 parent e235184 commit 4ca5d1e

11 files changed

+362
-243
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.DurableTask;
5+
6+
/// <summary>
7+
/// Represents the base request to run a <see cref="ITaskActivity" />.
8+
/// </summary>
9+
public interface IBaseActivityRequest
10+
{
11+
/// <summary>
12+
/// Gets the <see cref="TaskName" /> representing the <see cref="ITaskActivity" /> to run.
13+
/// </summary>
14+
/// <returns>A <see cref="TaskName" />.</returns>
15+
/// <remarks>
16+
/// This is a function instead of a property so it is excluded in serialization without needing to use a
17+
/// serialization library specific attribute to exclude it.
18+
/// </remarks>
19+
TaskName GetTaskName();
20+
}
21+
22+
/// <summary>
23+
/// Represents a request to run a <see cref="ITaskActivity" /> which returns <typeparamref name="TResult" />.
24+
/// </summary>
25+
/// <typeparam name="TResult">The result of the orchestrator that is to be ran.</typeparam>
26+
public interface IActivityRequest<out TResult> : IBaseActivityRequest
27+
{
28+
}
29+
30+
/// <summary>
31+
/// Represents a request to run a <see cref="ITaskActivity" /> which has no return.
32+
/// </summary>
33+
public interface IActivityRequest : IActivityRequest<Unit>
34+
{
35+
}
36+
37+
/// <summary>
38+
/// Helpers for creating activity requests.
39+
/// </summary>
40+
public static class ActivityRequest
41+
{
42+
/// <summary>
43+
/// Gets an <see cref="IActivityRequest{TResult}" /> which has an explicitly provided input.
44+
/// </summary>
45+
/// <remarks>
46+
/// This is useful when you want to use an existing type for input (like <see cref="string" />) and not derive an
47+
/// entirely new type.
48+
/// </remarks>
49+
/// <typeparam name="TResult">The result type of the activity.</typeparam>
50+
/// <param name="name">The name of the activity to run.</param>
51+
/// <param name="input">The input for the activity.</param>
52+
/// <returns>A request that can be used to enqueue an activity.</returns>
53+
public static IActivityRequest<TResult> Create<TResult>(TaskName name, object? input = null)
54+
=> new Request<TResult>(name, input);
55+
56+
/// <summary>
57+
/// Represents an activity request where the input is not the request itself.
58+
/// </summary>
59+
/// <typeparam name="TResult">The result type.</typeparam>
60+
class Request<TResult> : IActivityRequest<TResult>, IProvidesInput
61+
{
62+
readonly TaskName name;
63+
readonly object? input;
64+
65+
/// <summary>
66+
/// Initializes a new instance of the <see cref="Request{TResult}"/> class.
67+
/// </summary>
68+
/// <param name="name">The task name.</param>
69+
/// <param name="input">The input.</param>
70+
public Request(TaskName name, object? input)
71+
{
72+
this.name = name;
73+
this.input = input;
74+
}
75+
76+
/// <inheritdoc/>
77+
public object? GetInput() => this.input;
78+
79+
/// <inheritdoc/>
80+
public TaskName GetTaskName() => this.name;
81+
}
82+
}

src/Abstractions/IProvidesInput.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.DurableTask;
5+
6+
/// <summary>
7+
/// Contract for providing input to an orchestration or activity.
8+
/// </summary>
9+
interface IProvidesInput
10+
{
11+
/// <summary>
12+
/// Gets the input for the orchestration or activity.
13+
/// </summary>
14+
/// <returns>The input value.</returns>
15+
/// <remarks>
16+
/// This is a method and not a property to ensure it is not included in serialization.
17+
/// </remarks>
18+
object? GetInput();
19+
}

src/Abstractions/Mediator/IOrchestrationRequest.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Abstractions/Mediator/Orchestrator.cs

Lines changed: 0 additions & 103 deletions
This file was deleted.

src/Abstractions/Mediator/TaskOrchestrationContextRequestExtensions.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
namespace Microsoft.DurableTask;
5+
6+
/// <summary>
7+
/// Represents the base request to run a <see cref="ITaskOrchestrator" />.
8+
/// </summary>
9+
public interface IBaseOrchestrationRequest
10+
{
11+
/// <summary>
12+
/// Gets the <see cref="TaskName" /> representing the <see cref="ITaskOrchestrator" /> to run.
13+
/// </summary>
14+
/// <returns>A <see cref="TaskName" />.</returns>
15+
/// <remarks>
16+
/// This is a function instead of a property so it is excluded in serialization without needing to use a
17+
/// serialization library specific attribute to exclude it.
18+
/// </remarks>
19+
TaskName GetTaskName();
20+
}
21+
22+
/// <summary>
23+
/// Represents a request to run a <see cref="ITaskOrchestrator" /> which returns <typeparamref name="TResult" />.
24+
/// </summary>
25+
/// <typeparam name="TResult">The result of the orchestrator that is to be ran.</typeparam>
26+
public interface IOrchestrationRequest<out TResult> : IBaseOrchestrationRequest
27+
{
28+
}
29+
30+
/// <summary>
31+
/// Represents a request to run a <see cref="ITaskOrchestrator" /> which has no return.
32+
/// </summary>
33+
public interface IOrchestrationRequest : IOrchestrationRequest<Unit>
34+
{
35+
}
36+
37+
/// <summary>
38+
/// Helpers for creating orchestration requests.
39+
/// </summary>
40+
public static class OrchestrationRequest
41+
{
42+
/// <summary>
43+
/// Gets an <see cref="IOrchestrationRequest{TResult}" /> which has an explicitly provided input.
44+
/// </summary>
45+
/// <remarks>
46+
/// This is useful when you want to use an existing type for input (like <see cref="string" />) and not derive an
47+
/// entirely new type.
48+
/// </remarks>
49+
/// <typeparam name="TResult">The result type of the orchestration.</typeparam>
50+
/// <param name="name">The name of the orchestration to run.</param>
51+
/// <param name="input">The input for the orchestration.</param>
52+
/// <returns>A request that can be used to enqueue an orchestration.</returns>
53+
public static IOrchestrationRequest<TResult> Create<TResult>(TaskName name, object? input = null)
54+
=> new Request<TResult>(name, input);
55+
56+
/// <summary>
57+
/// Represents an orchestration request where the input is not the request itself.
58+
/// </summary>
59+
/// <typeparam name="TResult">The result type.</typeparam>
60+
class Request<TResult> : IOrchestrationRequest<TResult>, IProvidesInput
61+
{
62+
readonly TaskName name;
63+
readonly object? input;
64+
65+
/// <summary>
66+
/// Initializes a new instance of the <see cref="Request{TResult}"/> class.
67+
/// </summary>
68+
/// <param name="name">The task name.</param>
69+
/// <param name="input">The input.</param>
70+
public Request(TaskName name, object? input)
71+
{
72+
this.name = name;
73+
this.input = input;
74+
}
75+
76+
/// <inheritdoc/>
77+
public object? GetInput() => this.input;
78+
79+
/// <inheritdoc/>
80+
public TaskName GetTaskName() => this.name;
81+
}
82+
}

0 commit comments

Comments
 (0)