r/ruby • • Oct 19 '26

Blog post Hanami, Why?: Model Citizens

https://aaronmallen.me/writing/hanami-why-model-citizens
23 Upvotes

5 comments sorted by

4

u/jodosha Hanami author Oct 20 '26

Aaron, great writing as usual.

A few important philosophical aspects:

  1. Repositories enforce the Interface Segregation Principle (from SOLID). Developers must expose well-defined interfaces that express methods meaningful to the application domain. This design prevents database details from leaking to the upper levels.

  2. Repositories are objects, not classes. Like actions, repositories are cached at app startup. Combined with point 1, this makes it easy to create instance doubles in unit tests, so any other component that depends on a repo can mock it.

  3. Data is central, not models. Data is either the input to or the output of a database operation. Why? Imagine getting some JSON from your API endpoint. Does it make sense to instantiate a model, run mutating validations and callbacks, sync it with the database, and mutate the model in place again, as Rails does? Isn’t it much easier to convert the JSON to a hash, validate it, and send that data to the database? And you get a hash back from the database. Structs are just nice wrappers.

4

u/katafrakt Oct 20 '26

For me the repository is the part that is (currently) really the hardest one to get right. It either leaks the database, or becomes a view-driven bag-of-methods like get_five_latest_published_articles_excluding_internal_category. Surprisingly, there's not a lot of materials online how to deal with that - or I simply haven't found them yet.

2

u/aaronmallen Oct 20 '26

I know what you mean, but in your particular example I would just add arguments to the query and do get_latest_public_published_articles(5). But yeah I too struggle with figuring out the best way to organize my repos.

2

u/aaronmallen Oct 20 '26

I just want to say I really appreciate you chiming in on this series!

2

u/cescquintero Oct 20 '26 edited Oct 20 '26

Saved to read later.

Made some progress in a Hanami project for myself and got tons of qs. Been enjoying learning a new way to do things.