MetadataType problems on Silverlight Integration Pack

Topics: Silverlight Integration Pack, Validation Application Block
Nov 29, 2011 at 11:19 AM
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.DataAnnotations;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using System.ComponentModel.DataAnnotations;


namespace TransportController
{
    public class LimsModel
    {

        public abstract class EntityBase : IDataErrorInfo
        {
            public EntityBase()
            {
                this.errorInfoHelper = new ExtendedDataErrorInfoHelper(new DataErrorInfoHelper(this));
            }

            public readonly ExtendedDataErrorInfoHelper errorInfoHelper;

            #region IDataErrorInfo Members

            public string this[string columnName]
            {
                get { return errorInfoHelper[columnName]; }
            }

            public string Error
            {
                get { return errorInfoHelper.Error; }
            }

            #endregion

        }


        [MetadataType(typeof(UserMetadata))]
        public partial class User : EntityBase, INotifyPropertyChanged
        {
            public User()
            {

            }   

            private string descrizione;

            //[StringLengthValidator(1, RangeBoundaryType.Inclusive, 2, RangeBoundaryType.Ignore, MessageTemplate = "Descrizione vuota.")]
            public string Descrizione
            {
                get { return descrizione; }
                set { descrizione = value; OnPropertyChanged("Descrizione"); }
            }

            private DateTime dataArrivo;
            //[RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, MessageTemplate = "Must be 18 years or older.")]
            public DateTime DataArrivo
            {
                get { return dataArrivo; }
                set { dataArrivo = value; OnPropertyChanged("DataArrivo"); }
            }

            Microsoft.Practices.EnterpriseLibrary.Validation.Validator modelValidatorForCampione;

            private void ValidateModel()
            {
                
                if (modelValidatorForCampione == null)
                {
                    
                    modelValidatorForCampione = ValidationFactory.CreateValidator<User>();
                }
                ValidationResults results = modelValidatorForCampione.Validate(this );
                errorInfoHelper.ClearExtraMessages();
                errorInfoHelper.AddErrorMessages(results.Select(result => Tuple.Create(result.Key, result.Message)));
           
            }

            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;

            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
                ValidateModel();
            }

            #endregion

   

        }

    }


    public class UserMetadata
    {
        [Required(ErrorMessage = "Descrizione is required.")]
        [StringLengthValidator(1, RangeBoundaryType.Inclusive, 2, RangeBoundaryType.Ignore, MessageTemplate = "Descrizione vuota.")]
        public string Descrizione;

        [RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, MessageTemplate = "Must be 18 years or older.")]
        public DateTime DataArrivo;
    }


}


modelValidationForCampione does not contain rule for validate(with Metadatatype). While if use notation on User class it works!
What is the problem?
Nov 29, 2011 at 10:50 PM

The issue is that the User class has properties but the UserMetaData class has Fields.  When Enterprise Library performs validation with a metadata class
it looks for the same property name and the same type.

 If you change the code to use properties it should work:

    public class UserMetadata
    {
        [Required(ErrorMessage = "Descrizione is required.")]
        [StringLengthValidator(1, RangeBoundaryType.Inclusive, 2, RangeBoundaryType.Ignore, MessageTemplate = "Descrizione vuota.")]
        public string Descrizione
        {
             get;
             set;
        }

        [RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, MessageTemplate = "Must be 18 years or older.")]
        public DateTime DataArrivo
        {
             get;
             set;
        }
    }

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com