Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created November 11, 2025 19:51
Show Gist options
  • Select an option

  • Save dcomartin/272dbc24e04f90da75482b32e2de17bd to your computer and use it in GitHub Desktop.

Select an option

Save dcomartin/272dbc24e04f90da75482b32e2de17bd to your computer and use it in GitHub Desktop.
public class LessBloatedHandler
{
private readonly DB _db;
private readonly IEmailer _emailer;
private readonly IEventPublisher _eventPublisher;
private readonly ILogger _logger;
public LessBloatedHandler(DB db, IEmailer emailer, IEventPublisher eventPublisher, ILogger logger)
{
_db = db;
_emailer = emailer;
_eventPublisher = eventPublisher;
_logger = logger;
}
public void Handle(DispatchShipment command)
{
var shipment = _db.GetById(command.ShipmentId);
if (shipment == null)
{
throw new InvalidOperationException("Shipment not found.");
}
shipment.Dispatch();
_db.Save(shipment);
_emailer.NotifyCustomer(shipment);
_eventPublisher.Publish(new ShipmentDispatched(shipment.Id, shipment.DispatchedAt));
_logger.Log($"Shipment {shipment.Id} dispatched.");
}
}
public class Shipment
{
public Guid Id { get; private set; }
public ShipmentStatus Status { get; private set; }
public DateTime? DispatchedAt { get; private set; }
public void Dispatch()
{
if (Status != ShipmentStatus.Ready)
{
throw new InvalidOperationException("Shipment is not ready.");
}
Status = ShipmentStatus.Dispatched;
DispatchedAt = DateTime.UtcNow;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment