wilsonsantosphx

Mapper Extension

Aug 14th, 2025
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. // =======================
  2. // Alternativa: Métodos de Extensão (explícitos e desacoplados)
  3. // Úteis quando você prefere evitar operadores.
  4. // =======================
  5. public static class PedidoMappingExtensions
  6. {
  7.     public static Pedido ToDomain(this PedidoDto dto)
  8.     {
  9.         if (dto is null) throw new ArgumentNullException(nameof(dto));
  10.         if (string.IsNullOrWhiteSpace(dto.Id)) throw new ArgumentException("Id obrigatório no DTO");
  11.         if (string.IsNullOrWhiteSpace(dto.Cliente)) throw new ArgumentException("Cliente obrigatório no DTO");
  12.         if (dto.Data is null) throw new ArgumentException("Data obrigatória no DTO");
  13.         if (dto.Total is null) throw new ArgumentException("Total obrigatório no DTO");
  14.  
  15.         return Pedido.Criar(dto.Id!, dto.Cliente!, dto.Data.Value, dto.Total.Value);
  16.     }
  17.  
  18.     public static PedidoDto ToDto(this Pedido model)
  19.     {
  20.         if (model is null) throw new ArgumentNullException(nameof(model));
  21.         return new PedidoDto
  22.         {
  23.             Id = model.Id,
  24.             Cliente = model.Cliente,
  25.             Data = model.Data,
  26.             Total = model.Total
  27.         };
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment