I've been doing a lot of reading about DTO's recently, having never used them before. One article I read spoke about how DTO's should not just replicate domain model objects but rather each DTO should be tailored to the service operation being performed.
This got me thinking about validation if I decided to used DTO's and wondered if the following would be acceptable?
public class Person {
public Guid Id {get; set;}
public string Name {get; set;}
public Address Address {get; set;}
}
public class Address {
public Guid Id {get; set;}
public string AddressLine1 {get;set;}
...
}
publuc class CreatePersonDTO {
private string _name;
private string _addressLine1;
public string Name {
get {
if (_name == null)
throw new Exception("Missing");
return _name;
}
set { _name = value; }
}
public string AddressLine1 {
get { return _addressLine1; }
set { _addressLine1 = value; }
}
}
So you pass your data through in either Json or Xml and it is serialised to the object CreatePersonDTO and when mapping those values to create a Person and Address object if the Persons Name is missing then it will throw a validation exception.
As the DTO itself is service operation specific so is having this validation here ok or is it breaking some kind of rule regarding where business logic should reside?