码迷,mamicode.com
首页 > 其他好文 > 详细

Lind.DDD.ExpressionExtensions动态构建表达式树,实现对数据集的权限控制

时间:2017-11-11 23:53:06      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:param   ddd   建表   get   tty   分析   管理系统   field   break   

Lind.DDD.ExpressionExtensions动态构建表达式树,实现对数据集的权限控制

仓储大叔好了相赠 网上2500元 跟谁学课堂 C#视频 ddd领域驱动架构设计视频 还赠送ABP视频两套

qq 2589406800 qq1399494644 qq2128543647 qq2890083872 qq3235634116 qq3381945576 qq2171713479
源代码框架lind.ddd(后台管理系统,电商系统,API,SSO,xamarin,ko,各组件单元测试) 赠送ABP视频
+lindCore源碼+LindAgility(敏捷框架) (60元)

Lind.DDD框架里提出了对数据集的控制,某些权限的用户为某些表添加某些数据集的权限,具体实现是在一张表中存储用户ID,表名,检索字段,检索值和检索操作符,然后用户登陆后,通过自己权限来构建对应表的查询语句,即动态构建表达式树,这种操作一些被写在业务层上,我们可以在业务层需要进行数据集权限控制的地方,添加这种策略,下面具体分析说明一下.

看一下数据集权限表结果

技术分享
  public class User_DataSet_Policies
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId { get; set; }
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName { get; set; }
        /// <summary>
        /// 策略所需字段
        /// </summary>
        public string PolicyField { get; set; }
        /// <summary>
        /// 策略所需要值
        /// </summary>
        public string PolicyValue { get; set; }
        /// <summary>
        /// 策略操作符
        /// </summary>
        public string PolicyOperation { get; set; }
    }
技术分享

看一下,在程序中如何动态构建和使用我们的表达式树

技术分享
        Expression<Func<User, bool>> exe = ExpressionExtensions.GenerateExpression<User>(
                new string[] { "Age", "UserName" },
                new object[] { "12", "zzl" },
                new string[] { "=", "=" });
            userList.Where(exe.Compile()).ToList().ForEach(i =>
            {
                Console.WriteLine(i.UserName);
            });
技术分享

下面贡献一下GenerateExpression泛型方法的原码,大家可以把它添加到我们的LinqExtensions模块里

技术分享
   /// <summary>
    /// 表达式树的扩展
    /// </summary>
    public class ExpressionExtensions
    {
        /// <summary>
        /// 构建表达式树
        /// 调用:GenerateExpression(new string[]{"username"},new object[]{"zzl"},new string[]{"="});
        /// </summary>
        /// <typeparam name="T">表类型</typeparam>
        /// <param name="keys">字段名</param>
        /// <param name="values">字段值</param>
        /// <param name="operation">操作符</param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> GenerateExpression<T>(string[] keys, object[] values, string[] operation)
        {
            var TType = typeof(T);
            Expression expression_return = Expression.Constant(true);
            ParameterExpression expression_param = Expression.Parameter(TType, "p");
            Expression expression;
            for (int i = 0; i < keys.Length; i++)
            {
                switch (operation[i])
                {
                    case "=":
                        expression = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                            TType.GetMethod("ToString")),
                         Expression.Constant(values[i]));
                        expression_return = Expression.And(expression_return, expression);
                        break;
                    case "%":
                        expression = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                            typeof(string).GetMethod("Contains"),
                            Expression.Constant(values[i], typeof(string)));
                        expression_return = Expression.And(expression_return, expression);
                        break;
                    case ">":
                        expression = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                            typeof(double).GetType().GetMethod("GreaterThan"), Expression.Constant(values[i]));
                        expression_return = Expression.And(expression_return, expression);
                        break;
                    case "<":
                        expression = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                            typeof(double).GetType().GetMethod("LessThan"), Expression.Constant(values[i]));
                        expression_return = Expression.And(expression_return, expression);
                        break;
                    case ">=":
                        expression = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                            typeof(double).GetType().GetMethod("GreaterThanOrEqual"), Expression.Constant(values[i]));
                        expression_return = Expression.And(expression_return, expression);
                        break;
                    case "<=":
                        expression = Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                            TType.GetProperty(keys[i]).GetType().GetMethod("LessThanOrEqual"), Expression.Constant(values[i]));
                        expression_return = Expression.And(expression_return, expression);
                        break;
                    case "in":
                        string[] strarr = values[i].ToString().Split(‘,‘);
                        Expression or_return = Expression.Constant(false);
                        for (int k = 0; k < strarr.Length; k++)
                        {
                            expression = Expression.Equal(Expression.Call(Expression.Property(expression_param, TType.GetProperty(keys[i])),
                                TType.GetMethod("ToString")),
                             Expression.Constant(strarr[k]));
                            or_return = Expression.Or(or_return, expression);
                        }

                        expression_return = Expression.And(expression_return, or_return);
                        break;
                    default:
                        throw new ArgumentException("无效的操作符,目前只支持=,%,>,<,>=,<=,in");
                }
            }

            return (Expression<Func<T, bool>>)Expression.Lambda<Func<T, bool>>(expression_return, new ParameterExpression[] { expression_param });
        }
    }
技术分享

对于动态构建表达式的介绍就到这里了,以后在使用过程中如果出现什么问题,请直接回复我.

Lind.DDD.ExpressionExtensions动态构建表达式树,实现对数据集的权限控制

标签:param   ddd   建表   get   tty   分析   管理系统   field   break   

原文地址:http://www.cnblogs.com/lindddd/p/7820341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!