Add methods to add multiple workflows from a given assembly

This commit is contained in:
Sipke Schoorstra 2023-05-28 16:20:16 +02:00
parent cd187f93c1
commit c99280b792
8 changed files with 54 additions and 14 deletions

View file

@ -109,7 +109,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Email", "src\modules\E
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Workflows.Designer", "src\modules\Elsa.Workflows.Designer\Elsa.Workflows.Designer.csproj", "{B191F11D-583D-44D9-8A08-F5B0DC58B822}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.HelloWorld", "src\samples\aspnet\Elsa.Samples.HelloWorld\Elsa.Samples.HelloWorld.csproj", "{DF8CCA17-6B00-42C6-B4EC-61B42AA91EE0}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.AspNet.HelloWorld", "src\samples\aspnet\Elsa.Samples.AspNet.HelloWorld\Elsa.Samples.AspNet.HelloWorld.csproj", "{DF8CCA17-6B00-42C6-B4EC-61B42AA91EE0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Testing.Shared", "src\common\Elsa.Testing.Shared\Elsa.Testing.Shared.csproj", "{AD8C4BB0-C78F-496C-B326-1D82C02C7568}"
EndProject

View file

@ -87,7 +87,7 @@ public class WorkflowManagementFeature : FeatureBase
public WorkflowManagementFeature AddActivitiesFrom<TMarker>()
{
var activityTypes = typeof(TMarker).Assembly.GetExportedTypes()
.Where(x => typeof(IActivity).IsAssignableFrom(x) && !x.IsAbstract && !x.IsInterface && !x.IsGenericType)
.Where(x => typeof(IActivity).IsAssignableFrom(x) && x is { IsAbstract: false, IsInterface: false, IsGenericType: false })
.ToList();
return AddActivities(activityTypes);
}

View file

@ -1,3 +1,4 @@
using System.Reflection;
using Elsa.Features.Services;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Runtime.Features;
@ -37,6 +38,20 @@ public static class ModuleExtensions
module.Configure<WorkflowRuntimeFeature>().AddWorkflow<T>();
return module;
}
/// <summary>
/// Register all workflows contained in the assembly containing the specified marker type.
/// </summary>
public static IModule AddWorkflowsFrom<TMarker>(this IModule module) => module.AddWorkflowsFrom(typeof(TMarker).Assembly);
/// <summary>
/// Register all workflows in the specified assembly.
/// </summary>
public static IModule AddWorkflowsFrom(this IModule module, Assembly assembly)
{
module.Configure<WorkflowRuntimeFeature>().AddWorkflowsFrom(assembly);
return module;
}
/// <summary>
/// Configures the default workflow runtime.

View file

@ -12,9 +12,24 @@ public static class WorkflowDictionaryExtensions
/// <summary>
/// Register a workflow factory that creates an instance of the specified workflow type.
/// </summary>
public static void Add<TWorkflow>(this IDictionary<string, Func<IServiceProvider, ValueTask<IWorkflow>>> dictionary) where TWorkflow : IWorkflow => dictionary.Add(typeof(TWorkflow).Name, sp =>
public static void Add<TWorkflow>(this IDictionary<string, Func<IServiceProvider, ValueTask<IWorkflow>>> dictionary) where TWorkflow : IWorkflow
{
var workflow = ActivatorUtilities.GetServiceOrCreateInstance<TWorkflow>(sp);
return new ValueTask<IWorkflow>(workflow);
});
dictionary.Add(typeof(TWorkflow).Name, sp =>
{
var workflow = ActivatorUtilities.GetServiceOrCreateInstance<TWorkflow>(sp);
return new ValueTask<IWorkflow>(workflow);
});
}
/// <summary>
/// Register a workflow factory that creates an instance of the specified workflow type.
/// </summary>
public static void Add(this IDictionary<string, Func<IServiceProvider, ValueTask<IWorkflow>>> dictionary, Type workflowType)
{
dictionary.Add(workflowType.Name, sp =>
{
var workflow = (IWorkflow)ActivatorUtilities.GetServiceOrCreateInstance(sp, workflowType);
return new ValueTask<IWorkflow>(workflow);
});
}
}

View file

@ -1,3 +1,4 @@
using System.Reflection;
using Elsa.Common.Features;
using Elsa.Extensions;
using Elsa.Features.Abstractions;
@ -99,6 +100,21 @@ public class WorkflowRuntimeFeature : FeatureBase
Workflows.Add<T>();
return this;
}
/// <summary>
/// Register all workflows in the specified assembly.
/// </summary>
public WorkflowRuntimeFeature AddWorkflowsFrom(Assembly assembly)
{
var workflowTypes = assembly.GetExportedTypes()
.Where(x => typeof(IWorkflow).IsAssignableFrom(x) && x is { IsAbstract: false, IsInterface: false, IsGenericType: false })
.ToList();
foreach (var workflowType in workflowTypes)
Workflows.Add(workflowType);
return this;
}
/// <inheritdoc />
public override void Configure()

View file

@ -4,7 +4,7 @@ using Elsa.Workflows.Core.Abstractions;
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Contracts;
namespace Elsa.Samples.HelloWorld;
namespace Elsa.Samples.AspNet.HelloWorld;
public class HelloWorldHttpWorkflow : WorkflowBase
{

View file

@ -1,17 +1,11 @@
using Elsa.Extensions;
using Elsa.Samples.HelloWorld;
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
// Add Elsa services.
services.AddElsa(elsa => elsa
// Configure the workflow runtime.
.UseWorkflowRuntime(runtime =>
{
// Register our HTTP workflow with the runtime.
runtime.AddWorkflow<HelloWorldHttpWorkflow>();
})
.AddWorkflowsFrom<Program>()
// Enable Elsa HTTP module (for HTTP related activities).
.UseHttp()