[Translation] Hanami in the eyes of a Rails developer

Translated from: Hanami: Thoughts of a Rails developer-DEV Community


I still remember the first time I heard about the Hanami framework, a few years ago, at the Wroclove.rb conference. At that time, it didn't really attract my attention. At that time, I just entered the Ruby world and I was 100% focused on learning Rails. I didn't want to receive information from another framework in the brain.

Now that I have been working with Rails for several years, I am a little tired, and I think it’s time to get in touch with new concepts. I remembered the talk of wroclove.rb, I decided to learn Hanami. My favorite way of learning is to learn by doing, so I started a toy project called flashcard-genius. The purpose of this app is to help me learn Italian words. It allows users to create, share and memorize multiple sets of cards.

In the next two months of my spare time, I developed this prototype product (already online: http://flashcard-genius.com ). It uses basic server-side rendering and CRUD operations. It forced me to learn various Hanami concepts and overcome many obstacles encountered during use.

In this article, I will share with you some of my impressions of Hanami.


Why use Hanami?

Luga Guidi, the author of Hanami, hopes to go Rails. His main vision is to build a secure, feature-rich Web framework. The structure of this framework follows the principle of concise architecture and relies on third-party libraries that have been tested in actual combat, such as Sequel, Rack, and dry-rb.

Sounds attractive, right? For me, this is true, especially because our beloved Rails is often criticized for becoming too complex and breaking some best practices (for example, mixing the domain and data layer).

Memory usage may also be a potential benefit of Hanami. Hanami may consume less memory and is faster (even five times) ( https://rpanachi.com/2016/03/28/from-rails-to-hanami-part1-container-architecture-model-views -assets).

what do I like


Initializing the Hanami application is very simple. start page explains how to do it:

  • Set up items
  • Create the first route and link the controller actions, views and templates that match it
  • Prepare for basic tests

A good first impression is very important to me, because I can see the changes of things intuitively, which makes me interested to continue to understand more complex concepts to build my app.

The Hanami project is a "tiny"


Hanami can allow multiple apps to exist in a project, which helps deconstruct and split the project after it grows. Similarly, each project has its own independent "lib" folder.

Entities, repositories, verification objects, and interactive responders replace the fat data model

Hanami tries its best to prevent developers from making common mistakes in Rails when using Hanami.

  • Compared to the direct use of database query methods in the model, Hanami provides storage classes. When you first started using it, it seemed a bit redundant, but you will feel her benefits in the future.
  • Instead of using lifecycle callbacks (after_create, after_build, etc.) that are inconvenient for testing, Hanami encourages you to use service objects called interactors.
  • The model in Hanami only provides read-only access to the corresponding table by default. Compared with the active record model, a large number of model methods (such as username, username_changed, username_did_change? etc.) are created by default, which is a very lightweight implementation.
  • The creators of Hanami concluded that the validator should not be part of the model because the validation of the data is highly dependent on the use case. Therefore, instead of providing a verification mechanism on the model, they created the Hanami::Validation mixin (based on an awesome Gem, dry-validation ). The controller in Hanami responds to events out of the box, but you can also create your own validator object according to your needs. If you want to learn more about why validators are an anti-pattern, I suggest you take a look at this video Piotr Solnica.

Data access and control

As I mentioned earlier, in Hanami, data query and control are through the corresponding storage class. The Hanmi developers decided to introduce Sequel , another top gem, to complete the database communication, you can thank the author of this awesome thing, Jeremy Evans.

One of the reasons I like Hanami's storage idea is that when I define an accessor, Hanami's storage will not create additional public structures for me. Hanami will only create these methods when they are accessed.

Here are some additional advantages that I have not used yet, which I think belong to Hanami:

There is no global view helper method

The template uses the corresponding view class as the backend. In Rails, these view helper methods will be implemented as global helper methods. This makes it difficult to test some of the view methods used in ERB templates.


  • Hanami is based on Rack, which allows her to use many Rack middleware (error tracking, access frequency restriction, etc.)
  • The built-in terminal of Hanami is similar to the functions provided by Rails terminal, so it is easy to use
  • Hanami is a modern web framework that provides a large number of modern web development features (resource management, DB migration system, mail system, security priority design, etc.)

What is unsatisfactory?

Small ecological environment

Obviously, Hanami is a much smaller project than Rails, so there are not many related gems, manuals, etc. Similarly, Rails has many SDKs for third-party services, which you can use directly in Rails. In Hanami, you often need to spend some extra time to integrate in these places.

The tutorial is too simple

The tutorial only covers some very basic usage. A large number of choices are scattered among forums, gitter chat rooms, and stack overflow. The documentation can be improved a bit. Can provide a simple CRUD case. Now, I usually have to read various posts to understand how to implement a certain function.

In addition, the fact that some great gems are incorporated into Hanami is sometimes a curse, because you have to understand which APIs are based on what gems, and then delve into the code/documentation of the gems to understand how to achieve what you want.

Using repositories is sometimes a bad thing

Maybe I haven't spent enough time on the Hanami repository and Sequel, but I didn't really see an easy way to DRYing the various parts of the query, so I ended up writing a query method with many common parts.

I still remember that I encountered difficulties when writing a method based on the GROUP BY clause that returns additional column storage. It took me a long time to solve it. If the document contains more complex use cases like this, that would be great!


I have to admit, I really like to use Hanami. However, I do not recommend that you use it to build any large-scale projects, or to convert existing projects, unless you are ready to spend a lot of time in forums and chat rooms to solve many problems that have already been solved in the Rails ecosystem.

However, I suggest you use it to realize some of your new ideas, and I will continue to use it to build my non-main projects. Thank you for reading.

一些 Web 开发方面的技术文章,有翻译,有原创
1.9k 声望
743 粉丝
0 条评论
前言GC(Garbage collection)频繁和堆内存溢出原因简单来说是对象占用堆空间难以回收,新对象无法分配触发GC或者直接导致内存溢出,最终进程结束。排查思路是先查看进程各种类型对象占用空间大小和比例,锁定占...

运维社阅读 560

大屏是什么呢?再我前几年刚接触这个词得时候很新颖,全名叫态势感知大屏,大屏得特点是炫酷、好看,给用户满满得科技感。 听一位前辈说当年再招标会上,再都用exel、word做界面图表文档得时候,有一家公司把可视...

运维社阅读 421


运维社阅读 330

Spring Security入门学习
认识Spring SecuritySpring Security 是为基于 Spring 的应用程序提供声明式安全保护的安全性框架。Spring Security 提供了完整的安全性解决方案,它能够在 Web 请求级别和方法调用级别处理身份认证和授权。因为...

运维社阅读 310

前言在上一篇文章中:Netty:ChannelPipeline和ChannelHandler为什么会鬼混在一起? - 掘金 (juejin.cn)已经提到了ChannelPipeline里面维护了ChannelHandler的链表,ChannelHandler主要就是负责里面的时间进行拦...

运维社阅读 215


运维社阅读 112

1.9k 声望
743 粉丝