views:

23

answers:

1

I have a class library that contains my object model. I'd like each object to have data annotations to place validation rules into my model so that validation can be shared across 2 apps. One is an MVC 2 app and the other is a Windows forms app.

I need to be able to validate the object model manually from code using the data annotations, but without using xVal. When I switch the object model library to the 4.0 client profile it can no longer build with the xVal components. The domain object class library will be distributed with the windows app, so I wanted to utilize the 4.0 client profile.

Any ideas?

+1  A: 

Well, I'll provide an answer with a little more substance in case it might be of help to someone else.

For our validation, we use a simple Validate method like this:

public void Validate(T entity)
{
    var context = new ValidationContext(entity, null, null);
    var results = new List<ValidationResult>();

    bool valid = Validator.TryValidateObject(entity, context, results, true);

    if (!valid)
        ; // do something fancy with the results here, perhaps
}

You can also skip the TryValidateObject and go right to ValidateObject if you don't want to do anything fancy with the results.

ladenedge