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

More than you wanted to know about Service Objects

More than you wanted to know about Service Objects

It’s high time junior developers reap the benefits of Service Objects that intermediate and senior developers have been enjoying for ages. This talk will reveal the techniques we use to make our apps simpler, more testable, and infinitely easier to refactor.

Gabriel Fortuna

February 07, 2019
Tweet

More Decks by Gabriel Fortuna

Other Decks in Programming

Transcript

  1. More than you
    wanted to know
    about Service
    Objects
    Zero One × RUBYFUZA & FRIENDS

    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 × RUBYFUZA & FRIENDS

    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 × RUBYFUZA & FRIENDS

    View Slide

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

    View Slide

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

    View Slide

  6. V
    Logic in
    your
    views
    More than you wanted to know about Service objects
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  7. C
    What about
    controllers?
    More than you wanted to know about Service objects
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  8. M
    Models are
    the perfect
    place for
    my logic
    More than you wanted to know about Service objects
    Zero One × RUBYFUZA & FRIENDS

    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 × RUBYFUZA & FRIENDS

    View Slide

  10. Developer, meet Service Object.
    Service Object, meet developer.
    More than you wanted to know about Service objects
    Zero One × RUBYFUZA & FRIENDS

    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 × RUBYFUZA & FRIENDS

    View Slide

  12. 03
    More than you wanted to know about Service objects
    What makes a Service Object great?
    7
    Preferably via single
    public method
    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 × RUBYFUZA & FRIENDS

    View Slide

  13. organisers
    03
    Let's get
    meta
    More than you wanted to know about Service objects
    Two types typically
    found in the wild:
    actions &
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  14. What changed for me
    when I started using
    Service Objects in my code?
    More than you wanted to know about Service objects
    Zero One × RUBYFUZA & FRIENDS

    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 × RUBYFUZA & FRIENDS

    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 × RUBYFUZA & FRIENDS

    View Slide

  17. 05
    What can I
    expect if I use
    Service Objects?
    Zero One × RUBYFUZA & FRIENDS
    More than you wanted to know about Service objects

    View Slide

  18. 05
    What is
    this?
    More than you wanted to know about Service objects
    What does
    it do?
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  19. 05
    How about
    now?
    More than you wanted to know about Service objects
    Zero One × RUBYFUZA & FRIENDS
    What does
    it do?

    View Slide

  20. 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 × RUBYFUZA & FRIENDS

    View Slide

  21. 05
    Any clearer
    now?
    More than you wanted to know about Service objects
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  22. 05
    Let's take a
    closer look
    More than you wanted to know about Service objects
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  23. IssuesJWT
    IssuesCookie
    FetchExistingUserForOpenIDPayload
    CreateUserForOpenIDPayload
    More than you wanted to know about Service objects
    05
    Simpler
    feature
    refactoring
    LogUserIn
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  24. UpdateWhileYouWereGoneNotifications
    SendWelcomeBackEmail
    IssuesJWT
    FetchExistingUserForOpenIDPayload
    CreateUserForOpenIDPayload
    More than you wanted to know about Service objects
    05
    Modular
    logic at the
    process layer
    LogUserIn
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  25. Actions have easily
    stubbable or mockable
    inputs, and easily
    testable outputs
    Organisers take code on a
    journey, traversing through the
    whole stack. They implement a
    process end to end.
    Treat like unit tests
    Actions
    Treat like integration tests
    Organisers
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  26. 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 × RUBYFUZA & FRIENDS

    View Slide

  27. Controllers become super boring.
    0 resist the temptation to send
    params as primary argument
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  28. 06
    Let's build one
    More than you wanted to know about Service objects
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  29. Tip, Tricks, etc
    07
    More than you wanted to know about Service objects
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  30. 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 × RUBYFUZA & FRIENDS

    View Slide

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

    View Slide

  32. 07
    More than you wanted to know about Service objects
    Programatically generate action sequences
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  33. More than you wanted to know about Service objects
    Sidekiq Workers
    Service Objects W
    Hybrid Service Object × Async Worker
    Zero One × RUBYFUZA & FRIENDS

    View Slide

  34. 08
    Thank you!
    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.
    Zero One × RUBYFUZA & FRIENDS
    Seriously, let's play some Destiny, PSN: gee-forr

    View Slide