Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created October 29, 2025 15:15
Show Gist options
  • Select an option

  • Save dcomartin/0968d298c25a4d869ecd3075708b224d to your computer and use it in GitHub Desktop.

Select an option

Save dcomartin/0968d298c25a4d869ecd3075708b224d to your computer and use it in GitHub Desktop.
public interface IShipmentReadinessRule
{
bool IsSatisfiedBy(Shipment shipment);
}
public class HasValidDestinationRule : IShipmentReadinessRule
{
public bool IsSatisfiedBy(Shipment shipment)
{
return !string.IsNullOrWhiteSpace(shipment.Destination);
}
}
public class AllPackagesPackedRule : IShipmentReadinessRule
{
public bool IsSatisfiedBy(Shipment shipment)
{
return shipment.Packages.All(p => p.IsPacked);
}
}
public class NotAlreadyShippedRule : IShipmentReadinessRule
{
public bool IsSatisfiedBy(Shipment shipment)
{
return shipment.Status != ShipmentStatus.Shipped;
}
}
public class Shipment
{
public string Destination { get; init; }
public ShipmentStatus Status { get; init; }
public IReadOnlyList<Package> Packages { get; init; }
public Guid CustomerId { get; private set; }
public bool CanShip(IEnumerable<IShipmentReadinessRule> rules)
{
return rules.All(rule => rule.IsSatisfiedBy(this));
}
}
public class Example
{
public bool CanShip(Guid shipmentId)
{
var shipment = Db.GetShipment(shipmentId);
var shipmentRules = Db.GetRulesByCustomer(shipment.CustomerId);
return shipment.CanShip(shipmentRules);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment