views:

27

answers:

1

In my ASP.NET application I have separate projects for the Data, Business and UI layers.

My business layer is composed of plain objects with declarative validation, using DataAnnotations.

Problem is, when it comes to save them, I'm not sure how to process the validation, since they're not bound directly to any data context, but rather, are mapped to separate data-layer objects.

Is there a way to trigger validation on these kinds of objects?

A: 

Found the answer (indirectly) on StackOverflow, on this post. (Thanks Atwood & Spolsky!)

Turns out you have to call the Validator class.

So I added a Validate() method to my POCO:

public void Validate()
{
    Validator.ValidateObject(this, new ValidationContext(this, null, null));
}

I also had to swap the .NET 3.5 version of my ComponentModel.DataAnnotations DLL for the updated .NET 4.0 version, which includes the ValidationContext class, etc.

jonathanconway