Partial success with validation context

Jun 27, 2012 at 5:32 PM
Edited Jun 27, 2012 at 5:33 PM

By adding the following code into album.cs , you can validate between fields within the model at an object level ( especially useful for dates ...is date 1 later than date 2 ?) 

1 Change 

public class Album

to 

public class Album : IValidatableObject   

add the following code after any real or virtual getters & setters  

( In some versions that is public string AlbumArtUrl { get; set; }

in others it s  public virtual List<OrderDetail> OrderDetails { get; set; })

 

 public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)       

{           

            if (this.field1 > this.field2)           

 {               

 yield return new ValidationResult("This has tripped validation result 1 ");           

 }

 

            if (  if (this.field4 > this.field4)           )     

{               

yield return new ValidationResult("This has tripped validation result 2");           

}       

}

 

If any one has any ideas how to extend this validation with a query to see if if I have already got this album by this artist, then I'd be grateful,