.Net依賴註入神器Scrutor(上)

来源:https://www.cnblogs.com/ruipeng/p/18081965
-Advertisement-
Play Games

前言 從.Net Core 開始,.Net 平臺內置了一個輕量,易用的 IOC 的框架,供我們在應用程式中使用,社區內還有很多強大的第三方的依賴註入框架如: Autofac DryIOC Grace LightInject Lamar Stashbox Simple Injector 內置的依賴註入 ...


前言

從.Net Core 開始,.Net 平臺內置了一個輕量,易用的 IOC 的框架,供我們在應用程式中使用,社區內還有很多強大的第三方的依賴註入框架如:

內置的依賴註入容器基本可以滿足大多數應用的需求,除非你需要的特定功能不受它支持否則不建議使用第三方的容器。

我們今天介紹的主角Scrutor是內置依賴註入的一個強大的擴展,Scrutor有兩個核心的功能:一是程式集的批量註入 Scanning,二是 Decoration 裝飾器模式,今天的主題是Scanning

開始之前在項目中安裝 nuget 包:

Install-Package Scrutor

學習Scrutor前我們先熟悉一個.Net依賴註入的萬能用法。

builder.Services.Add(
    new ServiceDescriptor(/*"ServiceType"*/typeof(ISampleService), /*"implementationType"*/typeof(SampleService), ServiceLifetime.Transient)
    );

第一個參數ServiceType通常用介面表示,第二個implementationType介面的實現,最後生命周期,熟悉了這個後面的邏輯理解起來就容易些。

Scrutor官方倉庫和本文完整的源代碼在文末

Scanning

Scrutor提供了一個IServiceCollection的擴展方法作為批量註入的入口,該方法提供了Action<ITypeSourceSelector>委托參數。

builder.Services.Scan(typeSourceSelector => { });

我們所有的配置都是在這個委托內完成的,Setup by Setup 剖析一下這個使用過程。

第一步 獲取 types

typeSourceSelector 支持程式集反射獲取類型和提供類型參數

程式集選擇

ITypeSourceSelector有多種獲取程式集的方法來簡化我們選擇程式集


 typeSourceSelector.FromAssemblyOf<Program>();//根據泛型反射獲取所在的程式集

typeSourceSelector.FromCallingAssembly();//獲取開始發起調用方法的程式集

typeSourceSelector.FromEntryAssembly();//獲取應用程式入口點所在的程式集

typeSourceSelector.FromApplicationDependencies();//獲取應用程式及其依賴項的程式集

typeSourceSelector.FromDependencyContext(DependencyContext.Default);//根據依賴關係上下文(DependencyContext)中的運行時庫(Runtime Library)列表。它返回一個包含了所有運行時庫信息的集合。

typeSourceSelector.FromAssembliesOf(typeof(Program));//根據類型獲取程式集的集合

typeSourceSelector.FromAssemblies(Assembly.Load("dotNetParadise-Scrutor.dll"));//提供程式集支持Params或者IEnumerable

第二步 從 Types 中選擇 ImplementationType

簡而言之就是從程式中獲取的所有的 types 進行過濾,比如獲取的 ImplementationType 必須是非抽象的,是類,是否只需要 Public等,還可以用 ImplementationTypeFilter 提供的擴展方法等


builder.Services.Scan(typeSourceSelector =>
{
    typeSourceSelector.FromEntryAssembly().AddClasses();
});

AddClasses()方法預設獲取所有公開非抽象的類


還可以通過 AddClasses 的委托參數來進行更多條件的過濾
比如定義一個 Attribute,忽略IgnoreInjectAttribute

namespace dotNetParadise_Scrutor;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class IgnoreInjectAttribute : Attribute
{
}
builder.Services.Scan(typeSourceSelector =>
{
    typeSourceSelector.FromEntryAssembly().AddClasses(iImplementationTypeFilter =>
    {
        iImplementationTypeFilter.WithoutAttribute<IgnoreInjectAttribute>();
    });
});

利用 iImplementationTypeFilter 的擴展方法很簡單就可以實現

在比如 我只要想實現IApplicationService介面的類才可以被註入

namespace dotNetParadise_Scrutor;

/// <summary>
/// 依賴註入標記介面
/// </summary>
public interface IApplicationService
{
}
builder.Services.Scan(typeSourceSelector =>
{
    typeSourceSelector.FromEntryAssembly().AddClasses(iImplementationTypeFilter =>
    {
        iImplementationTypeFilter.WithoutAttribute<IgnoreInjectAttribute>().AssignableTo<IApplicationService>();
    });
});

類似功能還有很多,如可以根據命名空間也可以根據Type的屬性用lambda表達式對ImplementationType進行過濾


上面的一波操作實際上就是為了構造一個IServiceTypeSelector對象,選出來的ImplementationType對象保存了到了ServiceTypeSelectorTypes屬性中供下一步選擇。
除了提供程式集的方式外還可以直接提供類型的方式比如

創建介面和實現

public interface IForTypeService
{
}
public class ForTypeService : IForTypeService
{
}
builder.Services.Scan(typeSourceSelector =>
{
    typeSourceSelector.FromTypes(typeof(ForTypeService));
});

這種方式提供類型內部會調用AddClass()方法把符合條件的參數保存到ServiceTypeSelector

第三步確定註冊策略

AddClass之後可以調用UsingRegistrationStrategy()配置註冊策略是 AppendSkipThrowReplace
下麵是各個模式的詳細解釋

  • RegistrationStrategy.Append :類似於builder.Services.Add
  • RegistrationStrategy.Skip:類似於builder.Services.TryAdd
  • RegistrationStrategy.Throw:ServiceDescriptor 重覆則跑異常
  • RegistrationStrategy.Replace: 替換原有服務

這樣可以靈活地控制註冊流程

    builder.Services.Scan(typeSourceSelector =>
    {
        typeSourceSelector.FromEntryAssembly().AddClasses().UsingRegistrationStrategy(RegistrationStrategy.Skip);
    });

不指定則為預設的 Append 即 builder.Services.Add

第四步 配置註冊的場景選擇合適的ServiceType

ServiceTypeSelector提供了多種方法讓我們從ImplementationType中匹配ServiceType

  • AsSelf()
  • As<T>()
  • As(params Type[] types)
  • As(IEnumerable<Type> types)
  • AsImplementedInterfaces()
  • AsImplementedInterfaces(Func<Type, bool> predicate)
  • AsSelfWithInterfaces()
  • AsSelfWithInterfaces(Func<Type, bool> predicate)
  • AsMatchingInterface()
  • AsMatchingInterface(Action<Type, IImplementationTypeFilter>? action)
  • As(Func<Type, IEnumerable<Type>> selector)
  • UsingAttributes()

AsSelf 註冊自身

public class AsSelfService
{
}
{
    builder.Services.Scan(typeSourceSelector =>
    {
        typeSourceSelector.FromEntryAssembly().AddClasses(iImplementationTypeFilter =>
        {
            iImplementationTypeFilter.InNamespaces("dotNetParadise_Scrutor.Application.AsSelf").WithoutAttribute<IgnoreInjectAttribute>();
        }).AsSelf();
    });

    Debug.Assert(builder.Services.Any(_ => _.ServiceType == typeof(AsSelfService)));
}

等效於builder.Services.AddTransient<AsSelfService>();


As 批量為 ImplementationType 指定 ServiceType

public interface IAsService
{
}
public class AsOneService : IAsService
{
}
public class AsTwoService : IAsService
{
}
{
    builder.Services.Scan(typeSourceSelector =>
{
    typeSourceSelector.FromEntryAssembly().AddClasses(iImplementationTypeFilter =>
    {
        iImplementationTypeFilter.InNamespaces("dotNetParadise_Scrutor.Application.As").WithoutAttribute<IgnoreInjectAttribute>();
    }).As<IAsService>();
});
    Debug.Assert(builder.Services.Any(_ => _.ServiceType == typeof(IAsService)));
    foreach (var asService in builder.Services.Where(_ => _.ServiceType == typeof(IAsService)))
    {
        Debug.WriteLine(asService.ImplementationType!.Name);
    }
}

As(params Type[] types)和 As(IEnumerable types) 批量為ImplementationType指定多個 ServiceType,服務必須同時實現這裡面的所有的介面

上面的實例再改進一下

public interface IAsOtherService
{
}
public interface IAsSomeService
{
}

public class AsOneMoreTypesService : IAsOtherService, IAsSomeService
{
}
public class AsTwoMoreTypesService : IAsSomeService, IAsOtherService
{
}

{
    builder.Services.Scan(typeSourceSelector =>
    {
        typeSourceSelector.FromEntryAssembly().AddClasses(iImplementationTypeFilter =>
        {
            iImplementationTypeFilter.InNamespaces("dotNetParadise_Scrutor.Application.AsMoreTypes").WithoutAttribute<IgnoreInjectAttribute>();
        }).As(typeof(IAsSomeService), typeof(IAsOtherService));
    });
    List<Type> serviceTypes = [typeof(IAsSomeService), typeof(IAsOtherService)];
    Debug.Assert(serviceTypes.All(serviceType => builder.Services.Any(service => service.ServiceType == serviceType)));
    foreach (var asService in builder.Services.Where(_ => _.ServiceType == typeof(IAsSomeService) || _.ServiceType == typeof(IAsOtherService)))
    {
        Debug.WriteLine(asService.ImplementationType!.Name);
    }
}

AsImplementedInterfaces 註冊當前 ImplementationType 和實現的介面

public interface IAsImplementedInterfacesService
{
}
public class AsImplementedInterfacesService : IAsImplementedInterfacesService
{
}
//AsImplementedInterfaces 註冊當前ImplementationType和它實現的介面
{
    builder.Services.Scan(typeSourceSelector =>
    {
        typeSourceSelector.FromEntryAssembly().AddClasses(iImplementationTypeFilter =>
        {
            iImplementationTypeFilter.InNamespaces("dotNetParadise_Scrutor.Application.AsImplementedInterfaces").WithoutAttribute<IgnoreInjectAttribute>();
        }).AsImplementedInterfaces();
    });

    Debug.Assert(builder.Services.Any(service => service.ServiceType == typeof(IAsImplementedInterfacesService)));
    foreach (var asService in builder.Services.Where(_ => _.ServiceType == typeof(IAsImplementedInterfacesService)))
    {
        Debug.WriteLine(asService.ImplementationType!.Name);
    }
}

AsSelfWithInterfaces 同時註冊為自身類型和所有實現的介面

public interface IAsSelfWithInterfacesService
{
}
public class AsSelfWithInterfacesService : IAsSelfWithInterfacesService
{
}

{
    builder.Services.Scan(typeSourceSelector =>
    {
        typeSourceSelector.FromEntryAssembly().AddClasses(iImplementationTypeFilter =>
        {
            iImplementationTypeFilter.InNamespaces("dotNetParadise_Scrutor.Application.AsSelfWithInterfaces").WithoutAttribute<IgnoreInjectAttribute>();
        }).AsSelfWithInterfaces();
    });
    //Self
    Debug.Assert(builder.Services.Any(service => service.ServiceType == typeof(AsSelfWithInterfacesService)));
    //Interfaces
    Debug.Assert(builder.Services.Any(service => service.ServiceType == typeof(IAsSelfWithInterfacesService)));
    foreach (var service in builder.Services.Where(_ => _.ServiceType == typeof(AsSelfWithInterfacesService) || _.ServiceType == typeof(IAsSelfWithInterfacesService)))
    {
        Debug.WriteLine(service.ServiceType!.Name);
    }
}

AsMatchingInterface 將服務註冊為與其命名相匹配的介面,可以理解為一定約定假如服務名稱為 ClassName,會找 IClassName 的介面作為 ServiceType 註冊

public interface IAsMatchingInterfaceService
{
}
public class AsMatchingInterfaceService : IAsMatchingInterfaceService
{
}
//AsMatchingInterface 將服務註冊為與其命名相匹配的介面,可以理解為一定約定假如服務名稱為 ClassName,會找 IClassName 的介面作為 ServiceType 註冊
{
    builder.Services.Scan(typeSourceSelector =>
    {
        typeSourceSelector.FromEntryAssembly().AddClasses(iImplementationTypeFilter =>
        {
            iImplementationTypeFilter.InNamespaces("dotNetParadise_Scrutor.Application.AsMatchingInterface").WithoutAttribute<IgnoreInjectAttribute>();
        }).AsMatchingInterface();
    });
    Debug.Assert(builder.Services.Any(service => service.ServiceType == typeof(IAsMatchingInterfaceService)));
    foreach (var service in builder.Services.Where(_ => _.ServiceType == typeof(IAsMatchingInterfaceService)))
    {
        Debug.WriteLine(service.ServiceType!.Name);
    }
}

UsingAttributes 特性註入,這個還是很實用的在Scrutor提供了ServiceDescriptorAttribute來幫助我們方便的對Class進行標記方便註入

public interface IUsingAttributesService
{
}

[ServiceDescriptor<IUsingAttributesService>()]
public class UsingAttributesService : IUsingAttributesService
{
}
    builder.Services.Scan(typeSourceSelector =>
    {
        typeSourceSelector.FromEntryAssembly().AddClasses(iImplementationTypeFilter =>
        {
            iImplementationTypeFilter.InNamespaces("dotNetParadise_Scrutor.Application.UsingAttributes").WithoutAttribute<IgnoreInjectAttribute>();
        }).UsingAttributes();
    });
    Debug.Assert(builder.Services.Any(service => service.ServiceType == typeof(IUsingAttributesService)));
    foreach (var service in builder.Services.Where(_ => _.ServiceType == typeof(IUsingAttributesService)))
    {
        Debug.WriteLine(service.ServiceType!.Name);
    }

第五步 配置生命周期

通過鏈式調用WithLifetime函數來確定我們的生命周期,預設是 Transient

public interface IFullService
{
}
public class FullService : IFullService
{
}
{

    builder.Services.Scan(typeSourceSelector =>
    {
        typeSourceSelector.FromEntryAssembly().AddClasses(iImplementationTypeFilter =>
        {
            iImplementationTypeFilter.InNamespaces("dotNetParadise_Scrutor.Application.Full");
        }).UsingRegistrationStrategy(RegistrationStrategy.Skip).AsImplementedInterfaces().WithLifetime(ServiceLifetime.Scoped);
    });

    Debug.Assert(builder.Services.Any(service => service.ServiceType == typeof(IFullService)));
    foreach (var service in builder.Services.Where(_ => _.ServiceType == typeof(IFullService)))
    {
        Debug.WriteLine($"serviceType:{service.ServiceType!.Name},LifeTime:{service.Lifetime}");
    }
}

總結

到這兒基本的功能已經介紹完了,可以看出來擴展方法很多,基本可以滿足開發過程批量依賴註入的大部分場景。
使用技巧總結:

  • 根據程式集獲取所有的類型 此時 Scrutor 會返回一個 IImplementationTypeSelector 對象裡面包含了程式集的所有類型集合
  • 調用 IImplementationTypeSelectorAddClasses 方法獲取 IServiceTypeSelector 對象,AddClass 這裡面可以根據條件選擇 過濾一些不需要的類型
  • 調用UsingRegistrationStrategy確定依賴註入的策略 是覆蓋 還是跳過亦或是拋出異常 預設 Append 追加註入的方式
  • 配置註冊的場景 比如是 AsImplementedInterfaces 還是 AsSelf
  • 選擇生命周期 預設 Transient

藉助ServiceDescriptorAttribute更簡單,生命周期和ServiceType都是在Attribute指定好的只需要確定選擇程式集,調用UsingRegistrationStrategy配置依賴註入的策略然後UsingAttributes()即可

最後

本文從Scrutor的使用流程剖析了依賴註入批量註冊的流程,更詳細的教程可以參考Github 官方倉庫。在開發過程中看到很多項目還有一個個手動註入的,也有自己寫 Interface或者是Attribute反射註入的,支持的場景都十分有限,Scrutor的出現就是為了避免我們在項目中不停地造輪子,達到開箱即用的目的。

本文完整示例源代碼

本文來自博客園,作者:董瑞鵬,轉載請註明原文鏈接:https://www.cnblogs.com/ruipeng/p/18081965


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

-Advertisement-
Play Games
更多相關文章
  • 基於Js和Java+MyBatis實現xlsx\xls文檔的導入下載、導出 背景: ​ 實現xlsx\xls文檔的導入、導出 ​ 導入效果: ​ 導出效果: 導出效果圖 1、導入、下載 1.1、前臺 <div style="margin-left: 15px"> <input type="file" ...
  • 本文介紹基於Python中ArcPy模塊,讀取Excel表格數據並生成帶有屬性表的矢量要素圖層,同時配置該圖層的坐標系的方法~ ...
  • 前言 還有個迭代器,基礎語法基本已經說完了,後面想到啥再補充,之後的教程會從以下方面來講: 基礎庫的使用,比如string、table等 基礎控制項的使用,比如listview、tab等 aardio和Python交互,比如給Python寫個界面 自帶的範常式序 我寫的一些小程式 當然,我的理解也是很 ...
  • 在.NET中Newtonsoft.Json(Json.NET)是我們常用來進行Json序列化與反序列化的庫。 而在使用中常會遇到反序列化Json時,遇到不規則的Json數據解構而拋出異常。 Newtonsoft.Json 支持序列化和反序列化過程中的錯誤處理。 允許您捕獲錯誤並選擇是處理它並繼續序列 ...
  • 作者引言 很高興啊,我們來到了第一篇,程式員的HelloWorld,快速開始RPC之游 快速入門 演示如何在幾分鐘內,使用IceRPC,構建和運行一個完整的客戶端-伺服器(C/S)應用程式. 必要條件: 只要電腦安裝 .NET 8 SDK 就行了. 來吧,開始你的RPC之旅 接下來,我們要一起構建一 ...
  • 攔截器Interceptors是一種可以在編譯時以聲明方式替換原有應用的方法。 這種替換是通過讓Interceptors聲明它攔截的調用的源位置來實現的。 您可以使用攔截器作為源生成器的一部分進行修改,而不是向現有源編譯添加代碼。 演示 使用 .NET 8 創建一個控制台應用程式。併在Propert ...
  • 概述:C#中整數除法返回整數,維護與低級語言相容性,提高性能。雖然精度有損,但可通過顯式浮點數轉換實現小數保留。 在C#中,整數除法返回整數而不是浮點數,這是為了保持與低級語言(如C和C++)的相容性,同時提高性能和降低複雜性。這種設計使得整數之間的除法操作更加高效,但可能導致精度喪失。 基礎功能: ...
  • Linq的學習 這裡繼續使用之前文章創建的學生類,首先簡單介紹一下linq的使用。 Student.cs public class Student { public int Id { get; set; } public int ClassId { get; set; } public string ...
一周排行
    -Advertisement-
    Play Games
  • C#TMS系統代碼-基礎頁面BaseCity學習 本人純新手,剛進公司跟領導報道,我說我是java全棧,他問我會不會C#,我說大學學過,他說這個TMS系統就給你來管了。外包已經把代碼給我了,這幾天先把增刪改查的代碼背一下,說不定後面就要趕鴨子上架了 Service頁面 //using => impo ...
  • 委托與事件 委托 委托的定義 委托是C#中的一種類型,用於存儲對方法的引用。它允許將方法作為參數傳遞給其他方法,實現回調、事件處理和動態調用等功能。通俗來講,就是委托包含方法的記憶體地址,方法匹配與委托相同的簽名,因此通過使用正確的參數類型來調用方法。 委托的特性 引用方法:委托允許存儲對方法的引用, ...
  • 前言 這幾天閑來沒事看看ABP vNext的文檔和源碼,關於關於依賴註入(屬性註入)這塊兒產生了興趣。 我們都知道。Volo.ABP 依賴註入容器使用了第三方組件Autofac實現的。有三種註入方式,構造函數註入和方法註入和屬性註入。 ABP的屬性註入原則參考如下: 這時候我就開始疑惑了,因為我知道 ...
  • C#TMS系統代碼-業務頁面ShippingNotice學習 學一個業務頁面,ok,領導開完會就被裁掉了,很突然啊,他收拾東西的時候我還以為他要旅游提前請假了,還在尋思為什麼回家連自己買的幾箱飲料都要叫跑腿帶走,怕被偷嗎?還好我在他開會之前拿了兩瓶芬達 感覺感覺前面的BaseCity差不太多,這邊的 ...
  • 概述:在C#中,通過`Expression`類、`AndAlso`和`OrElse`方法可組合兩個`Expression<Func<T, bool>>`,實現多條件動態查詢。通過創建表達式樹,可輕鬆構建複雜的查詢條件。 在C#中,可以使用AndAlso和OrElse方法組合兩個Expression< ...
  • 閑來無聊在我的Biwen.QuickApi中實現一下極簡的事件匯流排,其實代碼還是蠻簡單的,對於初學者可能有些幫助 就貼出來,有什麼不足的地方也歡迎板磚交流~ 首先定義一個事件約定的空介面 public interface IEvent{} 然後定義事件訂閱者介面 public interface I ...
  • 1. 案例 成某三甲醫預約系統, 該項目在2024年初進行上線測試,在正常運行了兩天後,業務系統報錯:The connection pool has been exhausted, either raise MaxPoolSize (currently 800) or Timeout (curren ...
  • 背景 我們有些工具在 Web 版中已經有了很好的實踐,而在 WPF 中重新開發也是一種費時費力的操作,那麼直接集成則是最省事省力的方法了。 思路解釋 為什麼要使用 WPF?莫問為什麼,老 C# 開發的堅持,另外因為 Windows 上已經裝了 Webview2/edge 整體打包比 electron ...
  • EDP是一套集組織架構,許可權框架【功能許可權,操作許可權,數據訪問許可權,WebApi許可權】,自動化日誌,動態Interface,WebApi管理等基礎功能於一體的,基於.net的企業應用開發框架。通過友好的編碼方式實現數據行、列許可權的管控。 ...
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...