Skip to content

Instantly share code, notes, and snippets.

@rcarubbi
Created September 12, 2025 23:14
Show Gist options
  • Select an option

  • Save rcarubbi/3a9d993cd1c65b2330633b9b06186f21 to your computer and use it in GitHub Desktop.

Select an option

Save rcarubbi/3a9d993cd1c65b2330633b9b06186f21 to your computer and use it in GitHub Desktop.
// Published Language (DTOs públicos do nosso Open Host Service)
public sealed record PlaceOrderRequestDto(
string CustomerCode,
string Currency,
IReadOnlyList<PlaceOrderItemDto> Items);
public sealed record PlaceOrderItemDto(string Sku, int Quantity, decimal UnitPrice);
public sealed record PlaceOrderResponseDto(
string OrderNumber,
decimal TotalAmount,
string Currency);
// Caso de uso (núcleo, independente de transporte)
public interface IPlaceOrderUseCase
{
PlaceOrderResult Execute(PlaceOrderCommand cmd);
}
// Modelos internos do caso de uso (não expostos)
public sealed record PlaceOrderCommand(
string CustomerCode, string Currency, IReadOnlyList<PlaceOrderLine> Lines);
public sealed record PlaceOrderLine(string Sku, int Quantity, decimal UnitPrice);
public sealed record PlaceOrderResult(string OrderNumber, decimal TotalAmount, string Currency);
// DTO Mapper: Published Language <-> modelos internos
public sealed class OrderDtoMapper
{
public PlaceOrderCommand ToCommand(PlaceOrderRequestDto dto)
{
var currency = string.IsNullOrWhiteSpace(dto.Currency) ? "USD" : dto.Currency.ToUpperInvariant();
var lines = dto.Items.Select(i => new PlaceOrderLine(i.Sku.Trim(), i.Quantity, i.UnitPrice)).ToList();
return new PlaceOrderCommand(dto.CustomerCode.Trim(), currency, lines);
}
public PlaceOrderResponseDto ToResponse(PlaceOrderResult result) =>
new(result.OrderNumber, result.TotalAmount, result.Currency);
}
// Remote Facade: pele coarse-grained; sem regra de domínio
public sealed class OrdersFacade
{
private readonly IPlaceOrderUseCase _useCase;
private readonly OrderDtoMapper _mapper;
public OrdersFacade(IPlaceOrderUseCase useCase, OrderDtoMapper mapper)
{
_useCase = useCase;
_mapper = mapper;
}
public PlaceOrderResponseDto Place(PlaceOrderRequestDto request)
{
var cmd = _mapper.ToCommand(request);
var result = _useCase.Execute(cmd);
return _mapper.ToResponse(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment