Add options to name workflows and activities

Signed-off-by: Frank Buckley <5655810+frankbuckley@users.noreply.github.com>
This commit is contained in:
Frank Buckley 2025-07-03 16:13:12 +01:00
parent 1bbbc739e6
commit ca7d73b2ab
3 changed files with 92 additions and 4 deletions

View File

@ -0,0 +1,16 @@
// ------------------------------------------------------------------------
// Copyright 2025 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Dapr.Workflow.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b1f597635c44597fcecb493e2b1327033b29b1a98ac956a1a538664b68f87d45fbaada0438a15a6265e62864947cc067d8da3a7d93c5eb2fcbb850e396c8684dba74ea477d82a1bbb18932c0efb30b64ff1677f85ae833818707ac8b49ad8062ca01d2c89d8ab1843ae73e8ba9649cd28666b539444dcdee3639f95e2a099bb2")]

View File

@ -31,6 +31,11 @@ public sealed class WorkflowRuntimeOptions
/// </summary>
readonly Dictionary<string, Action<DurableTaskRegistry>> factories = new();
/// <summary>
/// For testing.
/// </summary>
internal IReadOnlyDictionary<string, Action<DurableTaskRegistry>> FactoriesInternal => this.factories;
/// <summary>
/// Override GrpcChannelOptions.
/// </summary>
@ -68,10 +73,11 @@ public sealed class WorkflowRuntimeOptions
/// <summary>
/// Registers a workflow class that derives from <see cref="Workflow{TInput, TOutput}"/>.
/// </summary>
/// <param name="name">Workflow name. If not specified, then the name of <typeparamref name="TWorkflow"/> is used.</param>
/// <typeparam name="TWorkflow">The <see cref="Workflow{TInput, TOutput}"/> type to register.</typeparam>
public void RegisterWorkflow<TWorkflow>() where TWorkflow : class, IWorkflow, new()
public void RegisterWorkflow<TWorkflow>(string? name = null) where TWorkflow : class, IWorkflow, new()
{
string name = typeof(TWorkflow).Name;
name ??= typeof(TWorkflow).Name;
// Dapr workflows are implemented as specialized Durable Task orchestrations
this.factories.Add(name, (DurableTaskRegistry registry) =>
@ -107,10 +113,11 @@ public sealed class WorkflowRuntimeOptions
/// <summary>
/// Registers a workflow activity class that derives from <see cref="WorkflowActivity{TInput, TOutput}"/>.
/// </summary>
/// <param name="name">Activity name. If not specified, then the name of <typeparamref name="TActivity"/> is used.</param>
/// <typeparam name="TActivity">The <see cref="WorkflowActivity{TInput, TOutput}"/> type to register.</typeparam>
public void RegisterActivity<TActivity>() where TActivity : class, IWorkflowActivity
public void RegisterActivity<TActivity>(string? name = null) where TActivity : class, IWorkflowActivity
{
string name = typeof(TActivity).Name;
name ??= typeof(TActivity).Name;
// Dapr workflows are implemented as specialized Durable Task orchestrations
this.factories.Add(name, (DurableTaskRegistry registry) =>

View File

@ -0,0 +1,65 @@
// ------------------------------------------------------------------------
// Copyright 2025 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
namespace Dapr.Workflow.Test;
public class WorkflowRuntimeOptionsTests
{
[Fact]
public void RegisterWorkflow_WithoutName_AddsWorkflowWithTypeName()
{
var options = new WorkflowRuntimeOptions();
options.RegisterWorkflow<TestWorkflow>();
Assert.Contains(typeof(TestWorkflow).Name, options.FactoriesInternal);
}
[Fact]
public void RegisterWorkflow_WithName_AddsWorkflowWithSpecifiedName()
{
var options = new WorkflowRuntimeOptions();
options.RegisterWorkflow<TestWorkflow>("MyWorkflow_v1.0");
Assert.Contains("MyWorkflow_v1.0", options.FactoriesInternal);
}
[Fact]
public void RegisterActivity_WithoutName_AddsWorkflowActivityWithTypeName()
{
var options = new WorkflowRuntimeOptions();
options.RegisterActivity<TestWorkflowActivity>();
Assert.Contains(typeof(TestWorkflowActivity).Name, options.FactoriesInternal);
}
[Fact]
public void RegisterActivity_WithName_AddsWorkflowActivityWithSpecifiedName()
{
var options = new WorkflowRuntimeOptions();
options.RegisterActivity<TestWorkflowActivity>("MyActivity_v1.0");
Assert.Contains("MyActivity_v1.0", options.FactoriesInternal);
}
public class TestWorkflow : Workflow<string, string>
{
public override Task<string> RunAsync(WorkflowContext context, string input)
{
return Task.FromResult(input);
}
}
public class TestWorkflowActivity : WorkflowActivity<string, string>
{
public override Task<string> RunAsync(WorkflowActivityContext context, string input)
{
return Task.FromResult(input);
}
}
}