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

Going #frameworkless in the backend

Going #frameworkless in the backend

My presentation at Codemotion Milano 2018

Matteo Vaccari

November 29, 2018
Tweet

More Decks by Matteo Vaccari

Other Decks in Technology

Transcript

  1. THE ART OF #FRAMEWORKLESS
    IN THE BACKEND
    Matteo Vaccari
    @xpmatteo

    View full-size slide

  2. You have a choice, in your growth as a developer

    View full-size slide

  3. You may look for a simplified, maybe
    more pleasant way to deal with reality

    View full-size slide

  4. And look for a framework that will
    provide that simplified worldview for
    you. What is your growth path then?

    View full-size slide

  5. You can become a developer that knows his
    framework(s) very well. Your growth is in
    learning more frameworks and learn them
    better. You will have plenty to learn! And I don’t
    say this is necessarily bad. However…

    View full-size slide

  6. There is another way: you may want to deal with
    reality as it is. To go closer to the metal.

    View full-size slide

  7. To do this, you will need knowledge of a more
    fundamental nature.
    There is a world of things to learn!
    This kind of knowledge is more enduring, more
    versatile, more powerful.

    View full-size slide

  8. … and you may gain superpowers :-)

    View full-size slide

  9. Frameworks?
    Risk
    Functionality
    Performance
    Obsolescence
    Incompatibilities
    Developer
    Experience
    App startup time
    Test running time
    The bad
    The good
    Learning
    Standardisation
    Bad habits
    DB-Centric
    Quick project startup
    Extra work
    Upgrades
    Searching on
    Stackoverflow
    for answers
    Addiction
    Anything is good or bad in a
    particular context, for a
    particular set of goals.

    View full-size slide

  10. I prepared a small demo of
    an API that plays the game
    of hangman. I wrote three
    versions of it; the first uses
    Spring and JPA. The second
    replaces JPA with plain
    JDBC calls. The third one
    removes Spring and uses
    the Servlet API.

    View full-size slide

  11. Creating a new game
    All three versions respond in
    the same way. This is a
    sample of how to play.

    View full-size slide

  12. First guess: the letter “e”

    View full-size slide

  13. Second guess: the letter “t”

    View full-size slide

  14. A small demo of how it feels
    to start a Spring/JPA service
    versus a plain Java one.

    View full-size slide

  15. Spring/JPA
    (Spring
    MVC)
    Controller
    Embedded
    Tomcat
    Spring
    MVC
    Servlet
    (Spring)
    Application
    Service
    (JPA)
    Repository
    Emtity
    Manager DB
    Our router
    Embedded
    Jetty
    Our Servlet
    Our
    Application
    Service
    Our
    Repository
    JDBC DB
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects

    View full-size slide

  16. Spring/JPA
    No Spring/No JPA
    (Spring
    MVC)
    Controller
    Embedded
    Tomcat
    Spring
    MVC
    Servlet
    (Spring)
    Application
    Service
    (JPA)
    Repository
    Emtity
    Manager DB
    Our router
    Embedded
    Jetty
    Our Servlet
    Our
    Application
    Service
    Our
    Repository
    JDBC DB
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects
    Domain
    Objects

    View full-size slide

  17. HttpServletRequest
    HttpServletResponse
    A Java web application is
    (not exactly, but similar to) a
    function that transforms an
    HttpServletRequest into an
    HttpServletResponse.

    View full-size slide

  18. HttpServletRequest
    HttpServletResponse
    WebRequest
    WebResponse
    However, those servlet API objects have 50+ methods and are
    very low level and hard to use. One key thing to make
    #frameworkless work well is to write simple code. Therefore,
    it’s good to transform the servlet API objects into something
    simpler and easier to use. These are custom made for each
    project, similar but never exactly the same thing.

    View full-size slide

  19. HttpServletRequest
    HttpServletResponse
    WebRequest
    WebResponse
    GameResponse
    x
    HttpStatus
    GuessRequest
    And this is still not enough; for any particular request, we
    probably want to transform the WebRequest into a specific
    request object that contains exactly what we need.

    View full-size slide

  20. The “Main Partition”

    View full-size slide

  21. The “Main Partition”
    The main and the servlet together are
    what Uncle Bob calls the “main partition”,
    that is, the place where objects are
    constructed.
    The main (of course) is executed once
    per process, while the servlet service
    method is executed once per request. It
    is a sort of “main” for the request.

    View full-size slide

  22. The Router The router’s job is to decide what is the
    user trying to do, and dispatch to an
    appropriate service call. The code of the
    router is made much simpler by having
    some logic in the WebRequest and
    WebResponse.

    View full-size slide

  23. The Router, part II

    View full-size slide

  24. The Router, part IIFor a simple service, a chain of IFs is
    simple and effective. If the service grows, it
    will probably evolve towards a table lookup.
    Again, this object should be developed
    afresh for every project/service. There’s
    no need to create a reusable “custom
    framework” here.

    View full-size slide

  25. The Application Service

    View full-size slide

  26. The Application Service
    The application service job is
    (a) to invoke business logic on domain
    objects, and
    (b) to deal with non-functional effects such
    as persistence.

    View full-size slide

  27. The Domain Objects

    View full-size slide

  28. The Game Repository

    View full-size slide

  29. The Game Repository
    The GuessRequest contains data that are
    not used by the business logic, but are
    relevant for audit/security/debug purposes.
    This information is saved in a way that
    does not pollute the business logic
    (compare the GuessRequest, shown later,
    with the Guess domain object)
    The QueryRunner is from Apache Dblib. It’s
    possible to write your own; it’s no more than
    10 lines of code.

    View full-size slide

  30. The Game Repository, part II

    View full-size slide

  31. The Game Repository, part II
    Reconstructing a Game involves replaying
    all the “guesses” of the player. This not
    exactly event sourcing, but it’s similar.
    I find that saving all the “events” involving
    an aggregate, with as little processing as
    possible at write time, makes persistence
    simpler, even when you don’t go full event-
    sourcing.
    Perhaps this way can be even better than
    full event-sourcing :-)

    View full-size slide

  32. The Web Request

    View full-size slide

  33. The Web Request
    The API of the WebRequest is at the same
    time simpler and higher level than the
    servlet API. The getOptionalParameter()
    and getMandatoryParameter() methods
    simplify the router and the service.

    View full-size slide

  34. The Guess Request

    View full-size slide

  35. Spring/JPA Spring/— —/—
    Server startup
    time
    ~6s ~3s 0.4s
    Time to run all
    tests
    10s 5.5s 3.5s
    Time to run
    slowest test
    7s 4-5s 1s
    # of tests 25 24 48
    SLOC prod 360 424 643
    SLOC test 358 357 745
    jar size 30MB 17MB 6.3MB
    Judge for yourself.

    View full-size slide

  36. Frameworks?
    Risk
    Functionality
    Performance
    Obsolescence
    Incompatibilities
    Developer
    Experience
    App startup time
    Test running time
    The bad
    The good
    Learning
    Standardisation
    Bad habits
    DB-Centric
    Quick project startup
    Extra work
    Upgrades
    Searching on
    Stackoverflow
    for answers
    Addiction

    View full-size slide

  37. References
    https://github.com/xpmatteo/frameworkless-hangman
    https://github.com/frameworkless-movement/manifesto
    45

    View full-size slide

  38. twitter.com/xpmatteo
    thoughtworks.com
    THANK YOU
    WE ARE HIRING!
    YES, IN ITALY!

    View full-size slide