Ejemplo n.º 1
0
        /// <summary>
        /// Valide les contraintes pour une propriété.
        /// </summary>
        /// <param name="value">Valeur de la propriété.</param>
        /// <param name="checkNullValue">True si la nullité doit être vérifiée.</param>
        /// <param name="isForceRequirement">Si on force le fait que la propriété est requise ou non.</param>
        internal void ValidConstraints(object value, bool checkNullValue, bool?isForceRequirement)
        {
            bool isRequired = this.IsRequired;

            if (isForceRequirement.HasValue)
            {
                isRequired = isForceRequirement.Value;
            }

            if (isRequired && checkNullValue)
            {
                RequiredAttribute c = new RequiredAttribute {
                    AllowEmptyStrings = false, ErrorMessageResourceType = typeof(SR), ErrorMessageResourceName = "ConstraintNotNull"
                };
                ExtendedValue extVal = value as ExtendedValue;
                if (extVal != null)
                {
                    if (!c.IsValid(extVal.Value) || !c.IsValid(extVal.Metadata))
                    {
                        throw new ConstraintException(this, c.FormatErrorMessage(PropertyName));
                    }
                }
                else if (!c.IsValid(value))
                {
                    throw new ConstraintException(this, c.FormatErrorMessage(PropertyName));
                }
            }

            this.Domain.CheckValue(value, this);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Indique si l'ExtendedValue est égale a un objet donné.
        /// </summary>
        /// <param name="obj">L'objet à comparer.</param>
        /// <returns>Indique si les objets sont égaux.</returns>
        public override bool Equals(object obj)
        {
            ExtendedValue other = obj as ExtendedValue;

            return(other == null?
                   base.Equals(obj) :
                       object.Equals(this.Value, other.Value) && object.Equals(this.Metadata, other.Metadata));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Comparaison.
        /// </summary>
        /// <param name="obj">L'objet.</param>
        /// <returns>Retourne 0 si équivalent, -1 si inférieur, 1 si supérieur.</returns>
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            ExtendedValue value = (ExtendedValue)obj;

            if (object.Equals(this.Value, value.Value) && object.Equals(this.Metadata, value.Metadata))
            {
                return(0);
            }

            if (this.Value == null)
            {
                return(-1);
            }

            return(decimal.Compare((decimal)this.Value, (decimal)value.Value));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Converti une valeur en string.
        /// </summary>
        /// <param name="value">Valeur à convertir.</param>
        /// <param name="propertyDescriptor">Propriété.</param>
        /// <exception cref="System.InvalidCastException">En cas d'erreur de type.</exception>
        /// <returns>La valeur sous sa forme textuelle.</returns>
        string IDomainChecker.ConvertToString(object value, BeanPropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor != null)
            {
                CheckValueType(value, propertyDescriptor);
            }

            if (_formatter != null)
            {
                return(_formatter.ConvertToString((T)value));
            }

            ExtendedValue extValue = value as ExtendedValue;

            if (_extendedFormatter != null && extValue != null)
            {
                return(_extendedFormatter.ConvertToString(extValue));
            }

            return(TypeDescriptor.GetConverter(typeof(T)).ConvertToString(value));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Vérifie que le type d'une valeur correspond bien au type
        /// de la propriété.
        /// </summary>
        /// <param name="value">Valeur à tester.</param>
        public void CheckValueType(object value)
        {
            if (value == null)
            {
                return;
            }

            Type valueType = value.GetType();

            if (valueType.IsPrimitive || typeof(decimal).Equals(valueType) || typeof(Guid).Equals(valueType) ||
                typeof(string).Equals(valueType) || typeof(DateTime).Equals(valueType) ||
                typeof(byte[]).Equals(valueType) || typeof(TimeSpan).Equals(valueType))
            {
                if (!valueType.Equals(PrimitiveType))
                {
                    throw new InvalidCastException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  SR.ExceptionInvalidValueType,
                                  valueType,
                                  PropertyType,
                                  PropertyName));
                }

                return;
            }

            ExtendedValue extValue = value as ExtendedValue;

            if (extValue != null)
            {
                this.CheckValueType(extValue.Value);
                return;
            }

            throw new NotSupportedException(string.Format(
                                                CultureInfo.CurrentUICulture,
                                                SR.ExceptionTypeDescription,
                                                PropertyType));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Définit la valeur de la propriété pour un objet.
        /// </summary>
        /// <param name="bean">Objet.</param>
        /// <param name="value">Valeur.</param>
        /// <exception cref="System.NotSupportedException">Si la propriété est en lecture seule.</exception>
        public void SetValue(object bean, object value)
        {
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(bean)[this.PropertyName];

            if (descriptor.IsReadOnly)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, SR.ReadOnlyProperty, this.PropertyName));
            }

            if (this.Domain == null || this.Domain.MetadataPropertySuffix == null)
            {
                descriptor.SetValue(bean, value);
                return;
            }

            ExtendedValue extValue = (ExtendedValue)value;

            descriptor.SetValue(bean, extValue.Value);
            PropertyDescriptor metadataDescriptor = TypeDescriptor.GetProperties(bean)[this.PropertyName + this.Domain.MetadataPropertySuffix];

            metadataDescriptor.SetValue(bean, extValue.Metadata);
        }