예제 #1
0
 /// <summary> Initializes a new instance of the BusinessValueMetadata class with the specified settings and callbacks.
 /// </summary>
 /// <param name="settings"></param>
 /// <param name="validateValueCallback">A reference to a callback that should perform any custom validation of the business value value beyond typical type validation.</param>
 /// <param name="valueChangedCallback">A reference to a handler implementation that the property system will call whenever the effective value of the property changes.</param>
 /// <param name="coerceValueCallback">A reference to a handler implementation will be called whenever the property system calls CoerceValue for the business value.</param>
 public BusinessValueMetadata(IValueSettings settings, ValidateValueCallback validateValueCallback, BusinessValueChangedCallback valueChangedCallback, CoerceValueCallback coerceValueCallback)
 {
     _settings = settings;
     _validateValueCallback = validateValueCallback;
     _valueChangedCallback  = valueChangedCallback;
     _coerceValueCallback   = coerceValueCallback;
 }
예제 #2
0
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]        //REVISE: too complex
        protected virtual Exception ValidateValueInternal(object value, IValueSettings rules, IObjectBM businessObject, bool throwOnInvalid)
        {
            //		    var result=base.Validate(value, rules, throwOnError);
            //		    if(!throwOnError && !result) return false;

            #region Null Validator

            if (Equals(value, null))
            {
                if (!rules.AllowNull)
                {
                    var ex = new ValueValidationException(value, businessObject, new ArgumentNullException(nameof(value), "Value must be not null!"));
                    if (!throwOnInvalid)
                    {
                        return(ex);
                    }
                    throw ex;
                }
                return(null);            // if null allowed all other test are skipped
            }

            #endregion

            // at this point value is never null

            #region Empty Validator

            if (value is string && Equals(value, string.Empty))
            {
                if (!rules.AllowEmpty)
                {
                    var ex = new ValueValidationException(value, businessObject, new ArgumentOutOfRangeException(nameof(value), value, "Value must not be an empty string!"));
                    if (!throwOnInvalid)
                    {
                        return(ex);
                    }
                    throw ex;
                }
                return(null);            // if empty string allowed all other test are skipped
            }

            #endregion

            // at this point value is never null nor empty string

            if (value is IComparable)
            {
                // this is true for all simple types (Int32, Boolean, etc.) and for String, DateTime, TimeSpan, Guid

                if (rules.MinimumSpecified)
                {
                    if (Comparer.Default.Compare(rules.Minimum, value) > 0)
                    {
                        var ex = new ValueValidationException(value, businessObject, new ArgumentOutOfRangeException(nameof(value), value, "Value must not be less then Minimum!"));
                        if (!throwOnInvalid)
                        {
                            return(ex);
                        }
                        throw ex;
                    }
                }
                if (rules.MaximumSpecified)
                {
                    if (Comparer.Default.Compare(value, rules.Maximum) > 0)
                    {
                        var ex = new ValueValidationException(value, businessObject, new ArgumentOutOfRangeException(nameof(value), value, "Value must not be greater then Maximum!"));
                        if (!throwOnInvalid)
                        {
                            return(ex);
                        }
                        throw ex;
                    }
                }
                if (rules.IncludeValuesSpecified)
                {
                    if (!rules.IncludeValues.Cast <object>().Any(a => Comparer.Default.Compare(a, value) == 0))
                    {
                        var ex = new ValueValidationException(value, businessObject, new ArgumentOutOfRangeException(nameof(value), value, "Value must be in IncludeValues"));
                        if (!throwOnInvalid)
                        {
                            return(ex);
                        }
                        throw ex;
                    }
                }
                if (rules.ExcludeValuesSpecified && rules.ExcludeValues != null)
                {
                    if (rules.ExcludeValues.Cast <object>().Any(a => Comparer.Default.Compare(a, value) == 0))
                    {
                        var ex = new ValueValidationException(value, businessObject, new ArgumentOutOfRangeException(nameof(value), value, "Value must not be in EcludeValues!"));
                        if (!throwOnInvalid)
                        {
                            return(ex);
                        }
                        throw ex;
                    }
                }
            }
            else
            {
                throw new NotImplementedException("Type is not comparable! ValidateValueInternal must be overridden! " + value.GetType().FullName + ", Implementing Type: " + this.GetType().FullName);
            }

            return(null);
        }