I am exploring app development with Hanami 2, which has led me to dive into the Dry-rb ecosystem as well. Loving it so far!
I am having some trouble understanding how to coordinate my usage of the various Dry-rb components to avoid logic duplication in my apps. I would love to hear from others how you are using the Dry-rb ecosystem to model domain objects and logic in your apps.
For example, I like the idea of building up a library of Types to represent all data that comes from outside of the system (user-submitted data, imported data, etc.). No primitives allowed!
However, these types can become quite complex when they represent domain objects with a dozen or more attributes. How do you avoid repeating these attributes and their constraints in multiple places?
For example, I think I can define a Hash Type representing a domain object, and then import the Type into a Dry-Struct definition to avoid having to list all of the attributes in two places (I haven’t really had to do this yet, as I’m still relying on ROM auto-structs). Is this a common practice?
What about schemas? I would also like to avoid repeating all of these attributes in a schema that I could use to parse user input params and other external data (that is, to coerce, transform, and validate the params). But inferring a schema from a Hash Type, while possible, is imperfect and can fail in crucial ways. I can provide a code example for this, but I don’t want to clutter up the initial post.
So . . . what are you all doing?
- How do you organize your Types?
- How do you re-use them to avoid duplicate logic?
- Do you use Types in Schemas and Structs, or is this a pipe dream?
- If you use Hanami, do you define Types for specific slices?
- In short, what are the best practices?
Thank you for reading and for any and all replies!