Introducing hexagonal.js

There’s an idea we were working on for more than one year till now. As backend developers we were thrown into mysterious world of frontend (client-side) apps without any good pattern how to create Single Page App. So we (GameBoxed + Arkency) invented one - hexagonal.js.

Our main inspiration was Alistair Cockburn’s Hexagonal architecture. In short hexagonal.js is its JavaScript’s implementation, but with some unusual solutions and additional philosophy. Let’s focus on philosophy first, because it’ll let you judge if you’re still interested in this idea.

hexagonal.js philosophy

Business logic is software’s heart and have to be exposed properly.

Business logic is pure: uses only objects that represents domain in domain-valid state.

Client-side app is core of whole project, should be implemented as first.

Server-side API development should be driven by client-side needs.

Client-side and server-side are separated.

Both layers implements MVC.

In this post I want to focus on client-side layer, because it is most interesting part. All snippets in this post are copied from hexagonal.js’ hello-world project.

Structure

Architecture is build with:

Business logic

Use cases

Models

Glue (ports)

Adapters

GUI

Server-side

WebSockets, LocalStorage etc.

Business logic

You’re probably familiar with use case term. If you have some experience with DCI (or figured it out different way) you probably know, that use cases can be represented as objects - and this is core idea.

Our story is quite simple: we want to greet user that uses app - ask for his name and greet him using name. As you can see it uses only plain objects and don’t care about booting, GUI or storage.

Adapters

This sample app has only one adapter, the most basic - GUI. Let’s have a look at code of GUI for just first step of UseCase - askForName. GUI#showAskForName shows simple form and binds to click event of its confirm button. It has no idea about domain objects and doesn’t contain any logic.

Conclusion

In about two weeks our beautiful city will host wroc_love.rb conference. If you’re going to participate in this event I have small announcement for you - I’ll provide QA session and hackathon / workshop on hexagonal.js on friday. It’s not an official part of conference and of course it will be free, to confront (production proved) idea with other developers. If you want to join please leave a comment, ping me on Twitter or just look for me on friday. There’ll be also fight between ember.js and hexagonal.js on saturday. To be honest: whole agenda looks very promising.