Example #1
0
        protected IObjectValidator <T> AddRule <F1, F2>(Expression <Func <T, F1> > field, Expression <Func <T, F2> > compareField, Func <F1, F2, F1> compareFunc, Func <T, bool> condition, string[] actions, bool isPartial)
        {
            Func <object, bool> func = null;

            lock (this.fieldRules)
            {
                string           name                = field.Parameters[0].Type.Name;
                string           fieldname           = (field.Body as MemberExpression).Member.Name;
                Func <T, object> getFieldFunc        = Expression.Lambda <Func <T, object> >(Expression.Convert(field.Body, typeof(object)), field.Parameters).Compile();
                string           str2                = (compareField.Body as MemberExpression).Member.Name;
                Func <T, object> getCompareFieldFunc = Expression.Lambda <Func <T, object> >(Expression.Convert(compareField.Body, typeof(object)), compareField.Parameters).Compile();
                FieldRule        item                = new FieldRule
                {
                    GetField = delegate(object m)
                    {
                        T arg = (T)m;
                        return(new object[] { getFieldFunc(arg), getCompareFieldFunc(arg) });
                    },
                    Actions  = actions,
                    Validate = delegate(object f, string action)
                    {
                        object obj2;
                        try
                        {
                            object[] objArray = f as object[];
                            compareFunc((F1)objArray[0], (F2)objArray[1]);
                            obj2 = objArray[0];
                        }
                        catch (ValidateException exception)
                        {
                            throw new FieldValidateException(exception.Message, fieldname, exception.FieldValue);
                        }
                        return(obj2);
                    }
                };
                if (condition != null)
                {
                    if (func == null)
                    {
                        func = m => condition((T)m);
                    }
                    item.When = func;
                }
                if (this.fieldRules.ContainsKey(fieldname))
                {
                    if (!isPartial)
                    {
                        throw new Exception(string.Format("重複添加 {0} 的驗證規則!", fieldname));
                    }
                }
                else
                {
                    this.fieldRules.Add(fieldname, new List <FieldRule>());
                }
                this.fieldRules[fieldname].Add(item);
            }
            return(this);
        }
Example #2
0
        protected IObjectValidator <T> AddRule <F>(Expression <Func <T, F> > field, Func <F, string, F> func, Func <T, bool> condition, string[] actions, bool isPartial)
        {
            Func <object, bool> func2 = null;

            lock (this.fieldRules)
            {
                MemberExpression body         = field.Body as MemberExpression;
                string           name         = field.Parameters[0].Type.Name;
                string           fieldname    = body.Member.Name;
                Func <T, object> getFieldFunc = Expression.Lambda <Func <T, object> >(Expression.Convert(field.Body, typeof(object)), field.Parameters).Compile();
                FieldRule        item         = new FieldRule
                {
                    GetField = m => getFieldFunc((T)m),
                    Actions  = actions,
                    Validate = delegate(object f, string action)
                    {
                        object obj2;
                        try
                        {
                            obj2 = func((F)f, action);
                        }
                        catch (ValidateException exception)
                        {
                            throw new FieldValidateException(exception.Message, fieldname, exception.FieldValue);
                        }
                        return(obj2);
                    }
                };
                if (condition != null)
                {
                    if (func2 == null)
                    {
                        func2 = m => condition((T)m);
                    }
                    item.When = func2;
                }
                if (this.fieldRules.ContainsKey(fieldname))
                {
                    if (!isPartial)
                    {
                        throw new Exception(string.Format("重複添加 {0} 的驗證規則!", fieldname));
                    }
                }
                else
                {
                    this.fieldRules.Add(fieldname, new List <FieldRule>());
                }
                this.fieldRules[fieldname].Add(item);
            }
            return(this);
        }