C#在 .NET中使用依賴注入的示例詳解
寫在前面
在 .NET 中使用依賴注入 (DI)。 可以借助 Microsoft 擴(kuò)展,通過添加服務(wù)并在 IServiceCollection 中配置這些服務(wù)來管理 DI。 使用 IHost 接口來公開所有 IServiceProvider 實(shí)例,用來充當(dāng)所有已注冊的服務(wù)的容器。
示例代碼中使用了一個(gè)關(guān)鍵的枚舉 ServiceLifetime 指定 IServiceCollection 中服務(wù)的生存期,該枚舉包含三個(gè)類型:
Scoped 服務(wù)只會(huì)隨著新范圍而改變,但在一個(gè)范圍中是相同的實(shí)例。
Singleton 服務(wù)總是相同的,新實(shí)例僅被創(chuàng)建一次。
Transient 服務(wù)總是不同的,每次檢索服務(wù)時(shí),都會(huì)創(chuàng)建一個(gè)新實(shí)例。
需要從NuGet安裝 Microsoft.Extensions.Hosting 類庫
代碼實(shí)現(xiàn)
服務(wù)接口實(shí)現(xiàn)
using Microsoft.Extensions.DependencyInjection; namespace ConsoleDI.Example; public interface IReportServiceLifetime { Guid Id { get; } ServiceLifetime Lifetime { get; } } // 創(chuàng)建了多個(gè)接口和相應(yīng)的實(shí)現(xiàn)。 其中每個(gè)服務(wù)都唯一標(biāo)識(shí)并與 ServiceLifetime 配對 public interface IExampleTransientService : IReportServiceLifetime { ServiceLifetime IReportServiceLifetime.Lifetime => ServiceLifetime.Transient; } public interface IExampleScopedService : IReportServiceLifetime { ServiceLifetime IReportServiceLifetime.Lifetime => ServiceLifetime.Scoped; } public interface IExampleSingletonService : IReportServiceLifetime { ServiceLifetime IReportServiceLifetime.Lifetime => ServiceLifetime.Singleton; } internal sealed class ExampleTransientService : IExampleTransientService { Guid IReportServiceLifetime.Id { get; } = Guid.NewGuid(); } internal sealed class ExampleScopedService : IExampleScopedService { Guid IReportServiceLifetime.Id { get; } = Guid.NewGuid(); } internal sealed class ExampleSingletonService : IExampleSingletonService { Guid IReportServiceLifetime.Id { get; } = Guid.NewGuid(); }
示例代碼
namespace ConsoleDI.Example; internal sealed class ServiceLifetimeReporter( IExampleTransientService transientService, IExampleScopedService scopedService, IExampleSingletonService singletonService) { public void ReportServiceLifetimeDetails(string lifetimeDetails) { Console.WriteLine(lifetimeDetails); LogService(transientService, "每次都是新建的對象,一直保持不同"); LogService(scopedService, "在函數(shù)域范圍內(nèi)只創(chuàng)建一次,不同函數(shù)內(nèi)為不同對象"); LogService(singletonService, "全局單例,一直是同一個(gè)"); } private static void LogService<T>(T service, string message) where T : IReportServiceLifetime => Console.WriteLine($" {typeof(T).Name}: {service.Id} ({message})"); }
調(diào)用示例
到此這篇關(guān)于C#在 .NET中使用依賴注入的示例詳解的文章就介紹到這了,更多相關(guān)C#依賴注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)鼠標(biāo)拖拽無邊框浮動(dòng)窗體的方法
一般情況下,在標(biāo)題欄中按住鼠標(biāo)左鍵不放即可實(shí)現(xiàn)拖動(dòng)操作,當(dāng)做浮動(dòng)窗體時(shí),如果包含窗體邊框,那么界面給使用者的感覺將很不友好,因此本文給大家介紹了C#實(shí)現(xiàn)鼠標(biāo)拖拽無邊框浮動(dòng)窗體的方法,感興趣的朋友可以參考下2024-04-04Unity UI組件ScrollRect實(shí)現(xiàn)無限滾動(dòng)條
這篇文章主要為大家詳細(xì)介紹了Unity UI組件ScrollRect實(shí)現(xiàn)無限滾動(dòng)條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07C# 實(shí)現(xiàn)Trim方法去除字符串前后的所有空格
這篇文章主要介紹了C# 實(shí)現(xiàn)Trim方法去除字符串前后的所有空格,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12C#中的Timer和DispatcherTimer使用實(shí)例
這篇文章主要介紹了C#中的Timer和DispatcherTimer使用實(shí)例,本文分別給出它們的使用代碼實(shí)例,需要的朋友可以參考下2015-01-01