Skip to content

Commit 0658f44

Browse files
committed
Add extension methods for registering entities by type
1 parent 6a7880d commit 0658f44

File tree

6 files changed

+86
-5
lines changed

6 files changed

+86
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
- Add specific logging for NotFound error on worker connection by @halspang in ([#413](https://github.com/microsoft/durabletask-dotnet/pull/413))
88
- Add user agent header to gRPC called in ([#417](https://github.com/microsoft/durabletask-dotnet/pull/417))
99
- Enrich User-Agent Header in gRPC Metadata to indicate Client or Worker as caller ([#421](https://github.com/microsoft/durabletask-dotnet/pull/421))
10+
- Add extension methods for registering entities by type
11+
1012
## v1.10.0
1113

1214
- Update DurableTask.Core to v3.1.0 and Bump version to v1.10.0 by @nytian in ([#411](https://github.com/microsoft/durabletask-dotnet/pull/411))

src/Abstractions/DurableTaskRegistry.Activities.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
namespace Microsoft.DurableTask;
77

88
/// <summary>
9-
/// Container for registered <see cref="ITaskOrchestrator" /> and <see cref="ITaskActivity" /> implementations.
9+
/// Container for registered <see cref="ITaskOrchestrator" />, <see cref="ITaskActivity" />,
10+
/// and <see cref="Microsoft.DurableTask.Entities.ITaskEntity"/> implementations.
1011
/// </summary>
1112
public sealed partial class DurableTaskRegistry
1213
{
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.DurableTask.Entities;
5+
using Microsoft.Extensions.DependencyInjection;
6+
7+
namespace Microsoft.DurableTask;
8+
9+
/// <summary>
10+
/// Container for registered <see cref="ITaskOrchestrator" />, <see cref="ITaskActivity" />,
11+
/// and <see cref="ITaskEntity"/> implementations.
12+
/// </summary>
13+
public partial class DurableTaskRegistry
14+
{
15+
/// <summary>
16+
/// Registers an entity factory.
17+
/// </summary>
18+
/// <param name="name">The name of the entity to register.</param>
19+
/// <param name="type">The entity type.</param>
20+
/// <returns>The same registry, for call chaining.</returns>
21+
public DurableTaskRegistry AddEntity(TaskName name, Type type)
22+
{
23+
// TODO: Compile a constructor expression for performance.
24+
Check.ConcreteType<ITaskEntity>(type);
25+
return this.AddEntity(name, sp => (ITaskEntity)ActivatorUtilities.CreateInstance(sp, type));
26+
}
27+
28+
/// <summary>
29+
/// Registers an entity factory. The TaskName used is derived from the provided type information.
30+
/// </summary>
31+
/// <param name="type">The entity type.</param>
32+
/// <returns>The same registry, for call chaining.</returns>
33+
public DurableTaskRegistry AddEntity(Type type)
34+
=> this.AddEntity(type.GetTaskName(), type);
35+
36+
/// <summary>
37+
/// Registers an entity factory.
38+
/// </summary>
39+
/// <typeparam name="TEntity">The type of entity to register.</typeparam>
40+
/// <param name="name">The name of the entity to register.</param>
41+
/// <returns>The same registry, for call chaining.</returns>
42+
public DurableTaskRegistry AddEntity<TEntity>(TaskName name)
43+
where TEntity : class, ITaskEntity
44+
=> this.AddEntity(name, typeof(TEntity));
45+
46+
/// <summary>
47+
/// Registers an entity factory. The TaskName used is derived from the provided type information.
48+
/// </summary>
49+
/// <typeparam name="TEntity">The type of entity to register.</typeparam>
50+
/// <returns>The same registry, for call chaining.</returns>
51+
public DurableTaskRegistry AddEntity<TEntity>()
52+
where TEntity : class, ITaskEntity
53+
=> this.AddEntity(typeof(TEntity));
54+
55+
/// <summary>
56+
/// Registers an entity singleton.
57+
/// </summary>
58+
/// <param name="name">The name of the entity to register.</param>
59+
/// <param name="entity">The entity instance to use.</param>
60+
/// <returns>The same registry, for call chaining.</returns>
61+
public DurableTaskRegistry AddEntity(TaskName name, ITaskEntity entity)
62+
{
63+
Check.NotNull(entity);
64+
return this.AddEntity(name, _ => entity);
65+
}
66+
67+
/// <summary>
68+
/// Registers an entity singleton.
69+
/// </summary>
70+
/// <param name="entity">The entity instance to use.</param>
71+
/// <returns>The same registry, for call chaining.</returns>
72+
public DurableTaskRegistry AddEntity(ITaskEntity entity)
73+
{
74+
Check.NotNull(entity);
75+
return this.AddEntity(entity.GetType().GetTaskName(), entity);
76+
}
77+
}

src/Abstractions/DurableTaskRegistry.Orchestrators.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
namespace Microsoft.DurableTask;
55

66
/// <summary>
7-
/// Container for registered <see cref="ITaskOrchestrator" /> and <see cref="ITaskActivity" /> implementations.
7+
/// Container for registered <see cref="ITaskOrchestrator" />, <see cref="ITaskActivity" />,
8+
/// and <see cref="Microsoft.DurableTask.Entities.ITaskEntity"/> implementations.
89
/// </summary>
910
public partial class DurableTaskRegistry
1011
{

src/Abstractions/DurableTaskRegistry.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
namespace Microsoft.DurableTask;
77

88
/// <summary>
9-
/// Container for registered <see cref="ITaskOrchestrator" /> and <see cref="ITaskActivity" /> implementations.
9+
/// Container for registered <see cref="ITaskOrchestrator" />, <see cref="ITaskActivity" />,
10+
/// and <see cref="ITaskEntity"/> implementations.
1011
/// </summary>
1112
public sealed partial class DurableTaskRegistry
1213
{

src/ScheduledTasks/Extension/DurableTaskWorkerBuilderExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33
using Microsoft.DurableTask.Worker;
4-
using Microsoft.Extensions.DependencyInjection;
54

65
namespace Microsoft.DurableTask.ScheduledTasks;
76

@@ -18,7 +17,7 @@ public static void UseScheduledTasks(this IDurableTaskWorkerBuilder builder)
1817
{
1918
builder.AddTasks(r =>
2019
{
21-
r.AddEntity(nameof(Schedule), sp => ActivatorUtilities.CreateInstance<Schedule>(sp));
20+
r.AddEntity<Schedule>();
2221
r.AddOrchestrator<ExecuteScheduleOperationOrchestrator>();
2322
});
2423
}

0 commit comments

Comments
 (0)