Update Sample17

This commit is contained in:
Sipke Schoorstra 2019-10-24 21:45:19 +02:00
parent 0ea4cd3671
commit 82ce170f8d
9 changed files with 80 additions and 48 deletions

View file

@ -24,7 +24,7 @@ namespace Elsa.Activities.Console.Activities
{
private readonly TextReader input;
public ReadLine() : this(System.Console.In)
public ReadLine()
{
}
@ -53,7 +53,7 @@ namespace Elsa.Activities.Console.Activities
protected override ActivityExecutionResult OnResume(WorkflowExecutionContext context)
{
var receivedInput = (string) context.Workflow.Input[VariableName];
var receivedInput = (string) context.Workflow.Input["ReadLineInput"];
return Execute(context, receivedInput);
}

View file

@ -1,4 +1,5 @@
using System;
using System.Linq.Expressions;
using Elsa.Models;
using Elsa.Services.Models;
@ -12,6 +13,7 @@ namespace Elsa.Services
IActivityBuilder Add<T>(Action<T> setup = default, string id = null) where T : class, IActivity;
IOutcomeBuilder When(string outcome);
IActivityBuilder Then<T>(Action<T> setup = null, Action<IActivityBuilder> branch = null, string id = null) where T : class, IActivity;
IActivityBuilder WithId(string id);
IWorkflowBuilder Then(string activityId);
WorkflowDefinitionVersion Build();
ActivityDefinition BuildActivity();

View file

@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using Elsa.Models;
using Elsa.Services;
using Elsa.Services.Models;
@ -40,6 +41,12 @@ namespace Elsa.WorkflowBuilders
return activityBuilder;
}
public IActivityBuilder WithId(string id)
{
Id = id;
return this;
}
public IWorkflowBuilder Then(string activityId)
{
WorkflowBuilder.Connect(

View file

@ -9,29 +9,36 @@ using Sample17.Models;
namespace Sample17.Activities
{
public class CreateDocument : Activity
public class CreatePerson : Activity
{
private readonly IWorkflowExpressionEvaluator expressionEvaluator;
public CreateDocument(IWorkflowExpressionEvaluator expressionEvaluator)
public CreatePerson(IWorkflowExpressionEvaluator expressionEvaluator)
{
this.expressionEvaluator = expressionEvaluator;
}
public WorkflowExpression<string> TitleExpression
{
get => GetState<WorkflowExpression<string>>();
set => SetState(value);
}
public WorkflowExpression<int> AgeExpression
{
get => GetState<WorkflowExpression<int>>();
set => SetState(value);
}
protected override async Task<ActivityExecutionResult> OnExecuteAsync(
WorkflowExecutionContext context,
CancellationToken cancellationToken)
{
var title = await expressionEvaluator.EvaluateAsync(TitleExpression, context, cancellationToken);
var document = new Document { Title = title};
var name = await expressionEvaluator.EvaluateAsync(TitleExpression, context, cancellationToken);
var age = await expressionEvaluator.EvaluateAsync(AgeExpression, context, cancellationToken);
var person = new Person { FullName = name, Age = age };
Output["Document"] = document;
Output["Person"] = person;
return Done();
}
}

View file

@ -1,27 +0,0 @@
using Elsa.Activities.Console.Activities;
using Elsa.Activities.Primitives;
using Elsa.Expressions;
using Elsa.Services;
using Elsa.Services.Models;
using Sample17.Activities;
using Sample17.Models;
namespace Sample17
{
public class CreateDocumentWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
builder
.StartWith<WriteLine>(x => x.TextExpression = new LiteralExpression("Enter a title"))
.Then<ReadLine>(id: "DocumentTitleInput")
.Then<CreateDocument>(x => x.TitleExpression = new JavaScriptExpression<string>("DocumentTitleInput.Input"), id: "CreateDocument")
.Then<SetVariable>(x =>
{
x.VariableName = "Document";
x.ValueExpression = new JavaScriptExpression<Document>("CreateDocument.Document");
})
.Then<WriteLine>(x => x.TextExpression = new JavaScriptExpression<string>("`A new document was created with title \"${Document.Title}\"`"));
}
}
}

View file

@ -0,0 +1,41 @@
using Elsa.Activities.Console.Activities;
using Elsa.Activities.Primitives;
using Elsa.Expressions;
using Elsa.Services;
using Elsa.Services.Models;
using Sample17.Activities;
using Sample17.Models;
namespace Sample17
{
public class CreatePersonWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
builder
.StartWith<WriteLine>(x => x.TextExpression = new LiteralExpression("Enter name:"))
.Then<ReadLine>().WithId("NameInput")
.Then<WriteLine>(x => x.TextExpression = new LiteralExpression("Enter age:"))
.Then<ReadLine>().WithId("AgeInput")
.Then<CreatePerson>(
x =>
{
x.TitleExpression = new JavaScriptExpression<string>("NameInput.Input");
x.AgeExpression = new JavaScriptExpression<int>("AgeInput.Input");
}).WithId("CreatePerson")
.Then<SetVariable>(
x =>
{
x.VariableName = "Person";
x.ValueExpression = new JavaScriptExpression<Person>("CreatePerson.Person");
})
.Then<SetVariable>(
x =>
{
x.VariableName = "Age";
x.ValueExpression = new JavaScriptExpression<int>("CreatePerson.Person.Age");
})
.Then<WriteLine>(x => x.TextExpression = new JavaScriptExpression<string>("`A new person was created with name \"${Person.FullName}\" and age \"${Age}\"`"));
}
}
}

View file

@ -1,9 +0,0 @@
namespace Sample17.Models
{
public class Document
{
public string Title { get; set; }
public override string ToString() => Title;
}
}

View file

@ -0,0 +1,10 @@
namespace Sample17.Models
{
public class Person
{
public string FullName { get; set; }
public int Age { get; set; }
public override string ToString() => FullName;
}
}

View file

@ -14,8 +14,8 @@ namespace Sample17
{
var services = BuildServices();
var workflowInvoker = services.GetRequiredService<IWorkflowInvoker>();
var workflow = await workflowInvoker.StartAsync<CreateDocumentWorkflow>();
var document = workflow.CurrentScope.GetVariable<Document>("Document");
var workflowExecutionContext = await workflowInvoker.StartAsync<CreatePersonWorkflow>();
var document = workflowExecutionContext.CurrentScope.GetVariable<Person>("Document");
Console.WriteLine("Created document: {0}", document);
}
@ -24,9 +24,10 @@ namespace Sample17
{
return new ServiceCollection()
.AddWorkflows()
.AddWorkflow<CreateDocumentWorkflow>()
.AddWorkflow<CreatePersonWorkflow>()
.AddConsoleActivities()
.AddActivity<CreateDocument>()
.AddActivity<CreatePerson>()
.AddSingleton(Console.In)
.BuildServiceProvider();
}
}