Improvements

This commit is contained in:
Shkar T. Noori 2022-06-18 11:22:12 +03:00
parent 98e02edb9c
commit 8d9b7d4134
No known key found for this signature in database
GPG Key ID: C5E1A00F3BB78732
6 changed files with 49 additions and 11 deletions

View File

@ -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}";
```

View File

@ -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);
}

View File

@ -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>>()

View File

@ -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
{

View File

@ -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}";
}
}

View File

@ -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);
}
}