Sample code from Norway

Here's the zipped up sample code that I've been showing during my Metadata talk at the VS.NET launch event in Oslo. It's demonstrating yet another use of the constraint attributes that I've written quite a while ago. The idea is simple. When you use restriction facets on types defined in XSD and map them into a programming model, most of the facets get crippled away. They get totally lost when XSD.EXE generates classes and only length constraints survive DataSet code generation. Now, if you use the code in this archive, you can retrofit your classes with the appropriate facets like this:

 [System.Xml.Serialization.XmlTypeAttribute(
    Namespace="urn:schemas-newtelligence-com:
    transactionsamples:customerdata:v1")]
 public class customerType 
 {
  [Match(@"\p{L}[\p{L}\p{P}0-9\s]+"),MaxLength(80)]
  public string FirstName;
        
  [Match(@"\p{L}[\p{L}\p{P}0-9\s]+"),MaxLength(80)]
  public string LastName;
        
  [Match(@"[0-9\+][0-9\-\(\)\s]*"),MaxLength(26)]
  public string HomePhone;
        
  [Match(@"[0-9\+][0-9\-\(\)\s]*"),MaxLength(26)]
  public string BusinessPhone;
        
  [Match(@"[0-9\+][0-9\-\(\)\s]*"),MaxLength(26)]
  public string MobilePhone;
 }

.. and then you simply run an entire object graph through the validation function provided by the ConstraintValidator class like this:

ConstraintValidator cv = new ConstraintValidator();
cv.Validate( customers );

If validation fails, you get an exception with a detailed description of the problem. The net result is that all validation happens on the object graph and not using a serialization step and a run through the XmlValidatingReader. That saves time and allows you to keep promises in regards to XSD based message contracts even if your data will never be serialized into angle brackets. XSD rocks. Consider queued components. Voilá.

Update: I got an email comment detailing an (obviously pretty stupid) bug that I have in the validation function. I'll take a look at it and will post an update shortly. Fixed.

Updated: