tl;dr I am searching for a way to establish a bidirectional association between to structs.
I am used to Rails and ActiveRecord. There, it is common to have bidirectional associations between objects, like a
has_many :order_items and
I am trying to move away from Rails style coding, and I am experimenting with different ways, like dry-struct. I’d like to create a domain layer of objects that offer rich behaviour (business logic) based on dry-struct. Therefore I view those objects not as pure data carriers (anemic model), but as full-fledged classes (like in OOP) that offer business logic through methods. Sometimes it is handy to be able to navigate an association in both ways.
A dry-struct object is immutable. After creation, I cannot change it. This makes it impossible to establish the bidirectional association. In the order example from above, I could instantiate all
OrderItem objects and then add them to the
Order on creation. Then I can navigate from an order to all its order items, but not the other way around. Or I could instantiate the
Order first and pass it to all
OrderItem objects on their creation. Than I can navigate from all order items to their parent order, but the order does not know its order items.
Am I missing something, or is this what I want just not possible? On a more general level, is dry-struct even a good choice for a rich domain layer as described above?