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

How we use service objects to make our apps clean, composable, maintainable, and testable.

How we use service objects to make our apps clean, composable, maintainable, and testable.

Wouldn’t it be great if you could keep the maintainability of your app high even as it grew in complexity? Wouldn’t it feel wonderful to know that all your app functionality is fully tested, and that those tests are fast? How amazing would it be to start finally treating your app like a set of lego bricks?

Using service objects can get you a very long way there. They’re not a silver bullet by any stretch of the imagination, but in my experience, they have been the number one thing alongside TDD that has really elevated my skill level.

Not all service object toolchains are created equal, and there are downsides, trade-offs, benefits, and useful shortcuts to be gained depending on the kind you reach for. I’ve battle tested a bunch of them, and this talk will include pointers for helping you decide what’s right for you, for when you start using these tools in your own software projects.

Join me as I walk through the learnings to be had from using this software development technique, and how I’ve matured my thinking on this topic to make me an extremely effective developer that ships robust working code.

Gabriel Fortuna

September 06, 2019
Tweet

More Decks by Gabriel Fortuna

Other Decks in Programming

Transcript

  1. More than you
    wanted to know
    about Service
    Objects
    Zero One × Rubyconf TH

    View Slide

  2. 4 Things About Me
    Coding In The Bad Old Days
    Meet Service Objects
    Service Object Nirvana
    What's In It For Me?
    Hey Kid, Wanna Build One?
    Tips & Tricks
    Questions & Closing
    I.
    II.
    III.
    IV.
    V.
    VI.
    VII.
    VIII.
    00
    Todays
    Agenda
    More than you wanted to know about Service objects
    Zero One × Rubyconf TH

    View Slide

  3. 01
    More than you wanted to know about Service objects
    4 Things About Me
    My Family My Dogs My Company
    Zero One × Rubyconf TH

    View Slide

  4. 01
    Too much
    Destiny
    More than you wanted to know about Service objects
    Hit me up on gee-forr on
    PSN. gee_forr on Steam.
    Let's raid.
    Zero One × Rubyconf TH

    View Slide

  5. A
    story about
    the bad old
    days
    More than you wanted to know about Service objects
    Zero One × Rubyconf TH

    View Slide

  6. V
    Logic in
    your
    views
    More than you wanted to know about Service objects
    Zero One × Rubyconf TH

    View Slide

  7. C
    What about
    controllers?
    More than you wanted to know about Service objects
    Zero One × Rubyconf TH

    View Slide

  8. M
    Models are
    the perfect
    place for
    my logic,
    right?
    More than you wanted to know about Service objects
    Zero One × Rubyconf TH

    View Slide

  9. Mixed concerns
    Single use context
    Hard to refactor
    Difficult to extend
    Painful to test
    More than you wanted to know about Service objects

    The
    downside
    Zero One × Rubyconf TH

    View Slide

  10. Developer, meet Service Object.
    Service Object, meet developer.
    More than you wanted to know about Service objects
    Zero One × Rubyconf TH

    View Slide

  11. A Service Object is a Plain old Ruby
    Object, that is meant to decompose
    business logic into manageable
    classes and methods.
    Rob Race,
    Hackernoon (http://bit.ly/serv-objs-1)


    Zero One × Rubyconf TH

    View Slide

  12. 03
    More than you wanted to know about Service objects
    What makes a Service Object great?
    7
    Preferably via a
    single public method
    like call()
    It Does One Thing
    8
    Simple Services can
    compose together to
    implement complex
    processes
    They Can Chain Together
    F
    Gives you more than
    a simple boolean
    return value
    Complex Return Objects
    Zero One × Rubyconf TH

    View Slide

  13. 03
    Let's get
    meta
    More than you wanted to know about Service objects
    Two types typically
    found in the wild:
    Actions & Composers
    Zero One × Rubyconf TH

    View Slide

  14. What changed
    when I started using
    Service Objects in my code?
    More than you wanted to know about Service objects
    Zero One × Rubyconf TH

    View Slide

  15. 04
    What
    Changed?
    More than you wanted to know about Service objects
    Give some input
    Test some output
    Stub friendly
    Tests became easier




    Change friendly architecture
    Limited blast radius for changes
    Logic can be swapped out easily
    Logic can be dropped easily
    Changes to reqs are simpler





    Zero One × Rubyconf TH

    View Slide

  16. 04
    What
    Changed?
    More than you wanted to know about Service objects
    Logic reuse
    Reuse across situations
    Reuse became a reality



    Objectively simpler code
    Most code fits in a screen
    More devs can contribute
    Less complexity → more quality
    Lower overall code complexity
    L




    Zero One × Rubyconf TH

    View Slide

  17. 04
    What
    Changed?
    More than you wanted to know about Service objects
    Clear codepath
    Linear logic flow
    Self-documenting sequencing
    Flattened structure with no or minimal nesting
    Explicit Flow
    C




    Zero One × Rubyconf TH

    View Slide

  18. 05
    More than you wanted to know about Service objects
    What can I
    expect if I use
    Service Objects?
    Zero One × Rubyconf TH

    View Slide

  19. 05
    What is
    this?
    More than you wanted to know about Service objects
    What does
    it do?

    View Slide

  20. 05
    How about
    now?
    More than you wanted to know about Service objects

    View Slide

  21. 05
    Great if
    you're
    DHH
    More than you wanted to know about Service objects
    Not so
    great
    for the
    rest of us.
    Zero One × Rubyconf TH

    View Slide

  22. 05
    Any clearer
    now?
    More than you wanted to know about Service objects

    View Slide

  23. 05
    Let's take a
    closer look
    More than you wanted to know about Service objects

    View Slide

  24. IssuesJWT
    IssuesCookie
    FetchExistingUserForOpenIDPayload
    CreateUserForOpenIDPayload
    More than you wanted to know about Service objects
    05
    Simpler
    feature
    refactoring
    LogUserIn
    Zero One × Rubyconf TH

    View Slide

  25. UpdateWhileYouWereGoneNotifications
    SendWelcomeBackEmail
    IssuesJWT
    FetchExistingUserForOpenIDPayload
    FetchExistingUserForOpenIDPayload
    More than you wanted to know about Service objects
    05
    Modular
    logic at the
    process layer
    LogUserIn
    Zero One × Rubyconf TH

    View Slide

  26. Services have easily
    stubbable or mockable
    inputs, and easily
    testable outputs
    Organisers take code on a
    journey, traversing through a
    'full stack'. They implement a
    process end to end.
    Treat like unit tests
    Services
    Treat like integration tests
    Organisers

    View Slide

  27. Web Controllers
    Async jobs
    Rake tasks
    05
    Use outside
    of just web
    requests
    More than you wanted to know about Service objects
    Your application is
    more than just a
    series of HTTP
    transactions
    Zero One × Rubyconf TH

    View Slide

  28. Controllers become super boring.
    0 resist the temptation to send
    params as primary argument
    Zero One × Rubyconf TH

    View Slide

  29. 06
    Let's build one
    More than you wanted to know about Service objects
    Zero One × Rubyconf TH

    View Slide

  30. Tip, Tricks, etc
    07
    More than you wanted to know about Service objects
    Zero One × Rubyconf TH

    View Slide

  31. 07
    Name after steps in business process.
    Avoid naming after implementation details.
    InviteFriend is better than CreateLinkedUser
    Actions
    P
    Naming
    More than you wanted to know about Service objects
    Name after business processes
    Organisers
    f
    Zero One × Rubyconf TH

    View Slide

  32. Not traditional. Publish/
    Subscribe Framework
    Wisper
    !
    Plenty of features.
    Responsive developer.
    Light-Service ^
    Can be very powerful.
    D.R.Y? more like D.I.Y, amirite?
    Dry-rb B
    Battle-tested. Simple. Core
    features supported. Has
    plugins. Recommended.
    Interactor
    V
    07
    More than you wanted to know about Service objects
    Picking a Service Object Framework
    01 02
    03 04
    Zero One × Rubyconf TH

    View Slide

  33. 07
    Programatically generate action sequences

    View Slide

  34. More than you wanted to know about Service objects
    Sidekiq Worker
    Service Object
    Hybrid Service Object × Async Worker

    View Slide

  35. 08
    Khop khun krab!
    More than you wanted to know about Service objects
    Gabriel Fortuna / [email protected] / @gee_forr
    Zero One / zero-one.io / @we_are_zero_one
    Kill some mediocrity with us - We're hiring.
    Seriously, let's play Destiny. PSN: gee-forr. Steam: gee_forr
    Zero One × Rubyconf TH

    View Slide

  36. Rubyfuza
    February 2020
    Cape Town
    South Africa
    rubyfuza.org

    View Slide