design patterns – Should the DTO be able to parse itself from and to Domain?


I’ve posted this on stackoverflow without much success, so I am reposting it here in hopes of it getting some attention.

Let’s say there’s the class Book, with different models in different endpoints:

Endpoint A (consumer):

class Book{
  Map<string,string> chapterName_content
}

Endpoint B (provider):

class Book{
  string () chapterNames
  string () content
}

Let’s also assume that these are the structures that make the most sense within each endpoint, so changing them is not recommended.

Since the conversion is a direct one, I do not see the need to use Mapper classes or anything of the kind. To me the answer would be to create a DTO on Endpoint A that already does the mapping as such:

class BookDTO{
  string () chapterNames
  string () content
  
  public Book toBook()      
  public string toJson() 

  static BookDTO fromBook(Book book)
  static BookDTO fromJson(string jsonRepresentation)
}
  

Is not using an external class for the mapping a bad practice?