自研ORM框架 实现类似EF Core Include 拆分查询 支持自定义条件、排序、选择

Baozi, I'm Mr.Zhong I like to brush TikTok, I know that anchors like to call it that, haha!
Recently, I haven't been so busy, and it took almost a day to add some APIs to the self-developed ORM framework to perfectly implement the Include query

宝子们,我是 Mr.Zhong  喜欢刷抖音都知道 主播都喜欢这么叫,哈哈!
最近没那么忙了,抽空给自研的ORM 框架新增一些API 花了将近一天的时间 完美实现 Include 查询

 

一、接口定义

 1     /// 
 2     /// 包括接口类
 3     /// 
 4     /// 
 5     /// 
 6     public interface IInclude : IQuery where TProperty : class, new()
 7     {
 8         /// 
 9         /// Ado
10         /// 
11         IAdo Ado { get; }
12     }

二、接口实现

 1     /// 
 2     /// 包括实现类
 3     /// 
 4     /// 
 5     public class IncludeProvider : QueryProvider, IInclude where TProperty : class, new()
 6     {
 7         /// 
 8         /// Ado
 9         /// 
10         public IAdo Ado { get; }
11 
12         /// 
13         /// 构造方法
14         /// 
15         /// 
16         /// 查询构建
17         public IncludeProvider(IAdo ado, IQueryBuilder queryBuilder) : base(ado, queryBuilder)
18         {
19             this.Ado = ado;
20         }
21     }

三、扩展方法实现   还可以扩展很多功能 只有你想不到,没有...

  1     /// 
  2     /// 包括扩展类
  3     /// 
  4     public static class IncludeExtensions
  5     {
  6         /// 
  7         /// 然后包括
  8         /// 
  9         /// 
 10         /// 
 11         /// 包括
 12         /// 
 13         public static IInclude Visit(this IInclude> include) where TPreviousProperty : class, new()
 14         {
 15             return new IncludeProvider(include.Ado, include.QueryBuilder);
 16         }
 17 
 18         /// 
 19         /// 然后包括
 20         /// 
 21         /// 
 22         /// 
 23         /// 包括
 24         /// 表达式
 25         /// 
 26         public static IInclude ThenInclude(this IInclude> include, Expression> expression) where TProperty : class, new()
 27         {
 28             return new IncludeProvider(include.Ado, include.QueryBuilder);
 29         }
 30 
 31         /// 
 32         /// 条件
 33         /// 
 34         /// 
 35         /// 
 36         /// 包括
 37         /// 表达式
 38         /// 
 39         public static IInclude Where(this IInclude include, Expressionbool>> expression) where TProperty : class, new()
 40         {
 41             var queryBuilder = include.QueryBuilder.IncludeInfos.Last().QueryBuilder;
 42 
 43             queryBuilder.EntityDbMapping.Alias = expression.Parameters[0]?.Name;
 44             include.QueryBuilder.IncludeInfos.Last().EntityDbMapping.Alias = expression.Parameters[1]?.Name;
 45 
 46             var result = expression.ResolveSql(new ResolveSqlOptions()
 47             {
 48                 DbType = include.Ado.DbOptions.DbType,
 49                 ResolveSqlType = ResolveSqlType.Where,
 50                 ParameterIndex = queryBuilder.DbParameters.Count + 1
 51             });
 52 
 53             queryBuilder.Where.Add(result.SqlString);
 54             queryBuilder.DbParameters.AddRange(result.DbParameters);
 55             return include;
 56         }
 57 
 58         /// 
 59         /// 排序
 60         /// 
 61         /// 
 62         /// 
 63         /// 包括
 64         /// 排序字段
 65         /// 排序类型
 66         /// 
 67         public static IInclude OrderBy(this IInclude include, Liststring> orderFields, OrderByType orderByType = OrderByType.ASC) where TProperty : class, new()
 68         {
 69             var queryBuilder = include.QueryBuilder.IncludeInfos.Last().QueryBuilder;
 70 
 71             queryBuilder.OrderBy.Add($"{string.Join(",", orderFields)} {orderByType}");
 72             return include;
 73         }
 74 
 75         /// 
 76         /// 排序
 77         /// 
 78         /// 
 79         /// 
 80         /// 包括
 81         /// 表达式
 82         /// 排序类型
 83         /// 
 84         public static IInclude OrderBy(this IInclude include, Expressionobject>> expression, OrderByType orderByType = OrderByType.ASC) where TProperty : class, new()
 85         {
 86             var queryBuilder = include.QueryBuilder.IncludeInfos.Last().QueryBuilder;
 87 
 88             queryBuilder.EntityDbMapping.Alias = expression.Parameters[0]?.Name;
 89             include.QueryBuilder.IncludeInfos.Last().EntityDbMapping.Alias = expression.Parameters[1]?.Name;
 90 
 91             var result = expression.ResolveSql(new ResolveSqlOptions()
 92             {
 93                 DbType = include.Ado.DbOptions.DbType,
 94                 ResolveSqlType = ResolveSqlType.OrderBy
 95             });
 96 
 97             queryBuilder.OrderBy.Add($"{result.SqlString} {orderByType}");
 98             return include;
 99         }
100 
101         /// 
102         /// 选择
103         /// 
104         /// 
105         /// 
106         /// 包括
107         /// 表达式
108         /// 
109         public static IInclude Select(this IInclude include, Expressionobject>> expression) where TProperty : class, new()
110         {
111             var queryBuilder = include.QueryBuilder.IncludeInfos.Last().QueryBuilder;
112 
113             queryBuilder.EntityDbMapping.Alias = expression.Parameters[0]?.Name;
114             include.QueryBuilder.IncludeInfos.Last().EntityDbMapping.Alias = expression.Parameters[1]?.Name;
115 
116             var result = expression.ResolveSql(new ResolveSqlOptions()
117             {
118                 DbType = include.Ado.DbOptions.DbType,
119                 ResolveSqlType = ResolveSqlType.NewAs
120             });
121 
122             queryBuilder.Columns = result.SqlString;
123             queryBuilder.DbParameters.AddRange(result.DbParameters);
124             return include;
125         }
126 
127     }
128 }

 

 

四、使用示例

1 var data = await db.Query().Include(a => a.Products).Visit().OrderBy((a1, a2) => a2.CreateTime, OrderByType.DESC).ToListAsync();

 

五、生成的SQL语句

SELECT a1.CategoryId,a1.CategoryName,a2.ProductId,a2.CategoryId,a2.ProductCode,a2.ProductName,a2.DeleteMark,a2.CreateTime,a2.ModifyTime,a2.Custom1,a2.Custom2,a2.Custom3,a2.Custom4,a2.Custom5,a2.Custom6,a2.Custom7,a2.Custom8,a2.Custom9,a2.Custom10,a2.Custom11,a2.Custom12 FROM Category a1
INNER JOIN Product a2 ON a1.CategoryId = a2.CategoryId
WHERE a1.CategoryId = @CategoryId
ORDER BY a2.CreateTime DESC

 

 

 

文章来源于互联网:自研ORM框架 实现类似EF Core Include 拆分查询 支持自定义条件、排序、选择

THE END
分享
二维码