public HandlingHistoryTests() { var shanghai = new BookingApi.Domain.Location.Location(UnLocodeHelpers.GetNewUnLocode(), "SHANGHAI"); var dallas = new BookingApi.Domain.Location.Location(UnLocodeHelpers.GetNewUnLocode(), "DALLAS"); var hongkong = new BookingApi.Domain.Location.Location(UnLocodeHelpers.GetNewUnLocode(), "HONGKONG"); _cargo = new BookingApi.Domain.Cargo.Cargo(new TrackingId("ABC"), new RouteSpecification(shanghai, dallas, new DateTime(2009, 4, 1))); _voyage = new VoyageBuilder(new VoyageNumber("X25"), hongkong) .AddMovement(shanghai, DateTime.Now, DateTime.Now) .AddMovement(dallas, DateTime.Now, DateTime.Now) .Build(); _handlingEvent1 = new HandlingEvent(HandlingEventType.Load, shanghai, new DateTime(100), new DateTime(2009, 3, 5), _cargo, _voyage); _handlingEvent1Duplicate = new HandlingEvent(HandlingEventType.Load, shanghai, new DateTime(200), new DateTime(2009, 3, 5), _cargo, _voyage); _handlingEvent2 = new HandlingEvent(HandlingEventType.Unload, dallas, new DateTime(150), new DateTime(2009, 3, 10), _cargo, _voyage); _handlingHistory = new HandlingHistory(new List<HandlingEvent> {_handlingEvent2, _handlingEvent1, _handlingEvent1Duplicate}); }
/// <summary> /// Creates a new delivery snapshot based on the complete handling history of a cargo, as well /// as its route specification and itinerary. /// </summary> /// <param name="specification">Current route specification.</param> /// <param name="itinerary">Current itinerary.</param> /// <param name="handlingHistory">Delivery history.</param> /// <returns>Delivery status description.</returns> public static Delivery DerivedFrom(RouteSpecification specification, Itinerary itinerary, HandlingHistory handlingHistory) { if (specification == null) throw new ArgumentNullException("specification", "Route specification is required"); if (handlingHistory == null) throw new ArgumentNullException("handlingHistory", "Handling history is required"); var lastHandlingEvent = handlingHistory.MostRecentlyCompletedEvent; return new Delivery(lastHandlingEvent, itinerary, specification); }
/// <summary> /// Updates delivery progress information according to handling history. /// Updates all aspects of the cargo aggregate status /// based on the current route specification, itinerary and handling of the cargo. /// <p/> /// When either of those three changes, i.e. when a new route is specified for the cargo, /// the cargo is assigned to a route or when the cargo is handled, the status must be /// re-calculated. /// <p/> /// <see cref="RouteSpecification" /> and <see cref="Itinerary" /> are both inside the Cargo /// aggregate, so changes to them cause the status to be updated <b>synchronously</b>, /// but changes to the delivery history (when a cargo is handled) cause the status update /// to happen <b>asynchronously</b> since <see cref="HandlingEvent" /> is in a different aggregate. /// </summary> /// <param name="handlingHistory">Handling history.</param> public virtual void DeriveDeliveryProgress(HandlingHistory handlingHistory) { // Delivery is a value object, so we can simply discard the old one // and replace it with a new Delivery = Delivery.DerivedFrom(RouteSpecification, Itinerary, handlingHistory); if (Delivery.IsMisdirected) { DomainEvents.Raise(new CargoWasMisdirectedEvent(this)); } else if (Delivery.IsUnloadedAtDestination) { DomainEvents.Raise(new CargoHasArrivedEvent(this)); } }
public void should_set_received_location_as_last_known_location() { var cargo = new BookingApi.Domain.Cargo.Cargo(new TrackingId("XYZ"), new RouteSpecification(_stockholm, _melbourne, DateTime.Now)); var handlingHistory = new HandlingHistory(new List<HandlingEvent> { new HandlingEvent(HandlingEventType.Receive, _stockholm, DateTime.Now, new DateTime(2007, 12, 1), cargo) }); cargo.DeriveDeliveryProgress(handlingHistory); Assert.Equal(_stockholm, cargo.Delivery.LastKnownLocation); }
public void should_set_off_location_as_last_known_location() { var cargo = new BookingApi.Domain.Cargo.Cargo(new TrackingId("XYZ"), new RouteSpecification(_stockholm, _melbourne, DateTime.Now)); var handlingHistory = new HandlingHistory(new List<HandlingEvent> { new HandlingEvent(HandlingEventType.Load, _stockholm, DateTime.Now, new DateTime(2007, 12, 1), cargo, _voyage), new HandlingEvent(HandlingEventType.Unload, _hambourg, DateTime.Now, new DateTime(2007, 12, 2), cargo, _voyage), new HandlingEvent(HandlingEventType.Load, _hambourg, DateTime.Now, new DateTime(2007, 12, 3), cargo, _voyage), new HandlingEvent(HandlingEventType.Unload, _hongkong, DateTime.Now, new DateTime(2007, 12, 4), cargo, _voyage) }); cargo.DeriveDeliveryProgress(handlingHistory); Assert.Equal(_hongkong, cargo.Delivery.LastKnownLocation); }