美文网首页
手写简单责任链框架

手写简单责任链框架

作者: 杰克_王_ | 来源:发表于2020-02-16 12:42 被阅读0次
    public interface IStepContext
    {
    }
    
    public class StepContext : IStepContext
    {
    }
    
    using System.Threading.Tasks;
    
    public delegate Task StepDelegate(IStepContext context);
    
    using System.Threading.Tasks;
    
    public interface IPipelineBuilder
    {
        IPipelineBuilder Use(Func<IStepContext, Func<Task>, Task> step);
        IPipelineBuilder Use(Func<StepDelegate, StepDelegate> step);
        StepDelegate Build();
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    public class PipelineBuilder : IPipelineBuilder
    {
        private readonly IList<Func<StepDelegate, StepDelegate>> _components = null;
        private bool _builded = false;
    
        public PipelineBuilder()
        {
            this._components = new List<Func<StepDelegate, StepDelegate>>();
        }
    
        public StepDelegate Build()
        {
            if (_builded) throw new InvalidOperationException("Build can only once");
            _builded = true;
    
            StepDelegate step = context => 
            {
                Console.WriteLine($"Warning: you didn't provide an endpoint on {DateTime.Now:yyyy/MM/dd HH:mm:ss.fff}"); ;
                return Task.CompletedTask;
            };
    
            foreach (var component in _components.Reverse())
            {
                step = component(step);
            }
    
            _components.Clear();
    
            return step;
        }
    
        public IPipelineBuilder Use(Func<IStepContext, Func<Task>, Task> step)
        {
            return Use(next =>
            {
                return context =>
                {
                    Task nextTask() => next(context); // Task nextTask() { return next(context); };
                    return step(context, nextTask);
                };
            });
        }
    
        public IPipelineBuilder Use(Func<StepDelegate, StepDelegate> step)
        {
            this._components.Add(step);
            return this;
        }
    }
    
    using System;
    using System.Threading.Tasks;
    
    class Program
    {
        static void Main(string[] args)
        {
            IPipelineBuilder pipelineBuilder = new PipelineBuilder();
            IStepContext stepContext = new StepContext();
    
            pipelineBuilder.Use(async (context, next) => 
            {
                Console.WriteLine("第一步开始");
                await next();
                Console.WriteLine("第一步结束");
            });
    
            pipelineBuilder.Use(async (context, next) =>
            {
                Console.WriteLine("第二步开始");
                await next();
                Console.WriteLine("第二步结束");
            }).Use(async (context, next) =>
            {
                Console.WriteLine("第三步开始");
                await next();
                Console.WriteLine("第三步结束");
            });
            
            pipelineBuilder.Use((context, next) => 
            {
                Console.WriteLine("第四步终结点");
                return Task.CompletedTask;
            });
    
            var pipeline = pipelineBuilder.Build();
            pipeline(stepContext).Wait();
        }
    }
    

    相关文章

      网友评论

          本文标题:手写简单责任链框架

          本文链接:https://www.haomeiwen.com/subject/bdypfhtx.html