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

Introduction

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.

image

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

initialization

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"

https://guides.hanamirb.org/architecture/overview/

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.

other

  • 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!

Summarize

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.


shiweifu
2k 声望744 粉丝

引用和评论

0 条评论