0

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):
        stops.append(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
StopsDataAccess.get_by_address(address)

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)
        road.add_stop(stop)
        RoadRepository.save(road)

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()
        stops.append(stop)

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?

7
  • "that value is part of the Company" - you mean "the list of allowed precision values", I guess?
    – Doc Brown
    Apr 28 at 9:04
  • Correct! Fixed! Apr 28 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
    Apr 28 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. Apr 28 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
    Apr 28 at 18:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.