let's say I have the following aggregate:

class Road:
    company_id: int # another aggregate
    name: str
    stops: List[Stop]

    def add_stop(stop: Stop):

class Stop:
    precission: Precission
    latitude: float
    longitude: float
    address: str

This is just a minified example. Let's also say that to create stops, I need to call a third party to geocode the object. Something like:

# this returns a correctly initialized Stop object

Now let's say that I want to add a stop to a road, so I would have an application service that would be similar to something like this:

class AddStopToRoadService:
    def run(road_id, address):
        road = RoadRepository.getById(road_id)
        stop = StopsDataAccess.get_by_address(address)

As it is, I do not see a lot of problems with this approach. The problem I have is when the following domain requirement comes:

  • Only stops with the precision EXACT can be added to a route, those that are APPROXIMATE or UNKNOWN cannot be added. Also, this can be changed by every company, so for instance, a different company can accept only APPROXIMATE or all of them.

So, to approach this I do the following:

class Road:
    allowed_precission_types: List[Precissions]

    def add_stop(stop: Stop):
        if stop.precission not in self.allowed_precission_types:
            raise StopNotAllowed()

The allowed_precission_types would be filled by the RoadRepository. The problem I see with this approach is that that the list of allowed precision values is part of the Company aggregate. So I'm not sure if it really fits in there. Am I breaking encapsulation? What would be the correct approach?

  • "that value is part of the Company" - you mean "the list of allowed precision values", I guess?
    – Doc Brown
    Commented Apr 28, 2022 at 9:04
  • Correct! Fixed! Commented Apr 28, 2022 at 9:38
  • Is the allowed precision list on the Company aggregate mutable? If so, should a change to that collection impact already created roads?
    – Rik D
    Commented Apr 28, 2022 at 15:17
  • It’s mutable. That could be changed on the company aggregate and the roads restored using the repository should come with that value updated. Commented Apr 28, 2022 at 15:24
  • That complicates it a lot. So if an allowed precision is removed, does that impact already created Stops of that precision?
    – Rik D
    Commented Apr 28, 2022 at 18:49


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.