| | | 1 | | using System.Reflection; |
| | | 2 | | using Elsa.Expressions.Extensions; |
| | | 3 | | using Elsa.Workflows.Management.Models; |
| | | 4 | | using Elsa.Workflows.Management.Options; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | // ReSharper disable once CheckNamespace |
| | | 8 | | namespace Elsa.Workflows.Management.Extensions; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// <see cref="IServiceCollection"/> extension methods for registering Elsa activity types |
| | | 12 | | /// and variable descriptors via <see cref="ManagementOptions"/>. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// These extensions are the shell-feature-compatible replacement for calling |
| | | 16 | | /// <c>WorkflowManagementFeature.AddActivitiesFrom<T>()</c> in the old-style feature system. |
| | | 17 | | /// Because <c>services.Configure<ManagementOptions></c> is additive, multiple features |
| | | 18 | | /// can independently register activities without any coupling to each other. |
| | | 19 | | /// </remarks> |
| | | 20 | | public static class ManagementServiceCollectionExtensions |
| | | 21 | | { |
| | | 22 | | // ------------------------------------------------------------------------- |
| | | 23 | | // Activities |
| | | 24 | | // ------------------------------------------------------------------------- |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Registers the supplied activity <paramref name="types"/> with <see cref="ManagementOptions"/>. |
| | | 28 | | /// </summary> |
| | | 29 | | public static IServiceCollection AddActivities(this IServiceCollection services, IEnumerable<Type> types) => |
| | 0 | 30 | | services.Configure<ManagementOptions>(options => |
| | 0 | 31 | | { |
| | 0 | 32 | | foreach (var type in types) |
| | 0 | 33 | | options.ActivityTypes.Add(type); |
| | 0 | 34 | | }); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Registers a single activity type <typeparamref name="TActivity"/> with <see cref="ManagementOptions"/>. |
| | | 38 | | /// </summary> |
| | | 39 | | public static IServiceCollection AddActivity<TActivity>(this IServiceCollection services) |
| | | 40 | | where TActivity : IActivity => |
| | 0 | 41 | | services.AddActivities([typeof(TActivity)]); |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Scans <paramref name="assembly"/> and registers every concrete, non-generic |
| | | 45 | | /// <see cref="IActivity"/> implementation found. |
| | | 46 | | /// </summary> |
| | | 47 | | public static IServiceCollection AddActivitiesFrom(this IServiceCollection services, Assembly assembly) |
| | | 48 | | { |
| | 0 | 49 | | var types = assembly.GetExportedTypes() |
| | 0 | 50 | | .Where(t => typeof(IActivity).IsAssignableFrom(t) |
| | 0 | 51 | | && t is { IsAbstract: false, IsInterface: false, IsGenericTypeDefinition: false }); |
| | 0 | 52 | | return services.AddActivities(types); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Scans the assembly that contains <typeparamref name="TMarker"/> and registers every |
| | | 57 | | /// concrete, non-generic <see cref="IActivity"/> implementation found. |
| | | 58 | | /// </summary> |
| | | 59 | | public static IServiceCollection AddActivitiesFrom<TMarker>(this IServiceCollection services) => |
| | 0 | 60 | | services.AddActivitiesFrom(typeof(TMarker).Assembly); |
| | | 61 | | |
| | | 62 | | // ------------------------------------------------------------------------- |
| | | 63 | | // Variable descriptors |
| | | 64 | | // ------------------------------------------------------------------------- |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Registers the supplied <paramref name="descriptors"/> with <see cref="ManagementOptions"/>. |
| | | 68 | | /// </summary> |
| | | 69 | | public static IServiceCollection AddVariableDescriptors( |
| | | 70 | | this IServiceCollection services, |
| | | 71 | | IEnumerable<VariableDescriptor> descriptors) => |
| | 0 | 72 | | services.Configure<ManagementOptions>(options => |
| | 0 | 73 | | { |
| | 0 | 74 | | foreach (var descriptor in descriptors) |
| | 0 | 75 | | options.VariableDescriptors.Add(descriptor); |
| | 0 | 76 | | }); |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Registers a single variable descriptor. |
| | | 80 | | /// </summary> |
| | | 81 | | public static IServiceCollection AddVariableDescriptor( |
| | | 82 | | this IServiceCollection services, |
| | | 83 | | VariableDescriptor descriptor) => |
| | 0 | 84 | | services.AddVariableDescriptors([descriptor]); |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Registers a variable descriptor for <typeparamref name="T"/> with the given |
| | | 88 | | /// <paramref name="category"/> and optional <paramref name="description"/>. |
| | | 89 | | /// </summary> |
| | | 90 | | public static IServiceCollection AddVariableDescriptor<T>( |
| | | 91 | | this IServiceCollection services, |
| | | 92 | | string category, |
| | | 93 | | string? description = null) => |
| | 0 | 94 | | services.AddVariableDescriptor(new(typeof(T), category, description)); |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Adds a variable type and its alias to the specified service collection. |
| | | 98 | | /// </summary> |
| | | 99 | | public static IServiceCollection AddVariableTypeAndAlias<T>(this IServiceCollection services, string alias, string c |
| | | 100 | | { |
| | 0 | 101 | | return services |
| | 0 | 102 | | .AddVariableDescriptor<T>(category) |
| | 0 | 103 | | .AddTypeAlias<T>(alias); |
| | | 104 | | } |
| | | 105 | | } |