| | | 1 | | using Elsa.Workflows.Activities; |
| | | 2 | | using Elsa.Workflows.Management.Entities; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Management.Materializers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A workflow materializer that deserializes workflows created in C# code. |
| | | 10 | | /// </summary> |
| | | 11 | | public class ClrWorkflowMaterializer( |
| | | 12 | | IPayloadSerializer payloadSerializer, |
| | | 13 | | IWorkflowBuilderFactory workflowBuilderFactory, |
| | | 14 | | IServiceProvider serviceProvider) : IWorkflowMaterializer |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// The name of the materializer. |
| | | 18 | | /// </summary> |
| | | 19 | | public const string MaterializerName = "CLR"; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public string Name => MaterializerName; |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public async ValueTask<Workflow> MaterializeAsync(WorkflowDefinition definition, CancellationToken cancellationToken |
| | | 26 | | { |
| | | 27 | | var providerContext = payloadSerializer.Deserialize<ClrWorkflowMaterializerContext>(definition.MaterializerConte |
| | | 28 | | var workflowBuilderType = providerContext.WorkflowBuilderType == null! ? typeof(NotFoundWorkflowBuilder) : provi |
| | | 29 | | var workflowBuilder = (IWorkflow)ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, workflowBuilderT |
| | | 30 | | var workflowDefinitionBuilder = workflowBuilderFactory.CreateBuilder(); |
| | | 31 | | var workflow = await workflowDefinitionBuilder.BuildWorkflowAsync(workflowBuilder, cancellationToken); |
| | | 32 | | |
| | | 33 | | // Assign identities from the definition. |
| | | 34 | | workflow.Identity = new WorkflowIdentity(definition.DefinitionId, definition.Version, definition.Id, definition. |
| | | 35 | | |
| | | 36 | | return workflow; |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Provides context for the CLR workflow materializer. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="WorkflowBuilderType">The type of the workflow builder.</param> |
| | 2246 | 44 | | public record ClrWorkflowMaterializerContext(Type WorkflowBuilderType); |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// A workflow builder that is used when the workflow builder type is not found. |
| | | 48 | | /// </summary> |
| | | 49 | | public class NotFoundWorkflowBuilder : WorkflowBase |
| | | 50 | | { |
| | | 51 | | } |