Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // =======================
- // Camada de Aplicação (DTO)
- // Obs.: Declaramos os operadores AQUI para evitar acoplar o Domínio a DTOs.
- // =======================
- public sealed class PedidoDto
- {
- public string? Id { get; init; }
- public string? Cliente { get; init; }
- public DateTime? Data { get; init; }
- public decimal? Total { get; init; }
- // Conversão EXPLÍCITA: DTO -> Domain (pode falhar por validação; exige cast)
- public static explicit operator Pedido?(PedidoDto? dto)
- {
- if (dto is null) return null;
- // Validações mínimas antes de ir ao domínio
- if (string.IsNullOrWhiteSpace(dto.Id)) throw new ArgumentException("Id obrigatório no DTO");
- if (string.IsNullOrWhiteSpace(dto.Cliente)) throw new ArgumentException("Cliente obrigatório no DTO");
- if (dto.Data is null) throw new ArgumentException("Data obrigatória no DTO");
- if (dto.Total is null) throw new ArgumentException("Total obrigatório no DTO");
- return Pedido.Criar(dto.Id!, dto.Cliente!, dto.Data.Value, dto.Total.Value);
- }
- // Conversão IMPLÍCITA: Domain -> DTO (não deve falhar; sem perda de info)
- public static implicit operator PedidoDto?(Pedido? model)
- {
- if (model is null) return null;
- return new PedidoDto
- {
- Id = model.Id,
- Cliente = model.Cliente,
- Data = model.Data,
- Total = model.Total
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment