偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

精準(zhǔn)掌握.NET依賴注入:DI自動注冊服務(wù)輕松搞定

開發(fā) 后端
在.NET中,進(jìn)行依賴注入(DI)的自動注冊,可以通過反射機(jī)制和程序集掃描來實(shí)現(xiàn)。以下是詳細(xì)的步驟以及相應(yīng)的C#源代碼示例,包括注冊指定類、注冊帶有自定義特性的類、以及注冊項(xiàng)目下所有帶有接口實(shí)現(xiàn)的類(項(xiàng)目下的所有接口)。

概述:.NET依賴注入(DI)通過反射自動注冊服務(wù),示例展示了注冊指定類、帶特性類、項(xiàng)目下所有接口實(shí)現(xiàn)的類。簡化配置,提高可維護(hù)性。

在.NET中,進(jìn)行依賴注入(DI)的自動注冊,可以通過反射機(jī)制和程序集掃描來實(shí)現(xiàn)。以下是詳細(xì)的步驟以及相應(yīng)的C#源代碼示例,包括注冊指定類、注冊帶有自定義特性的類、以及注冊項(xiàng)目下所有帶有接口實(shí)現(xiàn)的類(項(xiàng)目下的所有接口):

步驟1:創(chuàng)建接口和實(shí)現(xiàn)類

// 接口1
public interface IService1
{
    void PerformService1();
}

// 接口2
public interface IService2
{
    void PerformService2();
}

// 實(shí)現(xiàn)類1,實(shí)現(xiàn)IService1
public class MyService1 : IService1
{
    public void PerformService1()
    {
        Console.WriteLine("Service 1 performed.");
    }
}

// 實(shí)現(xiàn)類2,實(shí)現(xiàn)IService2
[CustomRegistration] // 帶有自定義特性
public class MyService2 : IService2
{
    public void PerformService2()
    {
        Console.WriteLine("Service 2 performed.");
    }
}

// 實(shí)現(xiàn)類3,實(shí)現(xiàn)IService1和IService2
public class MyService3 : IService1, IService2
{
    public void PerformService1()
    {
        Console.WriteLine("Service 3 (Service 1 part) performed.");
    }

    public void PerformService2()
    {
        Console.WriteLine("Service 3 (Service 2 part) performed.");
    }
}

步驟2:創(chuàng)建自定義特性

// 自定義特性
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
sealed class CustomRegistrationAttribute : Attribute
{
}

步驟3:創(chuàng)建自動注冊方法

using System;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

class Program
{
    static void Main()
    {
        // 創(chuàng)建服務(wù)集合
        var services = new ServiceCollection();

        // 步驟4:注冊指定類
        services.AddTransient<MyService1>();

        // 步驟5:注冊帶有自定義特性的類
        RegisterClassesWithAttribute<CustomRegistrationAttribute>(services);

        // 步驟6:注冊項(xiàng)目下所有帶有接口實(shí)現(xiàn)的類(項(xiàng)目下的所有接口)
        RegisterAllImplementationsOfInterfaces(services);

        // 構(gòu)建服務(wù)提供程序
        var serviceProvider = services.BuildServiceProvider();

        // 步驟7:使用注冊的服務(wù)
        var myService1 = serviceProvider.GetService<MyService1>();
        myService1.PerformService1();

        var myService2 = serviceProvider.GetService<MyService2>();
        myService2.PerformService2();

        var myService3 = serviceProvider.GetService<MyService3>();
        myService3.PerformService1();
        myService3.PerformService2();
    }

    // 自動注冊帶有指定特性的類
    static void RegisterClassesWithAttribute<TAttribute>(IServiceCollection services)
        where TAttribute : Attribute
    {
        // 獲取當(dāng)前程序集
        var assembly = Assembly.GetExecutingAssembly();

        // 獲取帶有指定特性的所有類
        var attributedTypes = assembly.GetTypes()
            .Where(type => type.GetCustomAttributes(typeof(TAttribute), true).Any() && type.IsClass);

        // 注冊這些類
        foreach (var attributedType in attributedTypes)
        {
            services.AddTransient(attributedType);
        }
    }

    // 自動注冊項(xiàng)目下所有帶有接口實(shí)現(xiàn)的類(項(xiàng)目下的所有接口)
    static void RegisterAllImplementationsOfInterfaces(IServiceCollection services)
    {
        // 獲取當(dāng)前程序集
        var assembly = Assembly.GetExecutingAssembly();

        // 獲取項(xiàng)目下所有接口
        var interfaceTypes = assembly.GetTypes()
            .Where(type => type.IsInterface);

        // 獲取實(shí)現(xiàn)了這些接口的所有類
        var implementationTypes = assembly.GetTypes()
            .Where(type => interfaceTypes.Any(interfaceType => interfaceType.IsAssignableFrom(type)) && type.IsClass);

        // 注冊這些類
        foreach (var implementationType in implementationTypes)
        {
            services.AddTransient(implementationType);
        }
    }
}

在上述代碼中:

  • 使用AddTransient方法注冊了特定的MyService1類。
  • 使用RegisterClassesWithAttribute方法注冊了帶有CustomRegistrationAttribute特性的類。這里使用了反射機(jī)制來動態(tài)獲取所有帶有指定特性的類的類型,并將它們注冊到DI容器中。
  • 使用RegisterAllImplementationsOfInterfaces方法注冊了項(xiàng)目下所有實(shí)現(xiàn)接口的類。

請確保在項(xiàng)目中引用了Microsoft.Extensions.DependencyInjection相關(guān)的包。這是一個基本的示例,實(shí)際應(yīng)用中可能需要更復(fù)雜的配置,具體取決于項(xiàng)目的需求。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2024-12-30 12:00:00

.NET Core依賴注入屬性注入

2024-11-27 00:24:04

2009-11-12 10:32:47

ADO.NET技術(shù)

2009-11-12 10:53:57

ADO.NET連接My

2010-01-14 13:59:01

2010-01-13 17:47:59

VB.NET拖放

2025-01-07 08:55:54

2024-06-12 00:00:01

Java函數(shù)式接口

2010-01-18 19:36:52

VB.NET調(diào)整控件

2010-01-14 10:07:08

VB.NET文件名排序

2023-06-28 08:16:50

Autofac應(yīng)用程序

2010-10-22 11:31:53

SQL Server自

2009-11-24 15:34:41

DNS服務(wù)器組建

2024-08-26 08:27:18

2010-01-11 18:40:03

VB.NET操作注冊表

2009-02-16 15:35:00

2024-02-26 00:04:00

代碼zip()開發(fā)

2017-05-11 15:01:43

Androidweb布局

2009-12-11 15:37:58

Linux日志處理

2024-04-15 07:00:00

Python開發(fā)Hatch
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號