From 208510a57ce6feaf990424df5a5043caedbcf3e9 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sun, 3 Jul 2022 10:56:34 +0200 Subject: [PATCH] Add sample project Adds a sample demonstrating how to save, load & execute workflow definitions. --- Elsa.sln | 7 ++++ ...mples.LoadAndRunFromDatabaseConsole.csproj | 16 ++++++++ .../Program.cs | 41 +++++++++++++++++++ .../WorkflowDefinitionBuilder.cs | 29 +++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/Elsa.Samples.LoadAndRunFromDatabaseConsole.csproj create mode 100644 src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/Program.cs create mode 100644 src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/WorkflowDefinitionBuilder.cs diff --git a/Elsa.sln b/Elsa.sln index 3ff97f6e2c..647fdf0506 100644 --- a/Elsa.sln +++ b/Elsa.sln @@ -407,6 +407,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.CompensationCo EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Scripting.Sql", "src\scripting\Elsa.Scripting.Sql\Elsa.Scripting.Sql.csproj", "{68BF7E0F-0E52-4477-A5B1-367E87697C75}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.LoadAndRunFromDatabaseConsole", "src\samples\console\Elsa.Samples.LoadAndRunFromDatabaseConsole\Elsa.Samples.LoadAndRunFromDatabaseConsole.csproj", "{507FE3FC-F775-46CF-8820-B80C6FCFD348}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -977,6 +979,10 @@ Global {68BF7E0F-0E52-4477-A5B1-367E87697C75}.Debug|Any CPU.Build.0 = Debug|Any CPU {68BF7E0F-0E52-4477-A5B1-367E87697C75}.Release|Any CPU.ActiveCfg = Release|Any CPU {68BF7E0F-0E52-4477-A5B1-367E87697C75}.Release|Any CPU.Build.0 = Release|Any CPU + {507FE3FC-F775-46CF-8820-B80C6FCFD348}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {507FE3FC-F775-46CF-8820-B80C6FCFD348}.Debug|Any CPU.Build.0 = Debug|Any CPU + {507FE3FC-F775-46CF-8820-B80C6FCFD348}.Release|Any CPU.ActiveCfg = Release|Any CPU + {507FE3FC-F775-46CF-8820-B80C6FCFD348}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1160,6 +1166,7 @@ Global {F9731DF7-CEB1-4B9B-8838-803275E301E4} = {B43B546E-23F3-46E8-ACB7-D04F05CDA180} {962653DD-BFE7-4DDB-9B3D-5AC8C222F43F} = {FC9F520F-BA51-4AD2-BFEE-EF787798E734} {68BF7E0F-0E52-4477-A5B1-367E87697C75} = {9C3C685E-3D7D-4638-A031-24F71CF74B52} + {507FE3FC-F775-46CF-8820-B80C6FCFD348} = {FC9F520F-BA51-4AD2-BFEE-EF787798E734} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {8B0975FD-7050-48B0-88C5-48C33378E158} diff --git a/src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/Elsa.Samples.LoadAndRunFromDatabaseConsole.csproj b/src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/Elsa.Samples.LoadAndRunFromDatabaseConsole.csproj new file mode 100644 index 0000000000..cb1dc84b32 --- /dev/null +++ b/src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/Elsa.Samples.LoadAndRunFromDatabaseConsole.csproj @@ -0,0 +1,16 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + diff --git a/src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/Program.cs b/src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/Program.cs new file mode 100644 index 0000000000..452a55d534 --- /dev/null +++ b/src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/Program.cs @@ -0,0 +1,41 @@ +using Elsa; +using Elsa.Models; +using Elsa.Persistence; +using Elsa.Persistence.EntityFramework.Core.Extensions; +using Elsa.Persistence.EntityFramework.Sqlite; +using Elsa.Samples.LoadAndRunFromDatabaseConsole; +using Elsa.Services; +using Microsoft.Extensions.DependencyInjection; + +// Create a service container with Elsa services. +var services = new ServiceCollection() + .AddElsa(options => options + .UseEntityFrameworkPersistence(ef => ef.UseSqlite()) + .AddConsoleActivities() + ) + .BuildServiceProvider(); + +// Run DB migrations. +await services.GetRequiredService().StartupAsync(); + +// Get a workflow definition store for saving & loading workflow definitions. +var workflowDefinitionStore = services.GetRequiredService(); + +// Create a workflow to store in DB (for demo purposes). +var workflowDefinition = WorkflowDefinitionBuilder.CreateDemoWorkflowDefinition(); + +// Add or update the workflow definition. +await workflowDefinitionStore.SaveAsync(workflowDefinition); + +// Load the workflow definition from DB. +workflowDefinition = (await workflowDefinitionStore.FindByDefinitionIdAsync(workflowDefinition.DefinitionId, VersionOptions.Published))!; + +// To execute a workflow definition, we need to create a workflow blueprint from it. +var workflowBlueprintMaterializer = services.GetRequiredService(); +var workflowBlueprint = await workflowBlueprintMaterializer.CreateWorkflowBlueprintAsync(workflowDefinition); + +// Get a workflow runner. +var workflowRunner = services.GetRequiredService(); + +// Run the workflow. +await workflowRunner.StartWorkflowAsync(workflowBlueprint); \ No newline at end of file diff --git a/src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/WorkflowDefinitionBuilder.cs b/src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/WorkflowDefinitionBuilder.cs new file mode 100644 index 0000000000..5191c90371 --- /dev/null +++ b/src/samples/console/Elsa.Samples.LoadAndRunFromDatabaseConsole/WorkflowDefinitionBuilder.cs @@ -0,0 +1,29 @@ +using Elsa.Activities.Console; +using Elsa.Models; + +namespace Elsa.Samples.LoadAndRunFromDatabaseConsole; + +public static class WorkflowDefinitionBuilder +{ + public static WorkflowDefinition CreateDemoWorkflowDefinition() => new() + { + Id = "SampleWorkflow-Version-1", + DefinitionId = "SampleWorkflow", + Version = 1, + IsPublished = true, + IsLatest = true, + PersistenceBehavior = WorkflowPersistenceBehavior.Suspended, + Activities = new[] + { + new ActivityDefinition + { + ActivityId = "activity-1", + Type = nameof(WriteLine), + Properties = new List() + { + ActivityDefinitionProperty.Liquid(nameof(WriteLine.Text), "Hello World") + } + }, + } + }; +} \ No newline at end of file