Skip to content

Commit

Permalink
Add sample project
Browse files Browse the repository at this point in the history
Adds a sample demonstrating how to save, load & execute workflow definitions.
  • Loading branch information
sfmskywalker committed Jul 3, 2022
1 parent 252b4d0 commit 208510a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Elsa.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\activities\webhooks\Elsa.Webhooks.Persistence.EntityFramework.Sqlite\Elsa.Webhooks.Persistence.EntityFramework.Sqlite.csproj" />
<ProjectReference Include="..\..\..\core\Elsa\Elsa.csproj" />
<ProjectReference Include="..\..\..\persistence\Elsa.Persistence.EntityFramework\Elsa.Persistence.EntityFramework.Sqlite\Elsa.Persistence.EntityFramework.Sqlite.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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<IStartupRunner>().StartupAsync();

// Get a workflow definition store for saving & loading workflow definitions.
var workflowDefinitionStore = services.GetRequiredService<IWorkflowDefinitionStore>();

// 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<IWorkflowBlueprintMaterializer>();
var workflowBlueprint = await workflowBlueprintMaterializer.CreateWorkflowBlueprintAsync(workflowDefinition);

// Get a workflow runner.
var workflowRunner = services.GetRequiredService<IStartsWorkflow>();

// Run the workflow.
await workflowRunner.StartWorkflowAsync(workflowBlueprint);
Original file line number Diff line number Diff line change
@@ -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>()
{
ActivityDefinitionProperty.Liquid(nameof(WriteLine.Text), "Hello World")
}
},
}
};
}

0 comments on commit 208510a

Please sign in to comment.