<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
        Go to my github

      ASP.NET Core快速入門(第3章:依賴注入)--學習筆記

      課程鏈接:http://video.jessetalk.cn/course/explore

      良心課程,大家一起來學習哈!

      任務16:介紹

      1、依賴注入概念詳解

      • 從UML和軟件建模來理解
      • 從單元測試來理解

      2、ASP.NET Core 源碼解析

      任務17:從UML角度來理解依賴

      1、什么是依賴

      當一個類A完成某個任務需要另一個類B來幫助時,A就對B產生了依賴

      例如CustomerController需要對customer進行新增或查找時用到EF,則對EF的Context產生了依賴

      var context = new CustomerContext(new DbContextOptions<CustomerContext>{});
      

      2、顯示依賴與隱式依賴

      顯示依賴:把一個類用到的所有外部組件放到一個類最上面,在構造函數里面初始化

      private CustomerContext _context;
      
      public CustomerController()
      {
          _context = new CustomerContext(new DbContextOptions<CustomerContext>{});
      }
      

      隱式依賴:需要用到的地方再初始化,不推薦

      var context = new CustomerContext(new DbContextOptions<CustomerContext>{});
      

      3、依賴倒置原則

      依賴高層業務,不依賴低層業務的具體實現,而依賴于具體的抽象

      CustomerController是高層業務的一個組件,依賴于CustomerContext是一個低層數據庫的實現,如果現在需要把EF換成一個內存的實現或者mysql,需要修改CustomerController類,風險很大,所以應該依賴于低層業務的抽象

      把低層業務方法抽象,比如查找,新增,抽象出一個接口,當不需要使用EF的時候,使用內存的實現替換

      private ICustomerRepository _customerRepository;
      
      public CustomerController()
      {
          _customerRepository = new EfCustomerRepository(
              new CustomerContext(new DbContextOptions<CustomerContext>{}));
      }
      

      任務18:控制反轉

      實現依賴注入的方式不由自己決定,而是交給一個IOC容器,需要什么由容器傳入,比如生產環境需要使用EF,則由容器傳入一個EfCustomerRepository,而測試環境需要使用內存級別的,則傳入一個MemoryCustomerRepository

      private ICustomerRepository _customerRepository;
      
      public CustomerController(ICustomerRepository customerRepository)
      {
          _customerRepository = customerRepository;
      }
      

      任務19:單元測試

      var repository = new Data.MemoryCustomerRepository();
      var controller = new CustomerController(repository);// 通過外部控制Controller里面的依賴
      
      var customer = new Model.Customer()
      {
          FirstName = "Mingson",
          LastName = "Zheng",
          Phone = "123456789",
      };
      
      var result = controller.Add(customer);
      Assert.IsType<OkResult>(result);// 正確結果
      
      var resultBad = controller.Add(customer);
      Assert.IsType<BadRequestObjectResult>(resultBad);// 錯誤結果
      

      通過單元測試可以得知修改Bug過程中是否誤刪代碼,導致原來通過的測試現在無法通過。

      任務20:DI初始化的源碼解讀

      Microsoft.AspNetCore.Hosting.WebHostBuilder

          /// <summary>
          /// Builds the required services and an <see cref="T:Microsoft.AspNetCore.Hosting.IWebHost" /> which hosts a web application.
          /// </summary>
          public IWebHost Build()
          {
      
            ......
            
            // 第一步,build
            
            IServiceCollection serviceCollection1 = this.BuildCommonServices(out hostingStartupErrors);
            
            // 第二步,獲取ServiceCollection,ServiceProvider
            
            IServiceCollection serviceCollection2 = serviceCollection1.Clone();
            IServiceProvider providerFromFactory = GetProviderFromFactory(serviceCollection1);
            
            ......
            
            // 第三步,new一個WebHost,傳入ServiceCollection,ServiceProvider
            
            WebHost webHost = new WebHost(serviceCollection2, providerFromFactory, this._options, this._config, hostingStartupErrors);
            
            ......
            
            // 第四步,webHost初始化方法Initialize
            
              webHost.Initialize();
              
              ......
      
          }
      

      第一步BuildCommonServices中new一個ServiceCollection就是在startup接口中使用

          private IServiceCollection BuildCommonServices(
            out AggregateException hostingStartupErrors)
          {
      
            ......
            
            ServiceCollection services = new ServiceCollection();
            
            // new完之后添加一些初始化操作
            
            ......
      
            return (IServiceCollection) services;
          }
      

      IStartup接口

      namespace Microsoft.AspNetCore.Hosting
      {
        public interface IStartup
        {
          IServiceProvider ConfigureServices(IServiceCollection services);
      
          void Configure(IApplicationBuilder app);// 配置管道
        }
      }
      

      第四步,webHost初始化方法Initialize

          public void Initialize()
          {
            
              ......
            
              this.EnsureApplicationServices();
      
              ......
      
          }
      
          private void EnsureApplicationServices()
          {
            
            ......
            
            this.EnsureStartup();
            this._applicationServices = this._startup.ConfigureServices(this._applicationServiceCollection);
          }
          
          private void EnsureStartup()
          {
            if (this._startup != null)
              return;
            this._startup = this._hostingServiceProvider.GetService<IStartup>();
            if (this._startup == null)
              throw new InvalidOperationException(string.Format("No startup configured. Please specify startup via WebHostBuilder.UseStartup, WebHostBuilder.Configure, injecting {0} or specifying the startup assembly via {1} in the web host configuration.", (object) "IStartup", (object) "StartupAssemblyKey"));
          }
      

      任務21:依賴注入的使用

      了解ASP.NET Core 依賴注入,看這篇就夠了:

      http://www.jessetalk.cn/2017/11/06/di-in-aspnetcore/

      知識共享許可協議

      本作品采用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可。

      歡迎轉載、使用、重新發布,但務必保留文章署名 鄭子銘 (包含鏈接: http://www.rzrgm.cn/MingsonZheng/ ),不得用于商業目的,基于本文修改后的作品務必以相同的許可發布。

      如有任何疑問,請與我聯系 (MingsonZheng@outlook.com) 。

      posted @ 2019-07-31 01:01  鄭子銘  閱讀(730)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 久久婷婷综合色丁香五月| 免费看欧美日韩一区二区三区| 九九热精品免费在线视频| 亚洲欧洲日产国码高潮αv| 免费看成人aa片无码视频吃奶 | 国产一级片内射在线视频| 日本污视频在线观看| 成人一区二区三区在线午夜| 国产av亚洲精品ai换脸电影| 欧美在线人视频在线观看| 春色校园综合人妻av| 久久综合色一综合色88欧美| 亚洲欧美日韩国产精品一区二区| 亚洲一区二区三区四区三级视频| 国产第一区二区三区精品| 亚洲制服无码一区二区三区 | 午夜在线观看成人av| 久久不见久久见免费影院www日本 亚洲综合精品一区二区三区 | 深夜福利资源在线观看| 亚洲区一区二区三区精品| 男人扒女人添高潮视频| 麻豆国产97在线 | 欧美| 男人的天堂av社区在线| 共和县| 青青草原国产精品啪啪视频| 国产L精品国产亚洲区在线观看| 天堂mv在线mv免费mv香蕉| 人妻系列无码专区无码中出| 国产亚洲欧美在线观看三区| 广水市| 国产成人精品1024免费下载| 久久综合久中文字幕青草| 鹿邑县| 国内偷自第一区二区三区| 亚洲欧洲日韩精品在线| 深夜av免费在线观看| 国产精品制服丝袜第一页| 久久中精品中文字幕入口| 图片区小说区av区| 又粗又硬又黄a级毛片| 宁蒗|