Best Practices For Mapping DTO to Domain Object?

前端 未结 11 1207
执念已碎
执念已碎 2020-11-28 19:43

I\'ve seen a lot of questions related to mapping DTOs to Domain Objects, but I didn\'t feel they answered my question. I\'ve used many methods before and have my own opinio

相关标签:
11条回答
  • 2020-11-28 20:09

    You could use an automapper such as the one written by Jimmy Bogard which has no connection between the objects and relies on naming conventions being adhered to.

    0 讨论(0)
  • 2020-11-28 20:11

    A benefit of having a mapper that sits between your domain and your DTO is not as appearent when you are only supporting a single mapping, but as the number of mappings increases, having that code isolated from the domain helps keep the domain simpler and leaner. You won't be cluttering your domain with a lot of extra weight.

    Personally, I try and keep the mapping out of my domain entities and put the responsibility in what I call "Manager / Service layer". This is a layer that sits between the application and the respository(ies), and provides business logic such as workflow coordination (If you modify A, you might have to also modify B so service A will work with Service B).

    If I had a lot of possible ending formats, I might look at creating a plugable formatter that could use the Visitor pattern, for example to transform my entities, but I've not found a need yet for anything this complex.

    0 讨论(0)
  • 2020-11-28 20:14

    Another possible solution: http://glue.codeplex.com.

    Features:

    • Bidirectional mapping
    • Automatic mapping
    • Mapping between different types
    • Nested mapping and Flattening
    • Lists and Arrays
    • Verification of relations
    • Testing the mapping
    • Properties, Fields and Methods
    0 讨论(0)
  • 2020-11-28 20:14

    We can use Factory, Memento, and Builder pattern for that. Factory hide the details on how to create instance of domain model from DTO. Memento will take care the serialization/deserialization of the domain model to/from DTO and can even access private members. The Builder will allows mapping from DTO to domain with fluent interface.

    0 讨论(0)
  • 2020-11-28 20:16

    Why not we can do like this?

    class UserDTO {
    }
    
    class AdminDTO {
    }
    
    class DomainObject {
    
     // attributes
     public DomainObject(DTO dto) {
          this.dto = dto;
     }     
    
     // methods
     public function isActive() {
          return (this.dto.getStatus() == 'ACTIVE')
     }
    
     public function isModeratorAdmin() {
          return (this.dto.getAdminRole() == 'moderator')
     }
    
    }
    
    
    userdto = new UserDTO();
    userdto.setStatus('ACTIVE');
    
    obj = new DomainObject(userdto)
    if(obj.isActive()) {
       //print active
    }
    
    admindto = new AdminDTO();
    admindto.setAdminRole('moderator');
    
    obj = new DomainObject(admindto)
    if(obj.isModeratorAdmin()) {
       //print some thing
    }
    

    @FrederikPrijck (or) someone: Please suggest. In the above example DomainObject is depends on DTO. By this way i can avoid the code to do mapping the dto <--> domainobject.

    or DomainObject class can extends the DTO class?

    0 讨论(0)
  • 2020-11-28 20:22

    How do you see to implement a constructor inside the DTO class that takes as a parameter a domain object?

    Say... Something like this

    class DTO {
    
         // attributes 
    
         public DTO (DomainObject domainObject) {
              this.prop = domainObject.getProp();
         }
    
         // methods
    }
    
    0 讨论(0)
提交回复
热议问题