views:

236

answers:

2

Am using the enterprise library validation.

I have classes like below

 public class Customer
    {

        public int Id { get; set; }
        [NotNullValidator(MessageTemplate = "{1} is null")]
        public string FirstName { get; set; }
        [NotNullValidator(MessageTemplate = "{1} is null")]
        public string Surname { get; set; }

    }

  public class Order
    {

        public int Id { get; set; }
        [NotNullValidator(MessageTemplate = "{1} is null")]
        public Customer Customer { get; set; }

    }

When am using the order object and surname and first name are null validation should kick in ,but it does not.

What am i doing wrong?

Note: Am using it with WCF

A: 

You correctly declared your validation rules, however you didn't validate your customer (evaluate the rules). Validation with the enterprise library validation block works like this:

ValidationResults r = Validation.Validate<Customer>(myCustomer);

See the corresponding MSDN article.

Johannes Rudolph
This is not practical, because it doesn't enable validation of object graphs.
Steven
+2  A: 

You should decorate your Customer property with the [ObjectValidator] attribute. This will insure that the Validation Application will validate object graphs:

public class Order
{
    [ObjectValidator]
    public Customer Customer { get; set; }
}

Tip: You should read the ValidationHOL.pdf (hands on lab) document that comes with the Validation Application Block 4.1 Hands On Labs (the PDF is part of the download). It will give you a lot of information about the VAB that will be hard to find out on your.

Steven