昨天开源了业务业余时间本身封装的dapper lambda扩展,同时写了篇博文《编写本身的dapper lambda扩展-使用篇》简单的介绍了下其使用,今天将分享下它的设计思路html
其实就是将多个方法经过点(.)将它们串接起来,让代码更加简洁, 可读性更强。node
new SqlConnection("").QuerySet<User>() .Where(a => a.Name == "aasdasd") .OrderBy(a => a.CreateTime)
.Top(10) .Select(a => a.Name).ToList();
其原理是类的调用方法的返回值类型为类自己或其基类,选择返回基类的缘由是为了作降级约束,例如我但愿使用了Top以后接着Select和ToList,没法再用where或orderBy。git
public class CommandSet<T> : IInsert<T>, ICommand<T> { #region 方法 public int Insert(T entity) { throw new NotImplementedException(); } public int Update(T entity) { throw new NotImplementedException(); } public int Update(Expression<Func<T, T>> updateExpression) { throw new NotImplementedException(); } public int Delete() { throw new NotImplementedException(); } public IInsert<T> IfNotExists(Expression<Func<T, bool>> predicate) { throw new NotImplementedException(); } public ICommand<T> Where(Expression<Func<T, bool>> predicate) { throw new NotImplementedException(); } #endregion } public interface ICommand<T> { int Update(T entity); int Update(Expression<Func<T, T>> updateExpression); int Delete(); } public interface IInsert<T> { int Insert(T entity); } public static class Database { public static QuerySet<T> QuerySet<T>(this SqlConnection sqlConnection) { return new QuerySet<T>(); } public static CommandSet<T> CommandSet<T>(this SqlConnection sqlConnection) { return new CommandSet<T>(); } }
public class QuerySet<T> : IAggregation<T> { #region 方法 public T Get() { throw new NotImplementedException(); } public List<T> ToList() { throw new NotImplementedException(); } public PageList<T> PageList(int pageIndex, int pageSize) { throw new NotImplementedException(); } public List<T> UpdateSelect(Expression<Func<T, T>> @where) { throw new NotImplementedException(); } public IQuery<TResult> Select<TResult>(Expression<Func<T, TResult>> selector) { throw new NotImplementedException(); } public IOption<T> Top(int num) { throw new NotImplementedException(); } public IOrder<T> OrderBy<TProperty>(Expression<Func<T, TProperty>> field) { throw new NotImplementedException(); } public IOrder<T> OrderByDescing<TProperty>(Expression<Func<T, TProperty>> field) { throw new NotImplementedException(); } public int Count() { throw new NotImplementedException(); } public bool Exists() { throw new NotImplementedException(); } public QuerySet<T> Where(Expression<Func<T, bool>> predicate) { throw new NotImplementedException(); } #endregion } public interface IAggregation<T> : IOrder<T> { int Count(); bool Exists(); } public interface IOrder<T> : IOption<T> { IOrder<T> OrderBy<TProperty>(Expression<Func<T, TProperty>> field); IOrder<T> OrderByDescing<TProperty>(Expression<Func<T, TProperty>> field); } public interface IOption<T> : IQuery<T>, IUpdateSelect<T> { IQuery<TResult> Select<TResult>(Expression<Func<T, TResult>> selector); IOption<T> Top(int num); } public interface IUpdateSelect<T> { List<T> UpdateSelect(Expression<Func<T, T>> where); } public interface IQuery<T> { T Get(); List<T> ToList(); PageList<T> PageList(int pageIndex, int pageSize); }
以上为基本的设计模型,具体实现若有问题能够查看个人源码。github
具体实现的时候会涉及到不少的表达式树的解析,例如where条件、部分字段update,而我实现的时候一共两步:先修树,再翻译。然而不管哪步都得对表达式树进行遍历。sql
百度的定义:也称为“表达式目录树”,以数据形式表示语言级代码,它是一种抽象语法树或者说是一种数据结构。编程
我对它的理解是,它本质是一个二叉树,节点拥有本身的属性像nodetype。数据结构
而它的遍历方式为前序遍历app
百度的定义:历首先访问根结点而后遍历左子树,最后遍历右子树。在遍历左、右子树时,仍然先访问根结点,而后遍历左子树,最后遍历右子树,如下图为例ide
其遍历结果为:ABDECFpost
以一个实际例子:
从上图能够看出,咱们会先遍历到根节点的NodeType AndAlso翻译为 and ,而后到节点2,NodeType的Equal翻译为 = ,再到3节点翻译为 Name,再到4节点翻译为'skychen',那么将三、4节点拼接起来就为Name = 'skychen',若是类推六、7为Age >= 18,最后拼接这个语句为 Name = 'skychen' and Age >= 18。
修树的目的,为了咱们更好的翻译,例如DateTime.Now表达式树里的NodeType为MemberAccess,我但愿转换成NodeType为Constant类型,以'2018-06-27 16:18:00'这个值做为翻译。
以上为设计和实现的要点,具体的实现问题能够查看源码,若是有建议和疑问能够在下方留言,若是对您起到做用,但愿您点一下推荐做为对个人支持。
再次双手奉上源码:https://github.com/SkyChenSky/Sikiro.DapperLambdaExtension.MsSql