-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加第二种Pipeline的实现方式
- Loading branch information
Showing
17 changed files
with
259 additions
and
44 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
src/framework/Luck.Framework/PipelineAbstract/DelegatePipe.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Luck.Framework.PipelineAbstract; | ||
|
||
public delegate ValueTask DelegatePipe<in TContext>(TContext context) where TContext : IContext; |
6 changes: 6 additions & 0 deletions
6
src/framework/Luck.Framework/PipelineAbstract/IDelegatePipe.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Luck.Framework.PipelineAbstract; | ||
|
||
public interface IDelegatePipe<TContext> where TContext : IContext | ||
{ | ||
ValueTask InvokeAsync(TContext context, DelegatePipe<TContext> next); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/framework/Luck.Framework/PipelineAbstract/IDelegatePipelineBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Luck.Framework.PipelineAbstract; | ||
|
||
public interface IDelegatePipelineBuilder<TContext> where TContext : IContext | ||
{ | ||
// IDelegatePipelineBuilder<TContext> UseMiddleware<TMiddleware>(IDelegatePipe<TContext> delegatePipe) where TMiddleware; | ||
|
||
IDelegatePipelineBuilder<TContext> UsePipe(IDelegatePipe<TContext> delegatePipe); | ||
DelegatePipe<TContext> Build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...k.Framework/PipelineAbstract/IActuator.cs → ...amework/PipelineAbstract/IPipeActuator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace Luck.Framework.PipelineAbstract; | ||
|
||
public interface IActuator<in TContext> where TContext : IContext | ||
public interface IPipeActuator<in TContext> where TContext : IContext | ||
{ | ||
ValueTask InvokeAsync(TContext context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/framework/Luck.Framework/PipelineAbstract/PipelineDelegate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Luck.Framework.PipelineAbstract; | ||
|
||
public delegate DelegatePipe<TContext> PipelineDelegate<TContext>(DelegatePipe<TContext> next) | ||
where TContext : IContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Luck.Framework.PipelineAbstract; | ||
|
||
namespace Luck.Pipeline; | ||
|
||
public abstract class DefaultDelegatePipe<TContext> : IDelegatePipe<TContext> where TContext : IContext | ||
{ | ||
public async ValueTask InvokeAsync(TContext context, DelegatePipe<TContext> next) | ||
{ | ||
try | ||
{ | ||
var runThis = await BeforeInvokeAsync(context); | ||
if (context.IsInterruptible) | ||
{ | ||
await OnCancelled(context); | ||
return; | ||
} | ||
|
||
if (runThis) | ||
{ | ||
await Invoke(context); | ||
} | ||
|
||
if (context.IsInterruptible) | ||
{ | ||
await OnCancelled(context); | ||
return; | ||
} | ||
|
||
await next(context); | ||
} | ||
catch (Exception ex) | ||
{ | ||
await OnCancelled(context, ex); | ||
} | ||
} | ||
|
||
protected abstract ValueTask Invoke(TContext context); | ||
|
||
protected virtual ValueTask<bool> BeforeInvokeAsync(TContext context) => ValueTask.FromResult(true); | ||
|
||
protected virtual ValueTask AfterInvokeAsync(TContext context) => ValueTask.CompletedTask; | ||
|
||
protected virtual ValueTask OnCancelled(TContext context, Exception ex = null!) | ||
{ | ||
if (ex != null) | ||
{ | ||
throw new Exception(this.GetType().Name + "->" + ex.Message, ex); | ||
} | ||
|
||
return ValueTask.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Luck.Framework.PipelineAbstract; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Luck.Pipeline; | ||
|
||
public class DelegatePipelineBuilder<TContext> : IDelegatePipelineBuilder<TContext> where TContext : IContext | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private IList<PipelineDelegate<TContext>> _pipes = new List<PipelineDelegate<TContext>>(); | ||
|
||
public DelegatePipelineBuilder(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public IDelegatePipelineBuilder<TContext> UsePipe(IDelegatePipe<TContext> delegatePipe) | ||
{ | ||
DelegatePipe<TContext> PipelineDelegate(DelegatePipe<TContext> next) => | ||
context => delegatePipe.InvokeAsync(context, next); | ||
|
||
_pipes.Add(PipelineDelegate); | ||
return this; | ||
} | ||
|
||
public DelegatePipe<TContext> Build() | ||
{ | ||
DelegatePipe<TContext> next = _ => ValueTask.CompletedTask; | ||
|
||
for (var i = _pipes.Count - 1; i >= 0; i--) | ||
{ | ||
next = _pipes[i].Invoke(next); | ||
} | ||
|
||
return next; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
test/Luck.UnitTest/Pipeline_Tests/FetchOrderDetailDelegatePipePipe.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Luck.Framework.PipelineAbstract; | ||
using Luck.Pipeline; | ||
using Luck.UnitTest.Pipeline_Tests.Context; | ||
|
||
namespace Luck.UnitTest.Pipeline_Tests; | ||
|
||
public class CreateCustomerDelegatePipe : DefaultDelegatePipe<CustomerContext> | ||
{ | ||
protected override ValueTask Invoke(CustomerContext context) | ||
{ | ||
if (context.Properties.TryGetValue("测试数据", out var value)) | ||
{ | ||
Console.WriteLine($"测试输出信息:【{value}】"); | ||
} | ||
|
||
; | ||
return new ValueTask(); | ||
} | ||
} | ||
|
||
public class FetchOrderDetailDelegatePipePipe : DefaultDelegatePipe<CustomerContext> | ||
{ | ||
protected override ValueTask Invoke(CustomerContext context) | ||
{ | ||
Console.WriteLine($"添加测试数据"); | ||
context.Properties.Add("测试数据", "测试订单列表"); | ||
return new ValueTask(); | ||
} | ||
} | ||
|
||
public class CancelDelegatePipe : DefaultDelegatePipe<CustomerContext> | ||
{ | ||
protected override ValueTask Invoke(CustomerContext context) | ||
{ | ||
context.SetInterruptible(Interruptible.Cancel); | ||
return new ValueTask(); | ||
} | ||
} |
Oops, something went wrong.