-
-
Save dcomartin/0968d298c25a4d869ecd3075708b224d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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