Newcastle is failing tech entrepreneurs. What can we do about it?

It was great to see Newcastle chosen by Rough Guides as their top place to visit in 2018. It’s a great reflection of the uniqueness of the city. Over the last two years I’ve watched the energy and…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Decouple your Elixir application from Phoenix and Ecto

06 Aug 2017

I came to Elixir and Phoenix from a Ruby and Rails background. I think Rails applications (most of them that follow standard way) have a lot of messy code. Rails has a good architecture for a web framework but does not promote good practices for writing a web application. Everything is tied up with Rails, a lot of business logic and ActiveResource related code goes into models (fat models approach), controllers and into views so everything becomes tied up with the framework, becomes highly coupled. There is no clear separation between Rails and business logic code.

By default there is no service layer that extracts business logic from models and controllers. Because of this it’s hard to write tests. You need to test the whole stack, involving http requests and database access. As application grows tests tend to become very time consuming to run and you may give up on them because they are hard to do.

Coming to Elixir and Phonenix I encountered the same problem at the beginning. There are no standardized practices suggesting separation between Phoenix and business logic. With the coming of Phoenix 1.3 things changed. Web folder has been moved to lib, repo has been extracted from web and models folder is gone. There are a lot of changes in the right direction. Ecto and business logic is extracted from web and it is suggested that your application should be separate from the Phoenix framework. But still I think it’s not enough, Ecto is still coupled with your business logic.

There are a few rules that I keep in mind when writing web applications:

For example taking a look at a house plan you know exactly what the plan is for and what is composed of (bedroom, bathroom, kitchen, living-room). But looking at a standard Phoenix app the intent is not so clear any more. Always you will see the standard structure, controller, model, views, router. The only thing that it says is that you have a web application but not what it does.

Following this principles I suggest using this approach:

Each layer of the application can be separated in a separate umbrella application for clear differentiation. Then in each app I suggest to make a folder structure based on the business logic not on the class types.

Where:

Add a comment

Related posts:

Private Collection

Only a few minutes to go before the lesson was about to start… the buzzing on the corridors subsided, the students went quiet. The top grammar school of this county town was famous for a strong…