欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#在 .NET中使用依賴(lài)注入的示例詳解

 更新時(shí)間:2024年01月10日 08:37:07   作者:rjcql  
這篇文章主要為大家詳細(xì)介紹了C#如何在 .NET中使用依賴(lài)注入,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下

寫(xiě)在前面

在 .NET 中使用依賴(lài)注入 (DI)。 可以借助 Microsoft 擴(kuò)展,通過(guò)添加服務(wù)并在 IServiceCollection 中配置這些服務(wù)來(lái)管理 DI。 使用 IHost 接口來(lái)公開(kāi)所有 IServiceProvider 實(shí)例,用來(lái)充當(dāng)所有已注冊(cè)的服務(wù)的容器。

示例代碼中使用了一個(gè)關(guān)鍵的枚舉 ServiceLifetime 指定 IServiceCollection 中服務(wù)的生存期,該枚舉包含三個(gè)類(lèi)型:

Scoped 服務(wù)只會(huì)隨著新范圍而改變,但在一個(gè)范圍中是相同的實(shí)例。

Singleton 服務(wù)總是相同的,新實(shí)例僅被創(chuàng)建一次。

Transient 服務(wù)總是不同的,每次檢索服務(wù)時(shí),都會(huì)創(chuàng)建一個(gè)新實(shí)例。

需要從NuGet安裝  Microsoft.Extensions.Hosting 類(lèi)庫(kù)

代碼實(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 配對(duì)
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, "每次都是新建的對(duì)象,一直保持不同");
        LogService(scopedService, "在函數(shù)域范圍內(nèi)只創(chuàng)建一次,不同函數(shù)內(nèi)為不同對(duì)象");
        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中使用依賴(lài)注入的示例詳解的文章就介紹到這了,更多相關(guān)C#依賴(lài)注入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 日常收集C#接口知識(shí)(知識(shí)全面)

    日常收集C#接口知識(shí)(知識(shí)全面)

    本文分為七章節(jié)給大家介紹c#接口知識(shí),內(nèi)容比較詳細(xì),特此分享腳本之家平臺(tái),供大家參考
    2016-01-01
  • c#求范圍內(nèi)素?cái)?shù)的示例分享(c#求素?cái)?shù))

    c#求范圍內(nèi)素?cái)?shù)的示例分享(c#求素?cái)?shù))

    問(wèn)題是判斷101-200之間有多少個(gè)素?cái)?shù),并輸出所有素?cái)?shù)。下面是使用C#解決這個(gè)問(wèn)題的方法 ,需要的朋友可以參考下
    2014-03-03
  • C# Redis學(xué)習(xí)系列(一)Redis下載安裝使用

    C# Redis學(xué)習(xí)系列(一)Redis下載安裝使用

    這篇文章主要為大家分享了C# Redis學(xué)習(xí)系列教程第一篇, Redis下載、安裝、使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • C#使用泛型隊(duì)列Queue實(shí)現(xiàn)生產(chǎn)消費(fèi)模式

    C#使用泛型隊(duì)列Queue實(shí)現(xiàn)生產(chǎn)消費(fèi)模式

    這篇文章介紹了C#使用泛型隊(duì)列Queue實(shí)現(xiàn)生產(chǎn)消費(fèi)模式的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • C# 實(shí)現(xiàn)FTP客戶(hù)端的小例子

    C# 實(shí)現(xiàn)FTP客戶(hù)端的小例子

    這篇文章主要介紹了C# 如何實(shí)現(xiàn)FTP客戶(hù)端,文中實(shí)例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#實(shí)現(xiàn)DevExpress本地化實(shí)例詳解

    C#實(shí)現(xiàn)DevExpress本地化實(shí)例詳解

    這篇文章主要介紹了C#實(shí)現(xiàn)DevExpress本地化,以實(shí)例形式較為詳細(xì)的分析了DevExpress本地化的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • 最新評(píng)論