.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());
相关推荐
weixin_421994783 天前
认识数据 - 变量与数据类型
c#·.net·.netcore
老龄程序员3 天前
记一次由于.netcore程序堆栈溢出的问题分析
.netcore
一然明月4 天前
.NET Core基础
.netcore
罗马苏丹默罕默德9 天前
Ubuntu下部署.NetCore WebApi的方法
数据库·ubuntu·.netcore
lingxiao1688811 天前
WebApi详解+Unity注入--中篇:.net core的WebAPI
unity·c#·.netcore
老龄程序员11 天前
基于OpenIddict6.4.0搭建的授权UI管理界面
.netcore
武藤一雄12 天前
C# 关于多线程如何实现需要注意的问题(持续更新)
windows·后端·microsoft·c#·.net·.netcore·死锁
冰茶_12 天前
WPF路由事件:隧道与冒泡机制解析
学习·c#·.net·wpf·.netcore·mvvm
武藤一雄12 天前
C# 关于GC垃圾回收需要注意的问题(持续更新)
后端·微软·c#·.net·.netcore
武藤一雄12 天前
C# 关于应用程序域(AppDomain)需要注意的问题(持续更新)
后端·microsoft·微软·c#·.net·.netcore