Skip to content

Commit

Permalink
Optimize naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Anson.G committed Jul 24, 2024
1 parent 66644bf commit cee03ee
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
29 changes: 14 additions & 15 deletions CoRProcessor/CoRProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace CoRProcessor
{
public delegate Task ActionDelegate<T>(T arg, CancellationToken cancellationToken) where T : IChainContext;
public delegate Task ActionDelegate<T>(T ctx, CancellationToken cancelToken) where T : IChainContext;

public delegate Task<T> FuncDelegate<T>(T arg, CancellationToken cancellationToken)
public delegate Task<T> FuncDelegate<T>(T ctx, CancellationToken cancelToken)
where T : IChainContext;

public delegate Task<bool> OnExceptionDelegate<T>(T arg, Exception e, CancellationToken cancellationToken)
public delegate Task<bool> OnExceptionDelegate<T>(T ctx, Exception e, CancellationToken cancelToken)
where T : IChainContext;

public class CoRProcessor<T> where T : IChainContext
Expand Down Expand Up @@ -39,34 +39,33 @@ public CoRProcessor<T> AddRange(IEnumerable<IChainProcessor<T>> processors)
return this;
}

public async Task<T> Execute(T t, CancellationToken token = default)
public async Task<T> Execute(T ctx, CancellationToken cancelToken = default)
{
var context = t;
try
{
if (_beforeAction != null) await _beforeAction.Invoke(t, token);
if (_beforeAction != null) await _beforeAction.Invoke(ctx, cancelToken);

foreach (var chainProcessor in _chainProcessors)
{
if (context.Abort) break;
if (ctx.Abort) break;
if (chainProcessor.CompensateOnFailure != null) _delegates.Add(chainProcessor.CompensateOnFailure);
context = await chainProcessor.Handle(context, token);
ctx = await chainProcessor.Handle(ctx, cancelToken);
}

if (_afterAction == null) return context;
if (_afterAction == null) return ctx;

await _afterAction.Invoke(context, token);
await _afterAction.Invoke(ctx, cancelToken);

return context;
return ctx;
}
catch (Exception e)
{
foreach (var funcDelegate in _delegates) await funcDelegate.Invoke(context, token);
foreach (var funcDelegate in _delegates) await funcDelegate.Invoke(ctx, cancelToken);

if (_onException != null)
{
var isThrow = await _onException.Invoke(context, e, token);
if (!isThrow) return context;
var isThrow = await _onException.Invoke(ctx, e, cancelToken);
if (!isThrow) return ctx;
}

ExceptionDispatchInfo.Capture(e).Throw();
Expand All @@ -75,7 +74,7 @@ public async Task<T> Execute(T t, CancellationToken token = default)
finally
{
if (_finallyAction != null)
await _finallyAction.Invoke(context, token);
await _finallyAction.Invoke(ctx, cancelToken);
}
}

Expand Down
4 changes: 1 addition & 3 deletions CoRProcessor/EmptyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ namespace CoRProcessor
{
public class EmptyProcessor<T> : IChainProcessor<T> where T : IChainContext
{
public IChainProcessor<T> Next { get; set; } = default;

public Task<T> Handle(T t, CancellationToken token = default)
{
return Task.FromResult(t);
}

public FuncDelegate<T> CompensateOnFailure { get; set; } = (arg, token) => Task.FromResult(arg);
public FuncDelegate<T> CompensateOnFailure { get; set; } = (ctx, token) => Task.FromResult(ctx);
}
}

2 changes: 1 addition & 1 deletion CoRProcessor/IChainProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace CoRProcessor
{
public interface IChainProcessor<T> where T : IChainContext
{
Task<T> Handle(T t, CancellationToken cancellationToken = default);
Task<T> Handle(T ctx, CancellationToken cancelToken = default);
FuncDelegate<T> CompensateOnFailure { get; set; }
}
}
2 changes: 0 additions & 2 deletions UnitTests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ public async Task TestCorProcessorRunningOnAutofac()
public async Task TestEmptyProcessorRunning()
{
var emptyProcessor = new EmptyProcessor<NumberContext>();
emptyProcessor.Next = new AdditionProcessor();

var result = await emptyProcessor.Handle(new NumberContext()
{
Expand All @@ -232,7 +231,6 @@ public async Task TestEmptyProcessorRunning()
}, default);

Assert.That(result.Result, Is.EqualTo(0));
Assert.That(emptyProcessor.Next.GetType().FullName, Is.EqualTo(typeof(AdditionProcessor).FullName));
}

[Test]
Expand Down

0 comments on commit cee03ee

Please sign in to comment.