I was trying to find examples for using DDD factories and I came across the shipping application from Eric Evans' book.
However when I checked the BookingService the code to create a Cargo
had this comment at line 37:
34 public TrackingId bookNewCargo(final UnLocode originUnLocode,
35 final UnLocode destinationUnLocode,
36 final Date arrivalDeadline) {
37 // TODO modeling this as a cargo factory might be suitable
38 final TrackingId trackingId = cargoRepository.nextTrackingId();
39 final Location origin = locationRepository.find(originUnLocode);
40 final Location destination = locationRepository.find(destinationUnLocode);
41 final RouteSpecification routeSpecification = new RouteSpecification(origin, destination, arrivalDeadline);
42
43 final Cargo cargo = new Cargo(trackingId, routeSpecification);
44
45 cargoRepository.store(cargo);
46 logger.info("Booked new cargo with tracking id " + cargo.trackingId().idString());
47
48 return cargo.trackingId();
49 }
How should the DDD factory be used here? Which parts exactly should be refactored to use the factory in these piece of code?