Creating Layers



Creating Layers

0 0


presentation-rails-layers

my presentation for MountainWestRuby conf 2014

On Github hqmq / presentation-rails-layers

Creating Layers

Michael Ries

Twitter @hqmq_

Github @hqmq

Component-based Architectures in Ruby and Rails

By Stephan Hagemann

http://www.confreaks.com/videos/2350-mwrc2013-component-based-architectures-in-ruby-and-rails

Make Boxes

By Stephan Hagemann

http://www.confreaks.com/videos/2350-mwrc2013-component-based-architectures-in-ruby-and-rails

Stephen's List

One App Modules Gems => Local Gems Rails Engines Looser Coupling => Domain Objects Services => Service Objects eco-system => Published Gems HTTP SOA Gems => Local Gems Looser Coupling => Domain Objects => Narrow API Services => Service Objects Eco-System => Published Gem => Collection of Gems/Libraries useful across applications

Layer

Box + Direction

Definition: A Layer is a box with directional dependency.
If you raise the level of abstraction, but all the boxes still talk to eachother we can quickly run into complexity problems.
We can put a lot more things into this diagram before it gets confusing.

Example 1

New Feature

If you are thinking about changing the structure of an application it is often easier to try it out on a new feature than than refactoring an existing feature.
Why is it broken? Because we don't persist information about course records, so we have to iterate through every game to determine who holds records and hand out points. Dynamically assigning points this way costs ~3sec for a database of 26 players and 24 games...

Example 1

Persisted Course Records

Protip: Pick a slice as small as possible.
              
                class CreateCourseRecords < ActiveRecord::Migration
                  ...
                end
              
            
So what would it take to make it work within our application? A Migration...
              
                class CourseRecord < ActiveRecord::Base
                  ...
                end
              
            
A model
              
                module UpdateCourseRecordsOnCreate
                  extend ActiveSupport::Concern
                  included do
                    after_save :recalculate_records
                  end
                end
              
            
Some sort of observer to recalculate records when new games are created.
              
                require 'sinatra'

                post '/game.json' do
                  ...calculate records...
                end

                get '/:course_id/records.json' do

                end
              
            
So what would it take to make it work as a new service? A Sinatra app...

+ All the stuff we mentioned before

Layer

Box + Direction

Definition: A Layer is a box with directional dependency.
If you raise the level of abstraction, but all the boxes still talk to eachother we can quickly run into complexity problems.
We can put a lot more things into this diagram before it gets confusing.