using RegisterAsMetadataTypeAttribute in the VAB

Topics: Validation Application Block
Oct 9, 2012 at 11:24 AM
Edited Oct 9, 2012 at 11:25 AM

I am trying to use RegisterAsMetadataTypeAttribute to separate my MetaData classes from the concrete class. 

 

 

//Assembly 1  Common.Entities.POCO 
public class Address 
{
        public string AddressLine1
        {
            get;
            set;
        }
}

//Assembly 2 Business.Validation
[RegisterAsMetadataTypeAttribute(typeof(Address))
public class AddressMetaData 
{
        [StringLength(50, MinimumLength = 10)]
        public string AddressLine1
        {
            get;
            set;
        }
}
var addressValidator = ValidationFactory.CreateValidator<Address>();

this addressValidator doesn't seem to be validation the DataAnnotations?? I am a bit new to VAB, and i guess i am missing something in the configuration to get this to work.. 

Any help or pointers will be appreciated..! 

Many Thanks..Bob.!

Oct 9, 2012 at 1:17 PM

Use the MetadataTypeAttribute and apply it to the Address class: [MetadataTypeAttribute(typeof(AddressMetaData)] .

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

Oct 9, 2012 at 1:44 PM

The AddressMetaData is not in the same assembly as Address. This requirement is the source of trying to use RegisterAsMetadataTypeAttribute 

Oct 10, 2012 at 9:19 AM

As far as I can see the RegisterAsMetadataTypeAttribute is only used by the configuration tool and not the Validation Application Block.  However, you should be able to specify Metadata in a separate assembly.

e.g. 

// Assembly 1
    [MetadataType(typeof(Metadata.CustomerMetaData))]
    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string SSN { get; set; }
        public Address Address { get; set; }
    }

    [MetadataType(typeof(Metadata.AddressMetaData))]
    public class Address
    {
        public string StreetAddress { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
    }

// Assembly 2
    public class CustomerMetaData
    {
        [StringLengthValidator(1, 25)]
        public string FirstName { get; set; }
        [StringLengthValidator(1, 25)]
        public string LastName { get; set; }
        [RegexValidator(@"^\d\d\d-\d\d-\d\d\d\d$")]
        public string SSN { get; set; }
        [ObjectValidator]
        public object Address { get; set; }
    }

    public class AddressMetaData
    {
        [StringLengthValidator(1, 50)]
        public string StreetAddress { get; set; }
        [ValidatorComposition(CompositionType.And)]
        [StringLengthValidator(1, 30)]
        [ContainsCharactersValidator("sea", ContainsCharacters.All)]
        public string City { get; set; }
        [StringLengthValidator(2, 2)]
        public string State { get; set; }
        [RegexValidator(@"^\d{5}$")]
        public string ZipCode { get; set; }
    }

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

Oct 11, 2012 at 11:00 AM

Thanks Randy, i haven't got he luxury to Referece Assembly2 in Aseembly1 as Assembly1 is POCO  :(

Cheers!

Oct 11, 2012 at 2:32 PM

Alternatively, you could look at using configuration to define the VAB rulesets.

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