When the chain of responsibility encounters DI

There is a project on GitHub, originally as their own research Demo .net core of learning, many students did not expect to see, and gave a lot of stars, so that should rise to 3.0, tidy, written blog to share learning .net core students.

Project Name: Asp.NetCoreExperiment

Project Address: https://github.com/axzxs2001/Asp.NetCoreExperiment

 

This case Github code library

https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/Common/DIChainOfResponsibility

When the design mode of duty chain, dependency injection asp.net core experience of how to do it?

Duty chain is a set of a ring, from FirstTask → SecondTask → ThirdTask → EndTask, the following code

ParentTask is a parent abstract class

 

FirstTask Code

 1    /// <summary>
 2     /// 第一个任务
 3     /// </summary>
 4     public class FirstTask : ITask
 5     {
 6         ITask _task;
 7         readonly ILogger<FirstTask> _logger;
 8         public FirstTask(ILogger<FirstTask> logger, SecondTask secondTask)
 9         {
10             _logger = logger;
11             this.Next(secondTask);
12         }
13         //错误姿势
14         //public FirstTask(ILogger<FirstTask> logger, IEnumerable<ITask> tasks)
15         //{
16         //    _logger = logger;
17         //    foreach (var task in tasks)
18         //    {
19         //        if (task is SecondTask)
20         //        {
21         //            this.Next(task);
22         //        }
23         //    }
24         //}
25 
26         /// <summary>
27         /// 传送下一个方法
28         /// </summary>
29         /// <param name="parentTask"></param>
30         public void Next(ITask task)
31         {
32             Console.WriteLine($"-------------{task.GetType().Name}.Next()");
33             _task = task;
34         }
35         /// <summary>
36         /// 职责链任务方法
37         /// </summary>
38         /// <param name="taskParmeter">任务内容</param>
39         /// <returns></returns>
40         public bool ExecuteTask(TaskParmeter taskParmeter)
41         {
42             var result = SelfTask(taskParmeter);
43             return _task.ExecuteTask(taskParmeter) && result;
44         }
45         bool SelfTask(TaskParmeter taskParmeter)
46         {
47             _logger.LogInformation("-------------------------------------------FirstTask");
48             return true;
49         }
50 }

FirstTask is linked to SecondTask, empathy SecondTask link to ThirdTask, ThirdTask link to EndTask, EndTask is finally, do not need to link to other tasks

. 1      ///  <Summary> 
2      /// final task
 . 3      ///  </ Summary> 
. 4      public  class EndTask: the ITask
 . 5      {       
 . 6          Readonly ILogger <EndTask> _logger;
 . 7          public EndTask (ILogger <EndTask> Logger)
 . 8          {
 . 9              _logger = Logger;
 10          }
 . 11    
12 is          ///  <Summary> 
13 is          /// duty chain method task
 14          ///  </ Summary> 
15          ///  <param name = "taskParmeter">任务内容</param>
16         /// <returns></returns>
17         public bool ExecuteTask(TaskParmeter taskParmeter)
18         {
19             _logger.LogInformation("-------------------------------------------EndTask");
20             return true;
21         }
22 }

Task-dependent occurrence of each task in the constructor of injection, the end EndTask task, it is not necessary tasks down transmission chain

In StartUp, the injection must be the entity class, under the responsibility can not find an exact type of task, in fact, use the code in the comment to the wrong posture code in the constructor try each task.

 1         public void ConfigureServices(IServiceCollection services)
 2         {
 3             //职责链依赖注入
 4             services.AddScoped<EndTask>();
 5             services.AddScoped<ThirdTask>();
 6             services.AddScoped<SecondTask>();
 7             services.AddScoped<FirstTask>();
 8 
 9             //错误姿势
10             //services.AddScoped<ITask,EndTask>();
11             //services.AddScoped<ITask, ThirdTask>();
12             //services.AddScoped<ITask, SecondTask>();
13             //services.AddScoped<ITask, FirstTask>(); 
14        }

In ValuesController notes from the first task

 1   public class ValuesController : ControllerBase
 2     {
 3         /// <summary>
 4         /// 第一个任务
 5         /// </summary>
 6         readonly ITask _task;
 7 
 8         public ValuesController(FirstTask firstTask)
 9         {
10             _task = firstTask;
11         }
12         //错误姿势
13         //public ValuesController(IEnumerable<ITask> tasks)
14         //{
15         //    foreach (var task in tasks)
16         //    {
17         //        if (task is EndTask)
18         //        {
19         //            _task = task;
20         //        }
21         //    }
22         //}
23 
24         [HttpGet]
25         public ActionResult<IEnumerable<string>> Get()
26         {
27             //调用第一个任务
28             _task.ExecuteTask(new TaskParmeter() { TaskID = 1 });
29      
30             return new string[] { "value1", "value2" };
31         }
32 }

 

Guess you like

Origin www.cnblogs.com/axzxs2001/p/11702738.html