This project is read-only.

Using xVal with a decoupled view model (DTO)

Sep 8, 2009 at 8:01 AM


xVal looks awesome and I really think we'll be using it in our project. However, I can't wrap my head around how to use xVal when using a specific class containing the data for the view to display.

For example having an entity in my domain model called Book with a property Name of type BookName.

public class Book {
    public BookName Name { get; set;}

public class BookName {
    public string Title { get; set; }

I don't want to expose Book to my View, instead I want to hand the view a BookForm instance and map between Book and BookForm.

public class BookForm {
    public string BookTitle { get; set; }

How would I add validation with xVal to a design like this?

  • If I add the validation attributes to the BookForm class and the system has a webservice for creating books which doesn't use BookForm but instead uses someother DTO the validation information in the BookForm class wouldn't be used and we had to duplicate the validation rules in another DTO.
  • On the other hand putting the validation rules on the domain object (Book) would mean no clientside validation, unless we use the "buddy" class feature. But that means keeping the same name on the properties in both classes?
  • A third alternative I guess would be to use the domain entity wrapped in a container class which gets handed to the view but I'd rather not do that either.

Whats best practice when designing a system like this? I know this might not be the correct forum to post this question but you guys seems to have a lot of good ideas about validation :)