Exemple #1
0
        private void SetLastUpdater(object obj, string userId)
        {
            DbObjectInfo dbObjectInfo = DbObjectTools.GetDbObjectInfo(obj.GetType());

            if (DbObjectTools.GetDynamicPropertyInfo(obj.GetType(), "LastUpdateUserId") != null)
            {
                ((DbObject)obj).SetValue <string>("LastUpdateUserId", userId);
            }
        }
Exemple #2
0
 private string MemberAccessToSql(MemberExpression m, ParameterExpression[] parameters, object[] parameterValues, ParameterExpression parameter)
 {
     if (((m.Member.MemberType == MemberTypes.Property) && (m.Expression != null)) && (m.Expression.NodeType == ExpressionType.Parameter))
     {
         ParameterExpression expression = m.Expression as ParameterExpression;
         if (((parameters.Length == 1) || (expression == parameter)) || (expression.Name == parameter.Name))
         {
             ParameterExpression expression2 = m.Expression as ParameterExpression;
             PropertyInfo        member      = m.Member as PropertyInfo;
             if (member != null)
             {
                 DynamicPropertyInfo dynamicPropertyInfo = DbObjectTools.GetDynamicPropertyInfo(expression2.Type, member);
                 if (dynamicPropertyInfo != null)
                 {
                     return(this.SqlFormatter.FormatFieldName(dynamicPropertyInfo.DataFieldName));
                 }
             }
         }
     }
     return(this.FormatValue(m, parameters, parameterValues));
 }
Exemple #3
0
        private IRuleMapper <T> Init <T>(IRuleMapper <T> ruleMapper = null) where T : DbObject, new()
        {
            Func <object, object> item  = null;
            Func <object, object> func4 = null;
            Func <object, object> func5 = null;
            Func <object, bool>   func6 = null;

            ruleMapper = ruleMapper ?? new RuleEngine <T>();
            Type type = typeof(T);

            foreach (PropertyInfo info1 in type.GetProperties())
            {
                Func <object, object>         func  = null;
                Func <object, object>         func2 = null;
                List <Func <object, object> > funcs;
                MethodInfo          getMethod;
                string              fieldName;
                DynamicPropertyInfo info = DbObjectTools.GetDynamicPropertyInfo(type, info1);
                if (info != null)
                {
                    Type propertyType = info.PropertyType;
                    funcs = new List <Func <object, object> >();
                    if (!info.AllowDBNull)
                    {
                        if (item == null)
                        {
                            item = o => ValidateExtend.NotNull <object>(o);
                        }
                        funcs.Add(item);
                    }
                    if ((propertyType == typeof(string)) && (info.Length > 0))
                    {
                        if (func == null)
                        {
                            func = delegate(object o)
                            {
                                if (o != null)
                                {
                                    ValidateExtend.LengthBelowOrEqual((string)o, info.Length, false);
                                }
                                return(o);
                            };
                        }
                        funcs.Add(func);
                    }
                    if (((propertyType != typeof(DateTime)) && (propertyType != typeof(DateTime?))) && ((info.NumericPercision != -1) || (info.NumericScale != -1)))
                    {
                        if (func2 == null)
                        {
                            func2 = delegate(object o)
                            {
                                if (o != null)
                                {
                                    ValidateExtend.IsDecimal <object>(o, info.NumericPercision, info.NumericScale);
                                }
                                return(o);
                            };
                        }
                        funcs.Add(func2);
                    }
                    if (propertyType == typeof(DateTime))
                    {
                        if (func4 == null)
                        {
                            func4 = delegate(object o)
                            {
                                if (o != null)
                                {
                                    ValidateExtend.GreaterThanOrEqual <DateTime>((DateTime)o, SqlDateTime.MinValue.Value);
                                }
                                return(o);
                            };
                        }
                        funcs.Add(func4);
                    }
                    if (propertyType == typeof(DateTime?))
                    {
                        if (func5 == null)
                        {
                            func5 = delegate(object o)
                            {
                                if (o != null)
                                {
                                    DateTime?nullable = (DateTime?)o;
                                    ValidateExtend.GreaterThanOrEqual <DateTime>(nullable.Value, SqlDateTime.MinValue.Value);
                                }
                                return(o);
                            };
                        }
                        funcs.Add(func5);
                    }
                    List <FieldRule> list = null;
                    if (!ruleMapper.FieldRules.TryGetValue(info1.Name, out list))
                    {
                        list = new List <FieldRule>();
                        ruleMapper.FieldRules.Add(info1.Name, list);
                    }
                    getMethod = info1.GetGetMethod();
                    fieldName = info1.Name;
                    FieldRule rule2 = new FieldRule();
                    rule2.GetField = (t => getMethod.Invoke(t, null));
                    rule2.Validate = (delegate(object o, string action)
                    {
                        Action <Func <object, object> > action2 = null;
                        object obj2;
                        try
                        {
                            if (action2 == null)
                            {
                                action2 = delegate(Func <object, object> f)
                                {
                                    f(o);
                                };
                            }
                            funcs.ForEach(action2);
                            obj2 = o;
                        }
                        catch (ValidateException exception)
                        {
                            throw new FieldValidateException(exception.Message, fieldName, exception.FieldValue);
                        }
                        return(obj2);
                    });
                    rule2.Actions = (new string[] { DbOperation.Create.ToString(), (DbOperation.None | DbOperation.Update).ToString() });
                    if (func6 == null)
                    {
                        func6 = t => true;
                    }
                    rule2.When = (func6);
                    FieldRule rule = rule2;
                    list.Insert(0, rule);
                }
            }
            return(ruleMapper);
        }