.Net Core根据文件名称自动注入服务

.Net Core根据文件名称自动注入服务

说明

这个适用于.Net Core 的Web项目,且需要在服务中注入接口的需求.因为之前些Java Web习惯了,所以会有Dao层,Serivce层和Controller层.但是如果一个项目里面对于不同的数据库会有多个Dao,如果一个一个引入会造成代码.可读性变差.使用以下方法可以根据文件命名来提取固定的接口与实现类,并自动注入 到服务中.

分析逻辑

首先知道注入接口的方法为:

csharp 复制代码
  services.AddScoped(interfaceType, type);

通过映射的方法扫描项目中所有文件命名(如果怕效率低也可以只扫描制定目录下文件),我这里定义接口以Dao结尾,实现类以DaoImpl结尾,以此定义两个变量:

csharp 复制代码
 var interfaceSuffix = "Dao";
 var implementationSuffix = "DaoImpl";

然后根据检索文件名称,拿到接口与实现类的列表:

csharp 复制代码
var interfaceTypes = assembly.GetTypes().Where(t => t.IsInterface && t.Name.EndsWith(interfaceSuffix)).ToArray();

var types = assembly.GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.Name.EndsWith(implementationSuffix)).ToList();

再将两个list结合,批量写入:

csharp 复制代码
            foreach (var interfaceType in interfaceTypes)
            {
                foreach (var type in types)
                {
                    var interfaceName = type.GetInterfaces()
                                            .FirstOrDefault(i => i.Name == interfaceType.Name)
                                            ?.Name;

                    if (interfaceName != null)
                    {
                        services.AddScoped(interfaceType, type);
                    }
                }
            }

所有代码

csharp 复制代码
public static class ServiceCollectionExtensions
{
   
    public static IServiceCollection AddDaosWithConvention(this IServiceCollection services, Assembly assembly)
    {

        var interfaceSuffix = "Dao";
        var implementationSuffix = "DaoImpl";

        var interfaceTypes = assembly.GetTypes()
                                     .Where(t => t.IsInterface && t.Name.EndsWith(interfaceSuffix))
                                     .ToArray();

        var types = assembly.GetTypes()
                            .Where(t => t.IsClass && !t.IsAbstract && t.Name.EndsWith(implementationSuffix))
                            .ToList();

        foreach (var interfaceType in interfaceTypes)
        {
            foreach (var type in types)
            {
                var interfaceName = type.GetInterfaces()
                                        .FirstOrDefault(i => i.Name == interfaceType.Name)
                                        ?.Name;

                if (interfaceName != null)
                {
                    services.AddScoped(interfaceType, type);
                }
            }
        }

        return services;
    }
}

一键注入

csharp 复制代码
builder.Services.AddDaosWithConvention(Assembly.GetExecutingAssembly());
相关推荐
csdn_aspnet4 天前
MongoDB C# .NetCore 驱动程序 序列化忽略属性
mongodb·c#·.netcore
Tiger_shl5 天前
【.Net技术栈梳理】08-控制反转(IoC)与依赖注入(DI)
开发语言·.net·.netcore
Tiger_shl5 天前
【.Net技术栈梳理】10-.NET Core 程序的执行
开发语言·.net·.netcore
MoFe16 天前
【.net core】【watercloud】登陆后跳转至指定页面,显示在系统框架页内
.netcore
周杰伦fans6 天前
.net core webapi/mvc阿里云服务器部署 - 错误解决
阿里云·mvc·.netcore
驾驭人生8 天前
Asp .Net Core 系列:Asp .Net Core 集成 Hangfire+MySQL
数据库·mysql·.netcore
时光追逐者8 天前
C#/.NET/.NET Core技术前沿周刊 | 第 53 期(2025年9.1-9.7)
c#·.net·.netcore
somethingGoWay9 天前
wpf .netcore 导出docx文件
wpf·.netcore
somethingGoWay9 天前
wpf .netcore 导出pdf文件
pdf·wpf·.netcore
切糕师学AI10 天前
如何建立针对 .NET Core web 程序的线程池的长期监控
java·前端·.netcore