Skip to content

Commit 7a8b01f

Browse files
authored
Merge pull request danielgerlag#780 from DevsAnon/master
Switched WorkflowRegistry to use ValueTuples to improve code readability
2 parents 200f16f + 141f704 commit 7a8b01f

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/WorkflowCore/Services/WorkflowRegistry.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace WorkflowCore.Services
1111
public class WorkflowRegistry : IWorkflowRegistry
1212
{
1313
private readonly IServiceProvider _serviceProvider;
14-
private readonly BlockingCollection<Tuple<string, int, WorkflowDefinition>> _registry = new BlockingCollection<Tuple<string, int, WorkflowDefinition>>();
14+
private readonly BlockingCollection<(string workflowId, int version, WorkflowDefinition definition)> _registry = new BlockingCollection<(string, int, WorkflowDefinition)>();
1515

1616
public WorkflowRegistry(IServiceProvider serviceProvider)
1717
{
@@ -20,75 +20,75 @@ public WorkflowRegistry(IServiceProvider serviceProvider)
2020

2121
public WorkflowDefinition GetDefinition(string workflowId, int? version = null)
2222
{
23+
(string workflowId, int version, WorkflowDefinition definition) workflowEntry;
2324
if (version.HasValue)
2425
{
25-
var entry = _registry.FirstOrDefault(x => x.Item1 == workflowId && x.Item2 == version.Value);
26-
// TODO: What in the heck does Item3 mean?
27-
return entry?.Item3;
26+
workflowEntry = _registry.FirstOrDefault(x => x.workflowId == workflowId && x.version == version.Value);
2827
}
2928
else
3029
{
31-
var entry = _registry.Where(x => x.Item1 == workflowId).OrderByDescending(x => x.Item2)
32-
.FirstOrDefault();
33-
return entry?.Item3;
30+
workflowEntry = _registry.Where(x => x.workflowId == workflowId).OrderByDescending(x => x.version)
31+
.FirstOrDefault();
3432
}
33+
34+
return workflowEntry != default ? workflowEntry.definition : default;
3535
}
3636

3737
public void DeregisterWorkflow(string workflowId, int version)
3838
{
39-
var definition = _registry.FirstOrDefault(x => x.Item1 == workflowId && x.Item2 == version);
40-
if (definition != null)
39+
var definition = _registry.FirstOrDefault(x => x.workflowId == workflowId && x.version == version);
40+
if (definition != default)
4141
{
4242
_registry.TryTake(out definition);
4343
}
4444
}
4545

4646
public void RegisterWorkflow(IWorkflow workflow)
4747
{
48-
if (_registry.Any(x => x.Item1 == workflow.Id && x.Item2 == workflow.Version))
48+
if (_registry.Any(x => x.workflowId == workflow.Id && x.version == workflow.Version))
4949
{
5050
throw new InvalidOperationException($"Workflow {workflow.Id} version {workflow.Version} is already registered");
5151
}
5252

53-
var builder = _serviceProvider.GetService<IWorkflowBuilder>().UseData<object>();
53+
var builder = _serviceProvider.GetService<IWorkflowBuilder>().UseData<object>();
5454
workflow.Build(builder);
5555
var def = builder.Build(workflow.Id, workflow.Version);
56-
_registry.Add(Tuple.Create(workflow.Id, workflow.Version, def));
56+
_registry.Add((workflow.Id, workflow.Version, def));
5757
}
5858

5959
public void RegisterWorkflow(WorkflowDefinition definition)
6060
{
61-
if (_registry.Any(x => x.Item1 == definition.Id && x.Item2 == definition.Version))
61+
if (_registry.Any(x => x.workflowId == definition.Id && x.version == definition.Version))
6262
{
6363
throw new InvalidOperationException($"Workflow {definition.Id} version {definition.Version} is already registered");
6464
}
6565

66-
_registry.Add(Tuple.Create(definition.Id, definition.Version, definition));
66+
_registry.Add((definition.Id, definition.Version, definition));
6767
}
6868

6969
public void RegisterWorkflow<TData>(IWorkflow<TData> workflow)
7070
where TData : new()
7171
{
72-
if (_registry.Any(x => x.Item1 == workflow.Id && x.Item2 == workflow.Version))
72+
if (_registry.Any(x => x.workflowId == workflow.Id && x.version == workflow.Version))
7373
{
7474
throw new InvalidOperationException($"Workflow {workflow.Id} version {workflow.Version} is already registered");
7575
}
7676

7777
var builder = _serviceProvider.GetService<IWorkflowBuilder>().UseData<TData>();
7878
workflow.Build(builder);
7979
var def = builder.Build(workflow.Id, workflow.Version);
80-
_registry.Add(Tuple.Create(workflow.Id, workflow.Version, def));
80+
_registry.Add((workflow.Id, workflow.Version, def));
8181
}
8282

8383
public bool IsRegistered(string workflowId, int version)
8484
{
85-
var definition = _registry.FirstOrDefault(x => x.Item1 == workflowId && x.Item2 == version);
86-
return (definition != null);
85+
var definition = _registry.FirstOrDefault(x => x.workflowId == workflowId && x.version == version);
86+
return definition != default;
8787
}
8888

8989
public IEnumerable<WorkflowDefinition> GetAllDefinitions()
9090
{
91-
return _registry.Select(i => i.Item3);
91+
return _registry.Select(i => i.definition);
9292
}
9393
}
94-
}
94+
}

0 commit comments

Comments
 (0)