|
| 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 | +} |
0 commit comments