Ejemplo n.º 1
0
        protected override bool ConditionApplies(ModelInstance root)
        {
            var value = root[Property];

            // Exit immediately if the target property has a value
            if (RequiredValue == null && (value != null &&
                                          (!(Property is ModelReferenceProperty) || !Property.IsList || root.GetList((ModelReferenceProperty)Property).Count > 0)))
            {
                return(false);
            }

            // If the required value is specified then the value must equal the required value
            var requiredValueCondition = RequiredValue == null || !RequiredValue.Equals(value);

            // Invoke the ModelExpression if it exists
            if (RequiredExpression != null)
            {
                try
                {
                    return((bool)RequiredExpression.Invoke(root) && requiredValueCondition);
                }
                catch
                {
                    return(false);
                }
            }
            // If the value to compare is null, then evaluate whether the compare source has a value
            else if (CompareValue == null)
            {
                return((CompareOperator == CompareOperator.Equal ? !compareSource.HasValue(root) : compareSource.HasValue(root)) && requiredValueCondition);
            }

            // Otherwise, perform a comparison of the compare source relative to the compare value
            bool?result = CompareRule.Compare(compareSource.GetValue(root), CompareOperator, CompareValue);

            return(result.HasValue && result.Value && requiredValueCondition);
        }