Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

Lines 38 to 43 could be classed as domain logic as this is how you create a valid cargo. Given that the booking service is an application service it should be delegating to the domain layer to create a cargo. Maybe the code would look like so...

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);
42  
43      final Cargo cargo = CargoFactory.create(trackingId, origin, destination);

Check out my free book on DDD https://leanpub.com/Practicing-DDD for more info on factory implementations.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.