| | | 1 | | using Elsa.Workflows; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | // ReSharper disable once CheckNamespace |
| | | 5 | | namespace Elsa.Extensions; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for <see cref="IDictionary{TKey,TValue}"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class WorkflowDictionaryExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Register a workflow factory that creates an instance of the specified workflow type. |
| | | 14 | | /// </summary> |
| | | 15 | | public static void Add<TWorkflow>(this IDictionary<string, Func<IServiceProvider, ValueTask<IWorkflow>>> dictionary) |
| | | 16 | | { |
| | 0 | 17 | | dictionary.Add(typeof(TWorkflow)); |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Register a workflow factory that creates an instance of the specified workflow type. |
| | | 22 | | /// </summary> |
| | | 23 | | public static void Add(this IDictionary<string, Func<IServiceProvider, ValueTask<IWorkflow>>> dictionary, Type workf |
| | | 24 | | { |
| | 232 | 25 | | ValidateWorkflowType(workflowType); |
| | | 26 | | |
| | 223 | 27 | | if (string.IsNullOrWhiteSpace(workflowType.FullName)) |
| | 0 | 28 | | throw new ArgumentException($"Workflow type '{workflowType.Name}' must have a full name.", nameof(workflowTy |
| | | 29 | | |
| | 223 | 30 | | var key = workflowType.GetSimpleAssemblyQualifiedName(); |
| | 223 | 31 | | var legacyKey = workflowType.FullName; |
| | 223 | 32 | | var hasFactory = dictionary.TryGetValue(key, out var factory); |
| | | 33 | | |
| | 223 | 34 | | if (!hasFactory && !string.IsNullOrWhiteSpace(legacyKey)) |
| | 223 | 35 | | hasFactory = dictionary.TryGetValue(legacyKey, out factory); |
| | | 36 | | |
| | 223 | 37 | | if (!hasFactory) |
| | | 38 | | { |
| | 222 | 39 | | factory = sp => |
| | 222 | 40 | | { |
| | 2361 | 41 | | var workflow = (IWorkflow)ActivatorUtilities.GetServiceOrCreateInstance(sp, workflowType); |
| | 2361 | 42 | | return new ValueTask<IWorkflow>(workflow); |
| | 222 | 43 | | }; |
| | | 44 | | } |
| | | 45 | | |
| | 223 | 46 | | dictionary[key] = factory!; |
| | | 47 | | |
| | 223 | 48 | | if (!string.IsNullOrWhiteSpace(legacyKey) && legacyKey != key) |
| | 223 | 49 | | dictionary[legacyKey] = factory!; |
| | 223 | 50 | | } |
| | | 51 | | |
| | | 52 | | private static void ValidateWorkflowType(Type workflowType) |
| | | 53 | | { |
| | 232 | 54 | | if (!typeof(IWorkflow).IsAssignableFrom(workflowType)) |
| | 3 | 55 | | throw new ArgumentException($"Workflow type '{GetDisplayName(workflowType)}' must implement {nameof(IWorkflo |
| | | 56 | | |
| | 229 | 57 | | if (workflowType.IsAbstract || workflowType.IsInterface || workflowType.ContainsGenericParameters) |
| | 6 | 58 | | throw new ArgumentException($"Workflow type '{GetDisplayName(workflowType)}' must be a concrete, closed type |
| | 223 | 59 | | } |
| | | 60 | | |
| | 9 | 61 | | private static string GetDisplayName(Type type) => type.FullName ?? type.Name; |
| | | 62 | | } |