コード例 #1
0
        protected override IEnumerable <Result> CheckInternal(object checkable, pct.BizRulez.Rules.Rule rule)
        {
            string collectionField = ((RuleStringParameter)rule.Parameters["CollectionFieldName"]).Value;
            var    collection      = GetValue(checkable, collectionField) as IEnumerable;

            if (collection != null)
            {
                var getKeyClassParameter    = rule.Parameters["GetKeyFunctionClass"] as RuleStringParameter;
                var getKeyFunctionParameter = rule.Parameters["GetKeyFunctionName"] as RuleStringParameter;

                MethodInfo getKeyFunction = null;

                // simple check here, more elaborate check is being done in IsCompatibleParameterSet
                if (getKeyClassParameter != null)
                {
                    var getKeyClassType = Type.GetType(getKeyClassParameter.Value);
                    if (getKeyClassType == null)
                    {
                        yield return(new Result(this, rule, ResultStatus.Fail, FailureReason.CheckNotPerformed,
                                                "GetKey class not found"));

                        yield break;
                    }
                    getKeyFunction = getKeyClassType.GetMethod(getKeyFunctionParameter.Value);
                    if (getKeyFunction == null)
                    {
                        yield return(new Result(this, rule, ResultStatus.Fail, FailureReason.CheckNotPerformed,
                                                "GetKey function not found"));

                        yield break;
                    }
                }

                Hashtable hashtable;
                if (getKeyFunction != null)
                {
                    hashtable = new Hashtable(new Comparer(getKeyFunction));
                }
                else
                {
                    hashtable = new Hashtable( );
                }

                foreach (var item in collection)
                {
                    if (!hashtable.ContainsKey(item))
                    {
                        hashtable.Add(item, null);
                    }
                    else
                    {
                        yield return(new Result(this, rule, ResultStatus.Fail, FailureReason.CheckFailed,
                                                String.Format(rule.ErrorInfoTemplate, item)));

                        yield break;
                    }
                }
            }

            yield return(new Result(this, rule, ResultStatus.Pass, FailureReason.None, String.Empty));
        }
コード例 #2
0
        protected override IEnumerable <Result> CheckInternal(object checkable, pct.BizRulez.Rules.Rule rule)
        {
            var    elementValueParam = rule.Parameters["ElementValue"];
            object value             = null;

            if (elementValueParam != null)
            {
                value = elementValueParam.Value;
            }
            else
            {
                var elementField = ((RuleStringParameter)rule.Parameters["ElementFieldName"]).Value;
                value = GetValue(checkable, elementField);
            }

            string collectionField = ((RuleStringParameter)rule.Parameters["CollectionFieldName"]).Value;
            object collection      = GetValue(checkable, collectionField);

            Type valueType      = null;
            Type collectionType = collection.GetType( );

            if (value == null)
            {
                var nullSatisfiesParam = rule.Parameters["RuleSatisfiedByNull"] as RuleBoolParameter;
                if (nullSatisfiesParam != null &&
                    nullSatisfiesParam.Value == true)
                {
                    yield return(new Result(
                                     this, rule, ResultStatus.Pass,
                                     FailureReason.None, String.Empty));

                    yield break;
                }
            }
            else
            {
                valueType = value.GetType( );
                var wantedType = typeof(IEnumerable <>).MakeGenericType(valueType);
                if (!wantedType.IsAssignableFrom(collectionType))
                {
                    yield return(new Result(this, rule, ResultStatus.Fail,
                                            FailureReason.CheckNotPerformed,
                                            "Collection doesn't implement " + wantedType));

                    yield break;
                }
            }

            if (Contains(collection, value, collectionType, valueType))
            {
                yield return(new Result(this, rule, ResultStatus.Pass,
                                        FailureReason.None, String.Empty));
            }
            else
            {
                yield return(new Result(this, rule, ResultStatus.Fail,
                                        FailureReason.CheckFailed,
                                        string.Format(CultureInfo.CurrentCulture,
                                                      rule.ErrorInfoTemplate, value)));
            }
        }