Skip to content

Instantly share code, notes, and snippets.

@promontis
Created August 18, 2023 18:16
Show Gist options
  • Save promontis/de10f538311d734387db239ef523e44f to your computer and use it in GitHub Desktop.
Save promontis/de10f538311d734387db239ef523e44f to your computer and use it in GitHub Desktop.
public class WorkflowInstancePreviewer : IWorkflowInstancePreviewer
{
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IWorkflowInstanceStore _workflowInstanceStore;
private readonly IWorkflowRegistry _workflowRegistry;
private readonly ICreatesActivityExecutionContextForActivityBlueprint _activityExecutionContextFactory;
public WorkflowInstancePreviewer(IServiceScopeFactory serviceScopeFactory, IWorkflowInstanceStore workflowInstanceStore, IWorkflowRegistry workflowRegistry, ICreatesActivityExecutionContextForActivityBlueprint activityExecutionContextFactory)
{
_serviceScopeFactory = serviceScopeFactory;
_workflowInstanceStore = workflowInstanceStore;
_workflowRegistry = workflowRegistry;
_activityExecutionContextFactory = activityExecutionContextFactory;
}
public async Task<WorkflowInstance> Get(WorkflowInstance workflowInstance, IWorkflowBlueprint workflowBlueprint)
{
var previewActivityData = new Dictionary<string, IDictionary<string, object?>>();
using var scope = _serviceScopeFactory.CreateAsyncScope();
var workflowExecutionContext = new WorkflowExecutionContext(scope.ServiceProvider, workflowBlueprint, workflowInstance);
foreach (var workflowActivity in workflowBlueprint.Activities)
{
var currentActivityData = new Dictionary<string, object?>();
previewActivityData.Add(workflowActivity.Id, currentActivityData);
var activityExecutionContext = _activityExecutionContextFactory.CreateActivityExecutionContext(workflowActivity, workflowExecutionContext, CancellationToken.None);
var activityPropertyProviders = workflowBlueprint.ActivityPropertyProviders.GetProviders(workflowActivity.Id);
if (activityPropertyProviders == null) continue;
foreach (var activityPropertyProvider in activityPropertyProviders)
{
var value = await activityPropertyProvider.Value.GetValueAsync(activityExecutionContext);
currentActivityData.Add(activityPropertyProvider.Key, value);
}
}
workflowInstance.ActivityData = previewActivityData;
return workflowInstance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment