.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_aspnet2 天前
.NetCore Webapi 接口验证请求来源
.netcore·ip·token·签名·webapi·cors·sign
宝桥南山5 天前
Microsoft Fabric - 简单尝试一下Microsoft Fabric .NET SDK
microsoft·微软·.net·.netcore·powerbi·fabric
geovindu6 天前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
geovindu10 天前
CSharp: Observer Pattern
开发语言·后端·观察者模式·设计模式·c#·.netcore·行为模式
geovindu11 天前
CSharp: Strategy Pattern
开发语言·后端·c#·.net·.netcore·策略模式·行为模式
geovindu11 天前
CSharp: Task Scheduler
开发语言·后端·c#·.net·.netcore
geovindu17 天前
CSharp: Wordcloud
开发语言·后端·c#·.net·.netcore·词云
geovindu17 天前
CSharp: Command Pattern
开发语言·后端·c#·.net·.netcore·命令模式·行为模式
全栈小525 天前
【C#】.net core,静态方法线程安全解析,面试时经常被问的线程安全就藏在里面
安全·c#·.netcore
清风与日月1 个月前
Yitter.IdGenerator:高性能分布式唯一ID生成器详解
分布式·c#·.net·.netcore