深入理解ASP.NET Core依賴註入

来源:https://www.cnblogs.com/sword-successful/archive/2020/02/18/12329198.html
-Advertisement-
Play Games

概述 ASP.NET Core可以說是處處皆註入,本文從基礎角度理解一下原生DI容器,及介紹下怎麼使用並且如何替換官方提供的預設依賴註入容器。 什麼是依賴註入 百度百科中對於依賴註入的定義: 控制反轉 (Inversion of Control,縮寫為 IoC ),是 "面向對象編程" 中的一種設計 ...


image.png

概述

       ASP.NET Core可以說是處處皆註入,本文從基礎角度理解一下原生DI容器,及介紹下怎麼使用並且如何替換官方提供的預設依賴註入容器。

什麼是依賴註入

       百度百科中對於依賴註入的定義:控制反轉(Inversion of Control,縮寫為IoC),是面向對象編程中的一種設計原則,可以用來減低電腦代碼之間的耦合度。其中最常見的方式叫做依賴註入(Dependency Injection,簡稱DI),還有一種方式叫“依賴查找”(Dependency Lookup)。通過控制反轉,對象在被創建的時候,由一個調控系統內所有對象的外界實體將其所依賴的對象的引用傳遞給它。也可以說,依賴被註入到對象中。

依賴反轉前

那麼在依賴反轉之前或者叫控制反轉之前,直接依賴是怎麼工作的呢,這裡ClassA直接依賴ClassB,而ClassB又直接依賴ClassC,任何一處的變動都會牽一發而動全身,不符合軟體工程的設計原則。
image.png

依賴反轉後

應用依賴關係反轉原則後,A 可以調用 B 實現的抽象上的方法,讓 A 可以在運行時調用 B,而 B 又在編譯時依賴於 A 控制的介面(因此,典型的編譯時依賴項發生反轉) 。 運行時,程式執行的流程保持不變,但介面引入意味著可以輕鬆插入這些介面的不同實現。
image.png
依賴項反轉是生成鬆散耦合應用程式的關鍵一環,因為可以將實現詳細信息編寫為依賴並實現更高級別的抽象,而不是相反 。 因此,生成的應用程式的可測試性、模塊化程度以及可維護性更高。 遵循依賴關係反轉原則可實現依賴關係註入 。

何謂容器

       如果你用過Spring,就知道其龐大而全能的生態圈正是因為有了它包含各種各樣的容器來做各種事情,其本質也是一個依賴反轉工廠,那麼不知道你註意到沒有,控制反轉後產生依賴註入,這樣的工作我們可以手動來做,那麼如果註入的服務成千上萬呢,那怎麼玩呢?那麼問題來了,控制反轉了,依賴的關係也交給了外部,現在的問題就是依賴太多,我們需要有一個地方來管理所有的依賴,這就是容器的角色。
      容器的主要職責有兩個:綁定服務與實例之間的關係(控制生命周期)獲取實例並對實例進行管理(創建和銷毀)

ASP.NET Core里依賴註入是怎麼實現的

     在.Net Core里提供了預設的依賴註入容器IServiceCollection,它是一個輕量級容器。核心組件為兩個IServiceCollection和IServiceProvider,IServiceCollection負責註冊,IServiceProvider負責提供實例。
      使用兩個核心組件前導入命名空間Microsoft.Extensions.DependencyInjection.
      預設的ServiceCollection有以下三個方法:

IServiceCollection serviceCollection=new ServiceCollection();
#三個方法都是註冊實例,只不過實例的生命周期不一樣。
#單例模式,只有一個實例
serviceCollection.AddSingleton<ILoginService, EFLoginService>();
#每次請求都是同一個實例,比如EntityFramework.Context
serviceCollection.AddScoped<ILoginService, EFLoginService>();
#每次調用都是不同的實例
serviceCollection.AddTransient<ILoginService, EFLoginService>();
#介面聲明
public interface IServiceCollection : IList<ServiceDescriptor>, ICollection<ServiceDescriptor>, IEnumerable<ServiceDescriptor>, IEnumerable
{
}
#預設的ServiceCollection實際上是一個提供了ServiceDescriptor的List。 
public class ServiceCollection : IServiceCollection, ICollection<ServiceDescriptor>, IEnumerable<ServiceDescriptor>, IEnumerable, IList<ServiceDescriptor>
  {
    private readonly List<ServiceDescriptor> _descriptors = new List<ServiceDescriptor>();

    public int Count
    {
      get
      {
        return this._descriptors.Count;
      }
    }
    
    public bool IsReadOnly
    {
      get
      {
        return false;
      }
    }

    public ServiceDescriptor this[int index]
    {
      get
      {
        return this._descriptors[index];
      }
      set
      {
        this._descriptors[index] = value;
      }
    }

    public void Clear()
    {
      this._descriptors.Clear();
    }

    public bool Contains(ServiceDescriptor item)
    {
      return this._descriptors.Contains(item);
    }
    
    public void CopyTo(ServiceDescriptor[] array, int arrayIndex)
    {
      this._descriptors.CopyTo(array, arrayIndex);
    }

    public bool Remove(ServiceDescriptor item)
    {
      return this._descriptors.Remove(item);
    }

    public IEnumerator<ServiceDescriptor> GetEnumerator()
    {
      return (IEnumerator<ServiceDescriptor>) this._descriptors.GetEnumerator();
    }

    void ICollection<ServiceDescriptor>.Add(ServiceDescriptor item)
    {
      this._descriptors.Add(item);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
      return (IEnumerator) this.GetEnumerator();
    }

    public int IndexOf(ServiceDescriptor item)
    {
      return this._descriptors.IndexOf(item);
    }
    
    public void Insert(int index, ServiceDescriptor item)
    {
      this._descriptors.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
      this._descriptors.RemoveAt(index);
    }
  }

三個方法對應的生命周期值,在枚舉ServiceLifeTime中定義:

public enum ServiceLifetime
{
   Singleton, 
   Scoped,
   Transient,
}

三個方法確切來說是定義在擴展方法ServiceCollectionServiceExtensions中定義:

#這裡我列出個別方法,詳細可參看源碼
#導入Microsoft.Extensions.DependencyInjection
public static class ServiceCollectionServiceExtensions
{   
    public static IServiceCollection AddTransient(
      this IServiceCollection services,
      Type serviceType,
      Type implementationType)
    {
      if (services == null)
        throw new ArgumentNullException(nameof (services));
      if (serviceType == (Type) null)
        throw new ArgumentNullException(nameof (serviceType));
      if (implementationType == (Type) null)
        throw new ArgumentNullException(nameof (implementationType));
      //這裡註入時指定ServiceLifetime.Transient
      return ServiceCollectionServiceExtensions.Add(services, serviceType, implementationType, ServiceLifetime.Transient);
    }
    
    public static IServiceCollection AddScoped(
      this IServiceCollection services,
      Type serviceType,
      Type implementationType)
    {
      if (services == null)
        throw new ArgumentNullException(nameof (services));
      if (serviceType == (Type) null)
        throw new ArgumentNullException(nameof (serviceType));
      if (implementationType == (Type) null)
        throw new ArgumentNullException(nameof (implementationType));
      //這裡註入時指定ServiceLifetime.Scoped
      return ServiceCollectionServiceExtensions.Add(services, serviceType, implementationType, ServiceLifetime.Scoped);
    }
    
    public static IServiceCollection AddSingleton(
      this IServiceCollection services,
      Type serviceType,
      Type implementationType)
    {
      if (services == null)
        throw new ArgumentNullException(nameof (services));
      if (serviceType == (Type) null)
        throw new ArgumentNullException(nameof (serviceType));
      if (implementationType == (Type) null)
        throw new ArgumentNullException(nameof (implementationType));
      //這裡註入時指定ServiceLifetime.Singleton
      return ServiceCollectionServiceExtensions.Add(services, serviceType, implementationType, ServiceLifetime.Singleton);
    }
}

ASP.NET Core里依賴註入是怎樣運行的

在Startup中初始化

ASP.NET Core在Startup.ConfigureService中註入指定服務,可以從方法參數IServiceCollection中看出,這裡還有個方法services.AddMvc(), 這個MVC框架本身自己註入的服務,定義在MvcServiceCollectionExtesnsions類中。

#Startup
public void ConfigureServices(IServiceCollection services)
{
     services.AddMvc();
     services.AddSingleton<ILoginService, EFLoginService>();        
}

在構造函數中註入

官方推薦在構造器中註入,這裡也是為了體現顯示依賴。

public class AccountController
{
    private ILoginService _loginService;
    public AccountController(ILoginService loginService)
    {
        _loginService = loginService;
    }
}

如何替換其他容器

      前面提到原生的依賴註入容器只是一個輕量級容器,但是功能真的很有限,比如屬性註入、方法註入、子容器、lazy對象初始化支持。為何不好好借鑒一下Spring強大的背景呢,所以這裡我們用Autofac替換系統預設的依賴註入容器。先引用命名空間Autofac、Autofac.Extensions.DependencyInjection。我本機環境使用的.Net Core3.0。 3.0不能修改直接修改Startup的ConfigureService方法了,直接修改ConfigureService方法返回值會拋出異常ConfigureServices returning an System.IServiceProvider isn't supported. 這裡可以參考Autofac文檔,已經有說明。

修改Startup

#直接聲明方法ConfigureContainer 
public class Startup
 {
     public Startup(IConfiguration configuration)
     {
         Configuration = configuration;
     }

     public IConfiguration Configuration { get; }

     public void ConfigureServices(IServiceCollection services)
     {
         services.AddControllersWithViews();
         services.AddMvc();
     }

     public void ConfigureContainer(ContainerBuilder containerBuilder)
     {
         containerBuilder.RegisterType<EFLoginService>().As<ILoginService>();
     }

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
     {
         if (env.IsDevelopment())
         {
             app.UseDeveloperExceptionPage();
         }
         else
         {
             app.UseExceptionHandler("/Home/Error");
             app.UseHsts();
         }

         app.UseStaticFiles();
         app.UseRouting();
         app.UseAuthorization();
         app.UseEndpoints(endpoints =>
                          {
                              endpoints.MapControllerRoute(
                                  name: "default",
                                  pattern: "{controller=Home}/{action=Index}/{id?}");
                          });
     }
 }

修改Program

#導入命名空間Autofac.Extensions.DependencyInjections,然後調用UseServiceProviderFactory 
public class Program
 {
     public static void Main(string[] args)
     {
         CreateHostBuilder(args).Build().Run();
     }

     public static IHostBuilder CreateHostBuilder(string[] args) =>
         Host.CreateDefaultBuilder(args)
         .ConfigureWebHostDefaults(
         webBuilder => { webBuilder.UseStartup<Startup>(); })
         .UseServiceProviderFactory(new AutofacServiceProviderFactory());
 }

參考鏈接

https://docs.microsoft.com/zh-cn/dotnet/architecture/modern-web-apps-azure/architectural-principles#dependency-inversion
https://www.cnblogs.com/loogn/p/10566510.html
https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 打開python官網https://www.python.org/,然後點擊頁面的downloads導航菜單。在下拉的菜單中選中windows, 直接點擊頁面右側的 python 3.X(X表示對應的版本號),可以下載最新的python安裝包。安裝完畢以後,在命令行中執行python -V來測試是... ...
  • 把x用八進位,十進位、十六進位的形式列印,把y用布爾值的形式列印:int x = 10;cout << oct << x << endl; //show octalcout << dec << x << endl; //show decimalcout << hex << x << endl; //... ...
  • 樹結構基礎 LCA c++ ……(省略,同LCA) int L[N], R[N];//每個子樹代表的區間 int tot;//總時間 //搜索整棵樹, 得到每個節點的深度 void dfs(int u, int f){//u: 一節點 f: 其節點的父親節點 L[u] = ++tot; dep[u] ...
  • [toc] 一.Java的基本數據類型介紹 | 類型 | 關鍵字 | 位數 | 預設值 | 取值範圍 | | : : | : : | : : | : : | : : | | 位元組型 | byte | 8 | 0 | 128~127 | | 短整型 | short | 16 | 0 | 32768~3 ...
  • springboot預設打包成jar,如果想打包成war,則需要做以下三步。 1.修改pom.xml文件 a.將jar改成war 1 <groupId>com.test</groupId> 2 <artifactId>01-springboot-HelloWorld</artifactId> 3 < ...
  • 下載cron包: go get github.com/robfig/cron 開啟一個定時: 根據cron表達式進行時間調度,cron可以精確到秒,大部分表達式格式也是從秒開始。 c := cron.New()預設從分開始,所以加上cron.WithSeconds(),保證定時按照表達式字面意思執行 ...
  • 1.工具類可以一次寫入多方多方調用,很大程度的節約開發時間得到想要的信息 這裡演示一個json介面的工具類(文件定義在App\Http\Controllers\Tools)中 <?php /** * Created by PhpStorm. * User: yly * Date: 2020/2/18 ...
  • 最近由於“武漢肺炎”疫情在家辦公,也沒閑著,最近學習了一下asp.net core mvc的一些網頁開發的的基礎知識,話不多說直接上教程! 一、創建Web應用程式 1)創建新項目 >找到 “ASP.NET Core Web 應用程式” 類型的項目(也可以模糊查詢:語言選擇c#,項目類型選擇Web): ...
一周排行
    -Advertisement-
    Play Games
  • Timer是什麼 Timer 是一種用於創建定期粒度行為的機制。 與標準的 .NET System.Threading.Timer 類相似,Orleans 的 Timer 允許在一段時間後執行特定的操作,或者在特定的時間間隔內重覆執行操作。 它在分散式系統中具有重要作用,特別是在處理需要周期性執行的 ...
  • 前言 相信很多做WPF開發的小伙伴都遇到過表格類的需求,雖然現有的Grid控制項也能實現,但是使用起來的體驗感並不好,比如要實現一個Excel中的表格效果,估計你能想到的第一個方法就是套Border控制項,用這種方法你需要控制每個Border的邊框,並且在一堆Bordr中找到Grid.Row,Grid. ...
  • .NET C#程式啟動閃退,目錄導致的問題 這是第2次踩這個坑了,很小的編程細節,容易忽略,所以寫個博客,分享給大家。 1.第一次坑:是windows 系統把程式運行成服務,找不到配置文件,原因是以服務運行它的工作目錄是在C:\Windows\System32 2.本次坑:WPF桌面程式通過註冊表設 ...
  • 在分散式系統中,數據的持久化是至關重要的一環。 Orleans 7 引入了強大的持久化功能,使得在分散式環境下管理數據變得更加輕鬆和可靠。 本文將介紹什麼是 Orleans 7 的持久化,如何設置它以及相應的代碼示例。 什麼是 Orleans 7 的持久化? Orleans 7 的持久化是指將 Or ...
  • 前言 .NET Feature Management 是一個用於管理應用程式功能的庫,它可以幫助開發人員在應用程式中輕鬆地添加、移除和管理功能。使用 Feature Management,開發人員可以根據不同用戶、環境或其他條件來動態地控制應用程式中的功能。這使得開發人員可以更靈活地管理應用程式的功 ...
  • 在 WPF 應用程式中,拖放操作是實現用戶交互的重要組成部分。通過拖放操作,用戶可以輕鬆地將數據從一個位置移動到另一個位置,或者將控制項從一個容器移動到另一個容器。然而,WPF 中預設的拖放操作可能並不是那麼好用。為瞭解決這個問題,我們可以自定義一個 Panel 來實現更簡單的拖拽操作。 自定義 Pa ...
  • 在實際使用中,由於涉及到不同編程語言之間互相調用,導致C++ 中的OpenCV與C#中的OpenCvSharp 圖像數據在不同編程語言之間難以有效傳遞。在本文中我們將結合OpenCvSharp源碼實現原理,探究兩種數據之間的通信方式。 ...
  • 一、前言 這是一篇搭建許可權管理系統的系列文章。 隨著網路的發展,信息安全對應任何企業來說都越發的重要,而本系列文章將和大家一起一步一步搭建一個全新的許可權管理系統。 說明:由於搭建一個全新的項目過於繁瑣,所有作者將挑選核心代碼和核心思路進行分享。 二、技術選擇 三、開始設計 1、自主搭建vue前端和. ...
  • Csharper中的表達式樹 這節課來瞭解一下表示式樹是什麼? 在C#中,表達式樹是一種數據結構,它可以表示一些代碼塊,如Lambda表達式或查詢表達式。表達式樹使你能夠查看和操作數據,就像你可以查看和操作代碼一樣。它們通常用於創建動態查詢和解析表達式。 一、認識表達式樹 為什麼要這樣說?它和委托有 ...
  • 在使用Django等框架來操作MySQL時,實際上底層還是通過Python來操作的,首先需要安裝一個驅動程式,在Python3中,驅動程式有多種選擇,比如有pymysql以及mysqlclient等。使用pip命令安裝mysqlclient失敗應如何解決? 安裝的python版本說明 機器同時安裝了 ...