Abp vNext 依賴註入

来源:https://www.cnblogs.com/WangJunZzz/archive/2023/09/26/17729743.html
-Advertisement-
Play Games

文章目錄 介紹 ABP的依賴註入系統是基於Microsoft的依賴註入擴展庫(Microsoft.Extensions.DependencyInjection nuget包)開發的。所以我們採用dotnet自帶的註入方式也是支持的。 由於ABP是一個模塊化框架,因此每個模塊都定義它自己的服務併在它自 ...


文章目錄

介紹

ABP的依賴註入系統是基於Microsoft的依賴註入擴展庫(Microsoft.Extensions.DependencyInjection nuget包)開發的。所以我們採用dotnet自帶的註入方式也是支持的

  • 由於ABP是一個模塊化框架,因此每個模塊都定義它自己的服務併在它自己的單獨模塊類中通過依賴註入進行註冊.例:
public class BlogModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        //在此處註入依賴項
        // dotnet自帶依賴註入方式也是支持的
        context.services.AddTransient
        context.services.AddScoped
        context.services.AddSingleton
    }
}

Autofac

Autofac 是.Net世界中最常用的依賴註入框架之一. 相比.Net Core標準的依賴註入庫, 它提供了更多高級特性, 比如動態代理和屬性註入.

集成

1.安裝 Volo.Abp.Autofac nuget 包到你的項目 (對於一個多項目應用程式, 建議安裝到可執行項目或者Web項目中.)
2.模塊添加 AbpAutofacModule 依賴:

    [DependsOn(typeof(AbpAutofacModule))]
    public class MyModule : AbpModule
    {
        //...
    }
}

3.配置 AbpApplicationCreationOptions 用 Autofac 替換預設的依賴註入服務. 根據應用程式類型, 情況有所不同

  • ASP.NET Core 應用程式
public class Program
{
    public static int Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    internal static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseAutofac(); //Integrate Autofac!
}
  • 控制台應用程式
namespace AbpConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var application = AbpApplicationFactory.Create<AppModule>(options =>
            {
                options.UseAutofac(); //Autofac integration
            }))
            {
                //...
            }
        }
    }
}

依照約定的註冊

如果實現這些介面,則會自動將類註冊到依賴註入:

  • ITransientDependency 註冊為transient生命周期.
  • ISingletonDependency 註冊為singleton生命周期.
  • IScopedDependency 註冊為scoped生命周期.

預設特定類型

一些特定類型會預設註冊到依賴註入.例子:

  • 模塊類註冊為singleton.
  • MVC控制器(繼承Controller或AbpController)被註冊為transient.
  • MVC頁面模型(繼承PageModel或AbpPageModel)被註冊為transient.
  • MVC視圖組件(繼承ViewComponent或AbpViewComponent)被註冊為transient.
  • 應用程式服務(實現IApplicationService介面或繼承ApplicationService類)註冊為transient.
  • 存儲庫(實現IRepository介面)註冊為transient.
  • 域服務(實現IDomainService介面)註冊為transient.

手動註冊

在某些情況下,你可能需要向IServiceCollection手動註冊服務,尤其是在需要使用自定義工廠方法或singleton實例時.在這種情況下,你可以像Microsoft文檔描述的那樣直接添加服務.

public class BlogModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.services.AddTransient<ITestMicrosoftManager, TestMicrosoftManager>();
    }
}

如何使用

構造函數註入

  • 構造方法註入是將依賴項註入類的首選方式

屬性註入

  • Microsoft依賴註入庫不支持屬性註入.但是,ABP可以與第三方DI提供商(例如Autofac)集成,以實現屬性註入。
  • 屬性註入依賴項通常被視為可選依賴項.這意味著沒有它們,服務也可以正常工作.Logger就是這樣的依賴項,MyService可以繼續工作而無需日誌記錄.
public class MyService : ITransientDependency
{
    public ILogger<MyService> Logger { get; set; }

    public MyService()
    {
        Logger = NullLogger<MyService>.Instance;
    }

    public void DoSomething()
    {
        //...使用 Logger 寫日誌...
    }
}

IServiceProvider

直接從IServiceProvider解析服務.在這種情況下,你可以將IServiceProvider註入到你的類並使用

public class MyService : ITransientDependency
{
    private readonly IServiceProvider _serviceProvider;

    public MyService(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public void DoSomething()
    {
        var taxCalculator = _serviceProvider.GetService<ITaxCalculator>();
        //...
    }
}

服務替換

在某些情況下,需要替換某些介面的實現.

  • ITestManager有一個預設實現DefaultManager,但是我現在想替換成TestReplaceManager,該如何操作呢?

原生dotnet方式替換

services.Replace(ServiceDescriptor.Transient<ITestManager, TestReplaceManager>());

Abp支持

  • 加上Dependency特性標簽
  • 加上ExposeServices特性標簽
[Dependency(ReplaceServices = true)]
[ExposeServices(typeof(ITestManager))]
public class TestReplaceManager : ITestManager, ITransientDependency
{
	public void Print()
	{
		Console.WriteLine("TestReplaceManager");
	}
}

問題

  1. 有時候我們實現了ITransientDependency,ISingletonDependency,IScopedDependency但是再運行是還是提示依賴註入失敗?
  • 實現類的名稱拼寫錯誤
  • 比如介面名稱為ITestAppService,但是實現類為DefaultTessAppService,這個時候編譯不會報錯,但是運行報錯,下麵會基於源碼分析。
public class DefaultTessAppService : ApplicationService, ITestAppService
{
   // ....
}
  1. 我通過[Dependency(ReplaceServices = true)]替換服務沒有生效?
  • 請添加[ExposeServices(typeof(ITestManager))]顯示暴露服務,下麵會基於源碼分析。

源碼分析

  1. 進入到Startup.AddApplication源碼
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplication<xxxManagementHttpApiHostModule>();
    }
}
  1. 進入到await app.ConfigureServicesAsync()源碼
public async static Task<IAbpApplicationWithExternalServiceProvider> CreateAsync(
    [NotNull] Type startupModuleType,
    [NotNull] IServiceCollection services,
    Action<AbpApplicationCreationOptions>? optionsAction = null)
{
    var app = new AbpApplicationWithExternalServiceProvider(startupModuleType, services, options =>
    {
        options.SkipConfigureServices = true;
        optionsAction?.Invoke(options);
    });

    await app.ConfigureServicesAsync();
    return app;
}
  1. 主要查看ConfigureServices下的Services.AddAssembly(assembly)方法。
 public virtual async Task ConfigureServicesAsync()
    {
        
        // 省略...

        var assemblies = new HashSet<Assembly>();

        //ConfigureServices
        foreach (var module in Modules)
        {
            if (module.Instance is AbpModule abpModule)
            {
                if (!abpModule.SkipAutoServiceRegistration)
                {
                    var assembly = module.Type.Assembly;
                    if (!assemblies.Contains(assembly))
                    {
                        Services.AddAssembly(assembly);
                        assemblies.Add(assembly);
                    }
                }
            }

            try
            {
                await module.Instance.ConfigureServicesAsync(context);
            }
            catch (Exception ex)
            {
                throw new AbpInitializationException($"An error occurred during {nameof(IAbpModule.ConfigureServicesAsync)} phase of the module {module.Type.AssemblyQualifiedName}. See the inner exception for details.", ex);
            }
        }

        // 省略...
    }

4.進入下麵AddAssembly下AddType的邏輯

public class DefaultConventionalRegistrar : ConventionalRegistrarBase
{
    public override void AddType(IServiceCollection services, Type type)
    {
        if (IsConventionalRegistrationDisabled(type))
        {
            return;
        }
        // 查看是否有DependencyAttribute特性標簽
        var dependencyAttribute = GetDependencyAttributeOrNull(type);
        // 判斷是否有實現介面,註入對於的類型。
        var lifeTime = GetLifeTimeOrNull(type, dependencyAttribute);

        if (lifeTime == null)
        {
            return;
        }

        var exposedServiceTypes = GetExposedServiceTypes(type);

        TriggerServiceExposing(services, type, exposedServiceTypes);

        foreach (var exposedServiceType in exposedServiceTypes)
        {
            var serviceDescriptor = CreateServiceDescriptor(
                type,
                exposedServiceType,
                exposedServiceTypes,
                lifeTime.Value
            );

            if (dependencyAttribute?.ReplaceServices == true)
            {
                services.Replace(serviceDescriptor);
            }
            else if (dependencyAttribute?.TryRegister == true)
            {
                services.TryAdd(serviceDescriptor);
            }
            else
            {
                services.Add(serviceDescriptor);
            }
        }
    }
}

    // GetLifeTimeOrNull

    protected virtual ServiceLifetime? GetLifeTimeOrNull(Type type, DependencyAttribute? dependencyAttribute)
    {
        return dependencyAttribute?.Lifetime ?? GetServiceLifetimeFromClassHierarchy(type) ?? GetDefaultLifeTimeOrNull(type);
    }
    // abp 三個生命周期
    protected virtual ServiceLifetime? GetServiceLifetimeFromClassHierarchy(Type type)
    {
        if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type))
        {
            return ServiceLifetime.Transient;
        }

        if (typeof(ISingletonDependency).GetTypeInfo().IsAssignableFrom(type))
        {
            return ServiceLifetime.Singleton;
        }

        if (typeof(IScopedDependency).GetTypeInfo().IsAssignableFrom(type))
        {
            return ServiceLifetime.Scoped;
        }

        return null;
    }

5.重點到了,看下為什麼名稱錯誤為什麼導致註入失敗。

  • 通過介面的名稱去獲取實現。
  • 也能解釋有時候不顯示指定ExposeServices可能替換失敗的問題
public class ExposeServicesAttribute : Attribute, IExposedServiceTypesProvider
{
    // 省略...

    private static List<Type> GetDefaultServices(Type type)
    {
        var serviceTypes = new List<Type>();

        foreach (var interfaceType in type.GetTypeInfo().GetInterfaces())
        {
            var interfaceName = interfaceType.Name;
            if (interfaceType.IsGenericType)
            {
                interfaceName = interfaceType.Name.Left(interfaceType.Name.IndexOf('`'));
            }

            // 查詢到實現類的名稱是否是移除I
            if (interfaceName.StartsWith("I"))
            {
                interfaceName = interfaceName.Right(interfaceName.Length - 1);
            }
            // 查詢到實現類的名稱是否以介面名結尾
            if (type.Name.EndsWith(interfaceName))
            {
                serviceTypes.Add(interfaceType);
            }
        }

        return serviceTypes;
    }
}

Abp vNext Pro

如果覺得可以,不要吝嗇你的小星星哦

文章目錄


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

-Advertisement-
Play Games
更多相關文章
  • BeingDebugged 是`Windows`系統`PEB`結構體中的一個成員,它是一個標誌位,用於標識當前進程是否正在被調試。BeingDebugged的值為0表示當前進程未被調試,值為1表示當前進程正在被調試。由於`BeingDebugged`是在`PEB`結構體中存儲的,因此可以通過訪問`P... ...
  • 基於java線上婚紗定製系統設計與實現,可適用於線上婚紗攝影預定系統,基於web的婚紗影樓管理系統設計,基於web的婚紗影樓管理系統設計,婚紗攝影網系統,婚紗攝影網站系統,婚紗攝影網站系統,婚紗系統,婚紗管理系統等等; ...
  • EasyExcel動態表頭導出(支持多級表頭) 在很多業務場景中,都會應用到動態表頭的導出,也會涉及到多級表頭的導出,如下圖所示 通過EasyExcel,我們可以快速實現這一需求,具體代碼如下 DynamicHeader import java.util.List; /** *@Author: <a ...
  • 1章:系統基礎信息模塊詳解 通過第三方模塊獲取伺服器的基本性能、塊設備、網卡介面、網路地址庫等信息。 1.1 系統性能模塊psutil:獲取系統性能信息、記憶體信息、磁碟信息、網路信息、用戶信息等。 1.2 IP地址處理模塊IPy: 處理IP地址,網段等。 1.3 DNS處理模塊dnspython: ...
  • 在工具類中封裝getBean,使用哪個介面來實現 實事上,在工具類中,實現BeanFactoryPostProcessor和ApplicationContextAware介面後,使用它們構造方法里的對象ConfigurableListableBeanFactory和ApplicationContex ...
  • 通過strimzi部署的kafka集群,如何部署prometheus+grafana去監控呢?官方文檔信息量太大,即便照著做也可能失敗,這裡有一份詳細的保姆級操作指南,助您成功部署監控服務 ...
  • 模擬.NET實際應用場景,綜合應用三個主要知識點:一是使用dnSpy反編譯第三庫及調試,二是使用Lib.Harmony庫實現第三庫攔截、偽造,三是實現同一個庫支持多版本同時引用。 ...
  • 前言 外觀模式,英文名稱是:Facade Pattern。我們先從名字上來理解一下“外觀模式”。我看到了“外觀”這個詞語,就想到了“外表”這個詞語,兩者有著很相近的意思。就拿談戀愛來說,“外表”很重要,如果第一眼看著很舒服、有眼緣,那就有交往下去的可能。如果長的“三寸釘、枯樹皮”,估計就夠嗆了。在這 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...