自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

精準掌握.NET依賴注入:DI自動注冊服務輕松搞定

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

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

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

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

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

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

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

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

// 實現(xiàn)類3,實現(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)建服務集合
        var services = new ServiceCollection();

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

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

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

        // 構建服務提供程序
        var serviceProvider = services.BuildServiceProvider();

        // 步驟7:使用注冊的服務
        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
    {
        // 獲取當前程序集
        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àn)的類(項目下的所有接口)
    static void RegisterAllImplementationsOfInterfaces(IServiceCollection services)
    {
        // 獲取當前程序集
        var assembly = Assembly.GetExecutingAssembly();

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

        // 獲取實現(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特性的類。這里使用了反射機制來動態(tài)獲取所有帶有指定特性的類的類型,并將它們注冊到DI容器中。
  • 使用RegisterAllImplementationsOfInterfaces方法注冊了項目下所有實現(xiàn)接口的類。

請確保在項目中引用了Microsoft.Extensions.DependencyInjection相關的包。這是一個基本的示例,實際應用中可能需要更復雜的配置,具體取決于項目的需求。

責任編輯:姜華 來源: 今日頭條
相關推薦

2024-12-30 12:00:00

.NET Core依賴注入屬性注入

2024-11-27 00:24:04

2009-11-12 10:32:47

ADO.NET技術

2009-11-12 10:53:57

ADO.NET連接My

2023-06-28 08:16:50

Autofac應用程序

2010-01-13 17:47:59

VB.NET拖放

2010-01-14 13:59:01

2025-01-07 08:55:54

2024-06-12 00:00:01

Java函數(shù)式接口

2010-01-14 10:07:08

VB.NET文件名排序

2010-01-18 19:36:52

VB.NET調整控件

2010-10-22 11:31:53

SQL Server自

2009-11-24 15:34:41

DNS服務器組建

2024-08-26 08:27:18

2010-01-11 18:40:03

VB.NET操作注冊表

2018-03-12 10:02:30

PHP依賴注入

2024-02-26 00:04:00

代碼zip()開發(fā)

2009-02-16 15:35:00

2025-02-17 00:00:55

NET開發(fā)依賴注入

2024-06-17 10:28:25

.NETXML 文件
點贊
收藏

51CTO技術棧公眾號