Best Practices For Mapping DTO to Domain Object?

前端 未结 11 1370
执念已碎
执念已碎 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: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?

提交回复
热议问题