Domain Data Structures that hold Domain objects?

175 Views Asked by At

I have a position, and some entities that use position as it's identifier (geography, biome, and so on). If I want to get acess to them, I would need to retrieve each one by it's position, which would cause repeated code. On the other hand, I could create a class that is a container, like a "location". But, in this case, to retrieve geography (for example), I would need to break demeter's law.

Repository.getLocation().getGeography().getHighestPeak();

Is there any other approach to this, or a common pattern that I'm missing? Keep in mind that this type of objects (that relate to position in the way I described) are very likely to grow large in number after a few months.

1

There are 1 best solutions below

1
On

I'm not sure it's a good idea to have a Position as an ID

  • The Position of an entity is likely to change, which leads to an unstable identifier.

  • You won't be able to have multiple instances of a given entity with the same position.

On the other hand, I could create a class that is a container, like a "location". But, in this case, to retrieve geography (for example), I would need to break demeter's law.

Repository.getLocation().getGeography().getHighestPeak();

You're basically saying that a Location has an entity, which is a bit awkward. It seems much more natural to say that an entity has a position. How about

GeographyRepository.getGeographyByPosition(new Position(...)).getHighestPeak();

?