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


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM