1 namespace ConsoleApp1.Delegate.Day3 2 { 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 /// <summary> 10 /// 比如有一个请求. 11 /// </summary> 12 public class Request 13 { 14 public string Name { get; set; } 15 16 public string Password { get; set; } 17 18 public string Url { get; set; } 19 } 20 21 /// <summary> 22 /// 中间件接口. 23 /// </summary> 24 public interface IBeforeMiddleware 25 { 26 Func<Request, bool> BuildAction(Func<Request, bool> action); 27 } 28 29 /// <summary> 30 /// 验证Url的中间的. 31 /// </summary> 32 public class ValidateUrlMiddleware : IBeforeMiddleware 33 { 34 public Func<Request, bool> BuildAction(Func<Request, bool> func) 35 { 36 return new Func<Request, bool>(request => 37 { 38 if (request.Url == null) 39 { 40 Console.WriteLine("url 为空"); 41 return false; 42 } 43 return func.Invoke(request); 44 }); 45 } 46 } 47 48 /// <summary> 49 /// 验证Name的中间的. 50 /// </summary> 51 public class ValidateNameMiddleware : IBeforeMiddleware 52 { 53 public Func<Request, bool> BuildAction(Func<Request, bool> func) 54 { 55 return new Func<Request, bool>(request => 56 { 57 if (request.Name == null) 58 { 59 Console.WriteLine("name 为空"); 60 return false; 61 } 62 return func.Invoke(request); 63 }); 64 } 65 } 66 67 /// <summary> 68 /// 验证password的中间的. 69 /// </summary> 70 public class ValidatePasswordMiddleware : IBeforeMiddleware 71 { 72 public Func<Request, bool> BuildAction(Func<Request, bool> func) 73 { 74 return new Func<Request, bool>(request => 75 { 76 if (request.Password == null) 77 { 78 Console.WriteLine("password 为空"); 79 return false; 80 } 81 return func.Invoke(request); 82 }); 83 } 84 } 85 86 /// <summary> 87 /// 然后这里有一个接口. 88 /// </summary> 89 public class PiplineController 90 { 91 /// <summary> 92 /// 中间件集合. 93 /// 94 /// 这里可以根据不同需求改变顺序 95 /// 如果新增其他验证规则,新建一个继承这个接口的类,然后加入这个集合就可以 96 /// </summary> 97 private readonly List<IBeforeMiddleware> _middlewares = new List<IBeforeMiddleware>() 98 { 99 new ValidateUrlMiddleware(), 100 new ValidateNameMiddleware(), 101 new ValidatePasswordMiddleware(), 102 }; 103 104 /// <summary> 105 /// 验证接口. 106 /// </summary> 107 /// <param name="request"></param> 108 /// <returns></returns> 109 public bool Validate(Request request) 110 { 111 try 112 { 113 // 真正要做的事情. 114 Func<Request, bool> func = new Func<Request, bool>(req => 115 { 116 Console.WriteLine(req.Name); 117 Console.WriteLine(req.Url); 118 Console.WriteLine(req.Password); 119 Console.WriteLine("比如存个数据库什么的"); 120 return true; 121 }); 122 // 真正执行的时候是按照注册倒叙进行 123 // 对这一系列操作重新构建委托. 124 for (int i = _middlewares.Count - 1; i >= 0; i--) 125 { 126 func = _middlewares[i].BuildAction(func); 127 } 128 // 最后执行 129 return func.Invoke(request); 130 } 131 catch (Exception e) 132 { 133 Console.WriteLine(e); 134 return false; 135 } 136 } 137 } 138 }
这个124行为什么是倒叙:
1 private void Why() 2 { 3 var func = new Func<Request, bool>(request => 4 { 5 // 真正要做的事情. 6 Console.WriteLine(request.Name); 7 Console.WriteLine(request.Url); 8 Console.WriteLine(request.Password); 9 Console.WriteLine("比如存个数据库什么的"); 10 return true; 11 }); 12 // 比如现在是正序. 13 // 按顺序委托中的代码分别是 14 //if (request.Url == null) 15 //{ 16 // Console.WriteLine("url 为空"); 17 // return false; 18 //} 19 //if (request.Name == null) 20 //{ 21 // Console.WriteLine("name 为空"); 22 // return false; 23 //} 24 //if (request.Password == null) 25 //{ 26 // Console.WriteLine("password 为空"); 27 // return false; 28 //} 29 30 // 现在加入第一个委托变成这样: 31 func = new Func<Request, bool>(request => 32 { 33 if (request.Url == null) 34 { 35 Console.WriteLine("url 为空"); 36 return false; 37 } 38 // 真正要做的事情. 39 Console.WriteLine(request.Name); 40 Console.WriteLine(request.Url); 41 Console.WriteLine(request.Password); 42 Console.WriteLine("比如存个数据库什么的"); 43 return true; 44 }); 45 46 // 加入第二个: 47 func = new Func<Request, bool>(request => 48 { 49 if (request.Url == null) 50 { 51 Console.WriteLine("url 为空"); 52 return false; 53 } 54 if (request.Name == null) 55 { 56 Console.WriteLine("name 为空"); 57 return false; 58 } 59 // 真正要做的事情. 60 Console.WriteLine(request.Name); 61 Console.WriteLine(request.Url); 62 Console.WriteLine(request.Password); 63 Console.WriteLine("比如存个数据库什么的"); 64 return true; 65 }); 66 67 // 加入第三个: 68 func = new Func<Request, bool>(request => 69 { 70 if (request.Password == null) 71 { 72 Console.WriteLine("password 为空"); 73 return false; 74 } 75 76 if (request.Name == null) 77 { 78 Console.WriteLine("name 为空"); 79 return false; 80 } 81 82 if (request.Url == null) 83 { 84 Console.WriteLine("url 为空"); 85 return false; 86 } 87 88 // 真正要做的事情. 89 Console.WriteLine(request.Name); 90 Console.WriteLine(request.Url); 91 Console.WriteLine(request.Password); 92 Console.WriteLine("比如存个数据库什么的"); 93 return true; 94 }); 95 96 // 由此可见,每次外面包新的验证,都是在最上面写的,执行也会执行最新的,所以最先验证的就要最后加进来 97 }
标签:Console,Func,委托,验证,request,return,WriteLine,封装,func From: https://www.cnblogs.com/anlingxiao/p/18058192