mirror of
https://github.com/ditkrg/DIT.Workflower.git
synced 2026-01-22 22:06:42 +00:00
Improvements
This commit is contained in:
parent
98e02edb9c
commit
8d9b7d4134
12
README.md
12
README.md
@ -19,7 +19,6 @@ Install the latest nuget package into your ASP.NET Core application.
|
||||
```
|
||||
|
||||
### Workflow Builder
|
||||
In the `Program.cs`, register the Swagger generator, defining one or more Swagger documents.
|
||||
|
||||
```csharp
|
||||
using DIT.Workflower;
|
||||
@ -50,11 +49,13 @@ var allowedTransitions = workflow.GetAllowedTransitions(from: TState.State_2);
|
||||
|
||||
### Dependency Injection
|
||||
|
||||
In the `Program.cs`, register the workflow factory, defining one or more workflow definitions.
|
||||
|
||||
|
||||
```csharp
|
||||
public record PhoneCall(bool Active);
|
||||
|
||||
services.AddWorkflowDefinition<PhoneState, PhoneCommand, PhoneCall>(version: 1)
|
||||
services.AddWorkflowDefinition<PhoneState, PhoneCommand, PhoneCall>(id: "constant-id", version: 1)
|
||||
// Idle -> Ringing (Incoming)
|
||||
.From(PhoneState.Idle)
|
||||
.On(PhoneCommand.IncomingCall)
|
||||
@ -80,7 +81,7 @@ public class SampleController : JsonApiControllerBase
|
||||
|
||||
public SampleController(IWorkflowFactory<PhoneState, PhoneCommand, PhoneCall> factory)
|
||||
{
|
||||
_workflow = factory.CreateWorkflow(version: 1); // Optional version param to support versioning on workflows.
|
||||
_workflow = factory.CreateWorkflow(id: "constant-id", version: 1); // Optional version param to support versioning on workflows.
|
||||
}
|
||||
|
||||
[HttpGet("{state}")]
|
||||
@ -93,3 +94,8 @@ public class SampleController : JsonApiControllerBase
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Note: If you do not specify an id for the workflow, the default id is:
|
||||
```csharp
|
||||
$"{typeof(TState).Name}_{typeof(TCommand).Name}_{typeof(TContext).Name}";
|
||||
```
|
||||
|
||||
@ -4,9 +4,8 @@ public interface IWorkflowFactory<TState, TCommand, TContext>
|
||||
where TState : struct
|
||||
where TCommand : struct
|
||||
{
|
||||
public IWorkflow<TState, TCommand, TContext> CreateWorkflow(int version = 1);
|
||||
|
||||
public IWorkflow<TState, TCommand, TContext> CreateWorkflow(string id);
|
||||
|
||||
public IWorkflow<TState, TCommand, TContext> CreateWorkflow(string id, int version);
|
||||
public IWorkflow<TState, TCommand, TContext> CreateWorkflow(string id, int version = 1);
|
||||
|
||||
}
|
||||
|
||||
@ -12,10 +12,13 @@ public class DefaultWorkflowFactory<TState, TCommand, TContext> : IWorkflowFacto
|
||||
_serviceProvider = sp;
|
||||
}
|
||||
|
||||
public IWorkflow<TState, TCommand, TContext> CreateWorkflow(string id)
|
||||
=> CreateWorkflow(id, version: 1);
|
||||
public IWorkflow<TState, TCommand, TContext> CreateWorkflow(int version = 1)
|
||||
{
|
||||
var id = WorkflowDefinitionWrapper<TState, TCommand, TContext>.GetDefaultId();
|
||||
return CreateWorkflow(id, version);
|
||||
}
|
||||
|
||||
public IWorkflow<TState, TCommand, TContext> CreateWorkflow(string id, int version)
|
||||
public IWorkflow<TState, TCommand, TContext> CreateWorkflow(string id, int version = 1)
|
||||
{
|
||||
var reference = $"{id}.v{version}";
|
||||
var service = _serviceProvider.GetServices<IWorkflow<TState, TCommand, TContext>>()
|
||||
|
||||
@ -5,7 +5,15 @@ namespace DIT.Workflower.DependencyInjection.Extensions;
|
||||
public static class IServiceCollectionExtensions
|
||||
{
|
||||
|
||||
public static ITransitionStart<TState, TCommand, TContext> AddWorkflowDefinition<TState, TCommand, TContext>(this IServiceCollection services, string id)
|
||||
public static ITransitionStart<TState, TCommand, TContext> AddWorkflowDefinition<TState, TCommand, TContext>(this IServiceCollection services, in int version = 1)
|
||||
where TState : struct
|
||||
where TCommand : struct
|
||||
{
|
||||
var id = WorkflowDefinitionWrapper<TState, TCommand, TContext>.GetDefaultId();
|
||||
return AddWorkflowDefinition<TState, TCommand, TContext>(services, id, version);
|
||||
}
|
||||
|
||||
public static ITransitionStart<TState, TCommand, TContext> AddWorkflowDefinition<TState, TCommand, TContext>(this IServiceCollection services, in string id)
|
||||
where TState : struct
|
||||
where TCommand : struct
|
||||
{
|
||||
|
||||
@ -16,4 +16,9 @@ public record WorkflowDefinitionWrapper<TState, TCommand, TContext> : WorkflowDe
|
||||
Version = version;
|
||||
}
|
||||
|
||||
public static string GetDefaultId()
|
||||
{
|
||||
return $"{typeof(TState).Name}_{typeof(TCommand).Name}_{typeof(TContext).Name}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ public class DependencyInjectionTests
|
||||
|
||||
var sp = sc.BuildServiceProvider();
|
||||
|
||||
var workflowFactory = sp.GetService<IWorkflowFactory<PhoneState, PhoneCommand, PhoneCall>>()!;
|
||||
var workflowFactory = sp.GetRequiredService<IWorkflowFactory<PhoneState, PhoneCommand, PhoneCall>>();
|
||||
|
||||
|
||||
var v1 = workflowFactory.CreateWorkflow(id);
|
||||
@ -58,4 +58,21 @@ public class DependencyInjectionTests
|
||||
Assert.Equal(2, v2.GetAllowedTransitions(PhoneState.Ringing).Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IdGenerationTest()
|
||||
{
|
||||
var sc = new ServiceCollection();
|
||||
|
||||
sc.AddWorkflowDefinition<PhoneState, PhoneCommand, PhoneCall>(version: 1)
|
||||
.From(PhoneState.Idle)
|
||||
.On(PhoneCommand.IncomingCall)
|
||||
.To(PhoneState.Ringing);
|
||||
|
||||
var sp = sc.BuildServiceProvider();
|
||||
var workflowFactory = sp.GetRequiredService<IWorkflowFactory<PhoneState, PhoneCommand, PhoneCall>>();
|
||||
var workflow = workflowFactory.CreateWorkflow();
|
||||
|
||||
Assert.Equal("PhoneState_PhoneCommand_PhoneCall", workflow.Id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user