Skip to content

Commit

Permalink
Specify Execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Anson.G committed Jul 24, 2024
1 parent cee03ee commit e8b7a14
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 5 deletions.
12 changes: 7 additions & 5 deletions CoRProcessor/CoRProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -39,17 +40,18 @@ public CoRProcessor<T> AddRange(IEnumerable<IChainProcessor<T>> processors)
return this;
}

public async Task<T> Execute(T ctx, CancellationToken cancelToken = default)
public async Task<T> Execute(T ctx, CancellationToken cancelToken = default, params int[] steps)
{
try
{
if (_beforeAction != null) await _beforeAction.Invoke(ctx, cancelToken);

foreach (var chainProcessor in _chainProcessors)
for (var i = 0; i < _chainProcessors.Count; i++)
{
if (steps.Length > 0 && !steps.Contains(i)) continue;
if (ctx.Abort) break;
if (chainProcessor.CompensateOnFailure != null) _delegates.Add(chainProcessor.CompensateOnFailure);
ctx = await chainProcessor.Handle(ctx, cancelToken);
if (_chainProcessors[i].CompensateOnFailure != null) _delegates.Add(_chainProcessors[i].CompensateOnFailure);
ctx = await _chainProcessors[i].Handle(ctx, cancelToken);
}

if (_afterAction == null) return ctx;
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ public class SampleProcessor : IChainProcessor<MyData>

```

### Specify Execution ⚙️
You can specify the order of execution of processors in the chain.
```csharp
var result = await CoRProcessor<NumberContext>
.New()
.AddRange([
new AdditionProcessor(),
new AdditionProcessor(),
new AdditionProcessor(),
new AdditionProcessor(),
])
.Execute(new NumberContext()
{
Number1 = 1,
Number2 = 1,
Operation = Operation.Addition
}, default, 2, 3); // The processor specified with index 2 and 3 will execute, while the others will not execute
Console.WriteLine(result.Data);
```

### Create and Execute the Processor Chain 🏗️
You can create and execute a processor chain using the `CoRProcessor<T>` class. Here's how to do it:
```csharp
Expand Down
21 changes: 21 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ public class SampleProcessor : IChainProcessor<MyData>

```

### 指定执行的处理器 ⚙️
您可以指定执行某几个处理器:
```csharp
var result = await CoRProcessor<NumberContext>
.New()
.AddRange([
new AdditionProcessor(),
new AdditionProcessor(),
new AdditionProcessor(),
new AdditionProcessor(),
])
.Execute(new NumberContext()
{
Number1 = 1,
Number2 = 1,
Operation = Operation.Addition
}, default, 3, 4); // 指定了 index 为 2, 3 的处理器将会执行, 其余则不会执行
Console.WriteLine(result.Data);
```

### 创建和执行处理器链 🏗️
您可以使用 `CoRProcessor<T>` 类创建和执行处理器链。如下所示:
```csharp
Expand Down
65 changes: 65 additions & 0 deletions UnitTests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,69 @@ public async Task TestCompensateOnFailure()

Assert.That(result.Result, Is.EqualTo(9));
}

[Test]
public async Task TestNotImplementationCompensateOnFailure()
{
var result = await CoRProcessor<NumberContext>
.New()
.AddRange([
new SubtractionProcessor(),
new ExceptionProcessor(),
new SubtractionProcessor(),
])
.OnException(async (t, e, token) => await Task.FromResult(false))
.Execute(new NumberContext()
{
Number1 = 1,
Number2 = 1,
Operation = Operation.Subtraction
}, default);

Assert.That(result.Result, Is.EqualTo(0));
}

[Test]
public async Task TestSpecifyExecution()
{
var result = await CoRProcessor<NumberContext>
.New()
.AddRange([
new AdditionProcessor(),
new AdditionProcessor(),
new AdditionProcessor(),
new AdditionProcessor(),
])
.Execute(new NumberContext()
{
Number1 = 1,
Number2 = 1,
Operation = Operation.Addition
}, default, 3);


Assert.That(result.Result, Is.EqualTo(2));
}

[Test]
public async Task TestNotSpecifyExecution()
{
var result = await CoRProcessor<NumberContext>
.New()
.AddRange([
new AdditionProcessor(),
new AdditionProcessor(),
new AdditionProcessor(),
new AdditionProcessor(),
])
.Execute(new NumberContext()
{
Number1 = 1,
Number2 = 1,
Operation = Operation.Addition
}, default);


Assert.That(result.Result, Is.EqualTo(8));
}
}

0 comments on commit e8b7a14

Please sign in to comment.