Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[KRUG] Architecture. The reclaimed years.

[KRUG] Architecture. The reclaimed years.

A short intro into clean architecture for Ruby apps based on dry-system gem.

Piotr Solnica

March 20, 2018
Tweet

More Decks by Piotr Solnica

Other Decks in Programming

Transcript

  1. "The top level architecture of my rails application, did not

    scream its intent at you, it screamed the framework at you, it screamed Rails at you" — Uncle Bob 5
  2. 12

  3. 13

  4. 17

  5. 18

  6. class CreateUser attr_reader :user_repo, :validator, :mailer def initialize(user_repo:, validator:, mailer:)

    @user_repo = user_repo @validator = validator @mailer = mailer end end 20
  7. 24

  8. > Minimize state in classes > Don't rely on class

    state at runtime > Don't rely on monkey-patching 25
  9. > Using singleton methods couple your code to class/ module

    constants > Singleton methods easily lead to awkward, procedural code 31
  10. > Minimize usage of singleton methods, they are only good

    as "builder" methods, or top-level configuration APIs > Don't use them at runtime, objects are 10 x better and more flexible 32
  11. > An architecture for Ruby applications > Based heavily on

    lightweight dependency injection > Allows you to compose an application from isolated components 34
  12. 35

  13. # app/system/app.rb require 'dry/system/container' class App < Dry::System::Container configure do

    |config| config.auto_register = %w(lib) end load_paths! 'lib', 'system' end 40
  14. SIMPLE OBJECT COMPOSITION require 'import' module Users class CreateUser include

    Import['repos.user_repo'] def call(params) user_repo.create(params) end end end 41
  15. YOUR APP IS THE ENTRY POINT TO YOUR SYSTEM ∞

    pry -r ./system/app [1] pry(main)> App['users.create_user'] => #<Users::CreateUser:0x00007ff3a1b2e520..> [2] pry(main)> App['repos.user_repo'] => #<Repos::UserRepo:0x00007ff3a11b0890..> 42
  16. 43

  17. TESTING IN ISOLATION require 'users/create_user' RSpec.describe Users::CreateUser do subject(:create_user) do

    Users::CreateUser.new end describe '#call' do it 'returns created user' do user = create_user.call(id: 1, name: 'Jane') expect(user).to eql(id: 1, name: 'Jane') end end end 44
  18. # system/web.rb require_relative 'app' require 'roda' class Web < Roda

    opts[:api] = App plugin :json route do |r| r.post 'users' do api['users.create_user'].call(r[:user]) end end def api self.class.opts[:api] end end 48
  19. #!/usr/bin/env ruby require "bundler/setup" require "hanami/cli" require "json" require_relative '../system/boot'

    module Commands extend Hanami::CLI::Registry class CreateUser < Command desc "Creates a user" argument :user, desc: "User data" def call(user: nil, **) params = JSON.parse(user) output = App['users.create_user'].call(params) puts "Created #{output.inspect}" end end register "create_user", CreateUser end Hanami::CLI.new(Commands).call 51
  20. WEB INPUT AS PRE-PROCESSED RACK PARAMS r[:user] # { "id"

    => 1, "name" => "Jane" } CLI INPUT AS A PLAIN JSON STRING '{"id":1,"name":"Jane"}' 54
  21. 58

  22. CLEAN ARCHITECTURE > Ruby app comes first > Respecting boundaries

    > Object composition > User interface as an extension of your Ruby app 59
  23. MORE THINGS TO CHECK OUT > Boundaries talk by Gary

    Bernhardt > dry-system on GitHub > sample app from slides on GitHub 61