コード例 #1
0
        public bool ProcessRule(TA a, IDataAdapter <TB> dataAdapter)
        {
            if (preIs.Any(v => !CheckPositivePrerequisite(v, a)))
            {
                return(false);
            }

            if (preNot.Any(v => !CheckNegativePrerequisite(v, a)))
            {
                return(false);
            }

            var query = dataAdapter.GetData();

            var resultSet = SkipToActionPart ? Enumerable.Empty <TB>() : TestForConditions(query, a);

            var enumerable = resultSet as IList <TB> ?? resultSet.ToList();

            if (enumerable.Any())
            {
                bool ok = true;

                if (singleFlag && enumerable.Count() > 1)
                {
                    if (singleFailAction != null)
                    {
                        singleFailAction(a, enumerable.First(), ruleName);
                    }
                    return(false);
                }

                foreach (var b in enumerable)
                {
                    if (customConditions.Any())
                    {
                        if (customConditions.Any(customCondition => !customCondition(a, b)))
                        {
                            ok = false;
                        }
                    }

                    if (!ok)
                    {
                        continue;
                    }

                    foreach (var apply in applies)
                    {
                        object val = Helper.GetMemberValue(apply.Item1, a);
                        if (apply.Item3)
                        {
                            if (!Helper.CheckIsNotNullOrDefault(apply.Item2, b))
                            {
                                Helper.SetMemberValue(apply.Item2, b, val);
                            }
                        }
                        else
                        {
                            Helper.SetMemberValue(apply.Item2, b, val);
                        }
                    }

                    dataAdapter.SaveChanges();

                    foreach (var customAction in customActions)
                    {
                        customAction(a, b, ruleName);
                    }
                }

                if (ok && throwErrorFlag)
                {
                    throw new UserDefinedRuleException(ruleName + "; throw error triggered");
                }

                return(!nextRuleFlag && ok);
            }

            if (SkipToActionPart)
            {
                foreach (var customAction in customActions)
                {
                    customAction(a, default(TB), ruleName);
                }
            }

            if (anyFlag)
            {
                if (anyFailAction != null)
                {
                    anyFailAction(a, ruleName);
                }
            }

            return(false);
        }