Padrão DTO em XOOPS
Padrão DTO (Data Transfer Objects) em XOOPS
Seção intitulada “Padrão DTO (Data Transfer Objects) em XOOPS”2.5.x ✅ 4.0.x ✅
Data Transfer Objects (DTOs) são objetos simples usados para transferir dados entre diferentes camadas de uma aplicação. DTOs ajudam a manter limites claros entre camadas e reduzem dependências de objetos de entidade.
Conceito de DTO
Seção intitulada “Conceito de DTO”O que é um DTO?
Seção intitulada “O que é um DTO?”Um DTO é:
- Um objeto de valor simples com propriedades
- Imutável ou somente leitura após criação
- Sem lógica de negócio ou métodos
- Otimizado para transferência de dados
- Independente de mecanismos de persistência
Quando Usar DTOs
Seção intitulada “Quando Usar DTOs”Use DTOs quando:
- Transferir dados entre camadas
- Expor dados através de APIs
- Agregar dados de múltiplas entidades
- Ocultar detalhes de implementação interna
- Alterar estrutura de dados para diferentes consumidores
Implementação Básica de DTO
Seção intitulada “Implementação Básica de DTO”<?phpclass UserDTO{ private $id; private $username; private $email; private $isActive; private $createdAt;
public function __construct($entity = null) { if ($entity instanceof User) { $this->id = $entity->getId(); $this->username = $entity->getUsername(); $this->email = $entity->getEmail(); $this->isActive = $entity->isActive(); $this->createdAt = $entity->getCreatedAt(); } }
// Acessores somente leitura public function getId() { return $this->id; } public function getUsername() { return $this->username; } public function getEmail() { return $this->email; } public function isActive() { return $this->isActive; } public function getCreatedAt() { return $this->createdAt; }
public function toArray() { return [ 'id' => $this->id, 'username' => $this->username, 'email' => $this->email, 'isActive' => $this->isActive, 'createdAt' => $this->createdAt, ]; }
public function toJson() { return json_encode($this->toArray()); }}?>DTO de Requisição/Entrada
Seção intitulada “DTO de Requisição/Entrada”<?phpclass CreateUserRequestDTO{ private $username; private $email; private $password; private $errors = [];
public function __construct(array $data) { $this->username = $data['username'] ?? ''; $this->email = $data['email'] ?? ''; $this->password = $data['password'] ?? '';
$this->validate(); }
private function validate() { if (empty($this->username) || strlen($this->username) < 3) { $this->errors['username'] = 'Username too short'; }
if (empty($this->email) || !filter_var($this->email, FILTER_VALIDATE_EMAIL)) { $this->errors['email'] = 'Invalid email'; }
if (empty($this->password) || strlen($this->password) < 6) { $this->errors['password'] = 'Password too short'; } }
public function isValid() { return empty($this->errors); }
public function getErrors() { return $this->errors; }
public function getUsername() { return $this->username; } public function getEmail() { return $this->email; } public function getPassword() { return $this->password; }}?>Uso em Serviços
Seção intitulada “Uso em Serviços”<?phpclass UserService{ public function createUserFromRequest(CreateUserRequestDTO $dto) { if (!$dto->isValid()) { throw new ValidationException('Invalid input', $dto->getErrors()); }
$user = new User(); $user->setUsername($dto->getUsername()); $user->setEmail($dto->getEmail()); $user->setPassword($dto->getPassword());
$userId = $this->userRepository->save($user);
return new UserDTO($user); }}?>Uso em Controladores de API
Seção intitulada “Uso em Controladores de API”<?phpclass ApiController{ public function createUserAction() { $input = json_decode(file_get_contents('php://input'), true); $dto = new CreateUserRequestDTO($input);
if (!$dto->isValid()) { http_response_code(400); return ['success' => false, 'errors' => $dto->getErrors()]; }
try { $userDTO = $this->userService->createUserFromRequest($dto); http_response_code(201); return ['success' => true, 'data' => $userDTO->toArray()]; } catch (\Exception $e) { http_response_code(500); return ['success' => false, 'message' => $e->getMessage()]; } }}?>Melhores Práticas
Seção intitulada “Melhores Práticas”- Mantenha DTOs focados e específicos
- Torne DTOs imutáveis ou somente leitura
- Não inclua lógica de negócio em DTOs
- Use DTOs separados para entrada e saída
- Documente claramente as propriedades de DTO
- Mantenha DTOs simples - apenas contêineres de dados
Documentação Relacionada
Seção intitulada “Documentação Relacionada”Veja também:
- Service-Layer para integração de serviço
- Repository-Pattern para acesso de dados
- MVC-Pattern para uso de controlador
- Testing para teste de DTO
Tags: #dto #data-transfer-objects #design-patterns #module-development