文章首發地址c#
在上一篇中,咱們講述了什麼是控制反轉(IoC)以及經過哪些方式實現的。這其中,咱們明白了,控制反轉(IoC) 是一種軟件設計的模式,指導咱們設計出更優良,更具備鬆耦合的程序,而具體的實現方式有依賴注入和依賴查找。框架
在上篇實例中,咱們經過日誌的方式舉例說明,其中經過代碼建立了一個ILogger
的接口,並實現接口實例,基於控制反轉的模式,依賴的建立也移交到了外部,可是也發現存在了問題,若是相似存在這樣多個接口和實現類,依賴太多,一一建立,沒有統一的管理,這反而增長了實際工做量麻煩。ide
所以咱們須要一個能夠統一管理系統中全部的依賴的地方,所以,IoC容器誕生了。函數
容器負責兩件事情:學習
因此在這一篇中,咱們主要講述Asp.Net Core中內置的IoC容器。測試
在Asp.Net Core中已經爲咱們集成提供了一個內置的IoC容器,咱們能夠看到在Startup.cs
的ConfigureServices
中,涉及到依賴注入的核心組件,一個負責實例註冊的IServiceCollection
和一個負責提供實例的IServiceProvider
ui
簡單的說就是兩步:1. 把實例註冊到容器中;2. 從容器中獲取實例.net
而在這其中,IServiceCollection
爲實現將開發者定義好的實例註冊進去提供了三種方法。設計
分別是:日誌
AddTransient 、AddScoped 、AddSingleton
而這三種不一樣實例方法也對應的着三種不一樣的實例生命週期。
三種不一樣的生命週期:
AddTransient
每次在向服務容器進行請求時都會建立新的實例,這種生存期適合輕量級、 無狀態的服務。
AddScoped
在每次Web請求時被建立一次實例,生命週期橫貫整次請求。
局部單例對象, 在某個局部內是同一個對象(做用域單例,本質是容器單例);一次請求內是一個單例對象,屢次請求則多個不一樣的單例對象。
AddSingleton
建立單例生命週期服務的狀況以下:
其後的每個後續請求都使用同一個實例。若是開發者的應用須要單例服務情景,推薦的作法是交給服務容器來負責單例的建立和生命週期管理,而不是手動實現單例模式而後由開發者在自定義類中進行操做。
不要從單一實例解析指定了做用域的服務。 當處理後續請求時,它可能會致使服務處於不正確的狀態。 能夠從範圍內或暫時性服務解析單一實例服務。
定義三個接口,分別測試Singleton,Scope,Transient三種,一個 TestService服務
public interface ITransientService { string GetGuid(); }
public interface IScopedService { string GetGuid(); }
public interface ISingletonService { string GetGuid(); }
public interface ITestService { public string GetSingletonID(); public string GetTransientID(); public string GetScopedID(); }
根據上面定義的幾種接口,並一一實現對應的接口
public class TransientService : ITransientService { public string OperationId { get; } public TransientService() { OperationId = Guid.NewGuid().ToString()[^4..]; } public string GetGuid() { return $"這是一個 Transient service : " + OperationId; } }
public class ScopedService : IScopedService { public string OperationId { get; } public ScopedService() { OperationId = Guid.NewGuid().ToString()[^4..]; } public string GetGuid() { return $"這是一個 scoped service : "+ OperationId; } }
public class SingletonService : ISingletonService { public string OperationId { get; } public SingletonService() { OperationId = Guid.NewGuid().ToString()[^4..]; } public string GetGuid() { return $"這是一個 Singleton service : " + OperationId; } }
public class TestService : ITestService { private ITransientService _transientService; private IScopedService _scopedService; private ISingletonService _singletonService; public TestService(ITransientService transientService, IScopedService scopedService, ISingletonService singletonService) { _transientService = transientService; _scopedService = scopedService; _singletonService = singletonService; } public string GetSingletonID() { return _singletonService.GetGuid(); } public string GetTransientID() { return _transientService.GetGuid(); } public string GetScopedID() { return _scopedService.GetGuid(); } }
在Startup.cs
類文件ConfigureServices
方法中,注入依賴
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddTransient<ITransientService, TransientService>(); services.AddSingleton<ISingletonService, SingletonService>(); services.AddScoped<IScopedService, ScopedService>(); services.AddScoped<ITestService, TestService>(); }
定義一個控制器,實現調用
[ApiController] [Route("[controller]")] public class TestController : ControllerBase { private ITransientService _transientService; private IScopedService _scopedService; private ISingletonService _singletonService; private ITestService _testService; public TestController(ITransientService transientService, IScopedService scopedService, ISingletonService singletonService, ITestService testService ) { _transientService = transientService; _scopedService = scopedService; _singletonService = singletonService; _testService = testService; } [HttpGet] public JsonResult Get() { var data1 = _transientService.GetGuid(); var data2 = _testService.GetTransientID(); var data3 = _scopedService.GetGuid(); var data4 = _testService.GetScopedID(); var data5 = _singletonService.GetGuid(); var data6 = _testService.GetSingletonID(); return new JsonResult(new { data1, data2, data3 , data4, data5, data6, }); } }
在上面中咱們瞭解到,注入的方式通常有三種,構造函數注入, 方法注入,屬性注入,而在ASP.NET Core中自帶的這個IoC容器,默認採用了構造函數注入的方式。
啓動運行項目,訪問接口/Test
效果以下:
對比兩次的請求訪問能夠發現,上面咱們一共獲得了 4個Transient實例,2個Scope實例,1個Singleton實例。
在請求中,AddSingleton方式的id值相同;
AddScope方式兩次請求之間不一樣,但同一請求內是相同的;
AddTransient方式在同一請求內的屢次注入間都不相同。
經過上述的代碼示例,也證明了以前的三種方式對應不一樣生命週期的說明。
暫時性(Transient) : 生命週期是每次得到對象都是一次新的對象,每一次都不同。
做用域內(Scoped) : 生命週期是在每一次請求做用域內是同一個對象,非做用域內則是新的對象。
單例(Singletion) : 生命週期是這個服務啓動後都是一個對象,也便是全局單例對象
。
經過上述的瞭解,咱們知道IoC容器是一個依賴注入框架,在.NET Core中也提供了內置的IoC容器,經過AddXXX方法來實例依賴對象,而在實際開發中,就是每個實例都須要一個個的添加,這樣的實現方式在小項目中還好,可是若是在複雜大型的項目中,就略向麻煩些,可能須要添加不少方法來實現,總體的可觀性也很差。
爲了達到能夠簡化咱們工做量,應該採用批量註冊,所以咱們也能夠引入其餘的Ioc容器框架,實現更多的功能和擴展。
在平時開發中,經常使用的IoC框架有不少,而在這裏咱們選擇用Autofac,這也是在.net下比較流行的,其餘的框架不作說明,可自行查閱瞭解。
ASP.Net Core中使用Autofac 框架注入 (在後續篇章會具體說明)
在實際的開發中,對於這幾種生命週期,咱們應該如何應用呢?
好比,在像DBContext這種實例,在實際開發中是用Transient 仍是Scoped呢?
建議使用Scoped
由於有些對象在請求中能夠須要用到多個方法或者多個Service、Repository的時候,爲了減小實例初始化的消耗,實現事務功能,能夠在整個請求的生命週期共用一個Scope。
其餘的思考:
以上的思考,你們能夠說說本身的想法
在後續的篇章中,也會對這些問題,進行深刻討論說明。
本篇主要介紹了什麼是IoC容器,瞭解到它是DI構造函注入的框架,它管理着依賴項的生命週期以及映射關係,同時也介紹實踐了在ASP.Net Core中,默認提供的內置IoC容器,以及它的實例註冊方式和相應的生命週期。
好啦,這篇文章就先講述到這裏吧,在後續篇章中會對ASP.Net Core中使用Autofac 框架實踐說明,但願對你們有所幫助。
若是有不對的或不理解的地方,但願你們能夠多多指正,提出問題,一塊兒討論,不斷學習,共同進步。🤣