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

Clean Architecture - hands on

Yoan
November 21, 2018

Clean Architecture - hands on

Yoan

November 21, 2018
Tweet

More Decks by Yoan

Other Decks in Education

Transcript

  1. @yot88
    Clean architecture

    View Slide

  2. Who am I ?
    Technical Agile coach, Software craftsman
    I’m Yoan THIRION (freelance)
    • design software since more than 15 years
    • fundamental to succeed in that area : agility and technical excellence
    • help teams deliver well crafted software
    • implementation of agile and technical practices (eXtreme programming, Refactoring, DDD, Mob programming, …)
    Let’s connect
    My services
    https://www.yoan-thirion.com/
    Technical agile coaching Communities of practice Cultural change
    devops Brown bags Serious games

    View Slide

  3. @yot88
    Learning Objectives
    • Discover the fundamentals of Clean Architecture
    • how to design for maintainability, extensibility and reusability
    • Show you a method to set your project on a path with higher success chances

    View Slide

  4. @yot88
    Clean architecture
    Robert C. Martin a.k.a Uncle bob
    2017

    View Slide

  5. @yot88
    Alistair Cockburn Jeffrey Palermo
    What characteristics do these architectures have in common?
    Onion architecture
    hexagonal architecture

    View Slide

  6. @yot88
    Hexagonal Architecture

    View Slide

  7. @yot88
    Source : https://blog.octo.com/architecture-hexagonale-trois-principes-et-un-exemple-dimplementation/
    Hexagonal Architecture

    View Slide

  8. @yot88
    Hexagonal Architecture
    Source : https://blog.octo.com/architecture-hexagonale-trois-principes-et-un-exemple-dimplementation/
    Business + rules

    View Slide

  9. @yot88
    Hexagonal Architecture
    Completely isolatethe core of your system (your business logic) and test automatically its behavior
    independently of other concerns (Database, Services, …)
    class Program {
    static void Main(string[] args) {
    // 1. Instantiate right-side adapter(s) ("I want to go outside the hexagon")
    IObtainPoems fileAdapter = new PoetryLibraryFileAdapter(@".\Rimbaud.txt");
    // 2. Instantiate the hexagon
    IRequestVerses poetryReader = new PoetryReader(fileAdapter);
    // 3. Instantiate the left-side adapter(s) ("I want ask/to go inside the hexagon")
    var consoleAdapter = new ConsoleAdapter(poetryReader);
    System.Console.WriteLine("Here is some...");
    consoleAdapter.Ask();
    System.Console.WriteLine("Type enter to exit...");
    System.Console.ReadLine();
    }
    }

    View Slide

  10. @yot88
    Onion Architecture

    View Slide

  11. @yot88
    Onion Architecture
    Source : https://dzone.com/articles/onion-architecture-is-interesting
    Entities and classes closely related to them
    Domain-defined processes
    Application-specific logic / use cases
    Outer layer
    Peripheral concerns

    View Slide

  12. @yot88
    Onion Architecture
    Source : https://dzone.com/articles/onion-architecture-is-interesting
    Between the layers of the Onion, there is a strong dependency rule
    outer layers can depend on lower layers, but no code in the lower
    layer can depend directly on any code in the outer layer.
    Dependency Inversion Principle for the
    architecture

    View Slide

  13. @yot88
    Onion Architecture

    View Slide

  14. @yot88
    Alistair Cockburn Jeffrey Palermo
    What characteristics do these architectures have in common?
    Onion architecture
    hexagonal architecture

    View Slide

  15. @yot88
    Independent of frameworks
    Does not depend on the existence of libraries
    Allow us to use frameworks as tools (not a constraint)
    Independent of the front-end
    Can easily change the UI (from web to console)
    Independent of the database
    Business rules not bound to Database logic
    Independent of any external agency
    Business rules don’t know anything about outside world
    What characteristics do these architectures have in common?

    View Slide

  16. @yot88
    Business rules can be tested without external components
    Testable

    View Slide

  17. @yot88
    Use Cases

    View Slide

  18. @yot88
    Use case driven approach
    Ivar Jacobson

    View Slide

  19. @yot88

    View Slide

  20. @yot88
    Entities (Enterprise business rules)
    • Encapsulate Enterprise wide business rules
    • Can be
    o Object with methods
    o Set of data structures and functions
    • Could be used by many different applications in the enterprise.

    View Slide

  21. @yot88
    Use cases (Application business rules)
    • Capture business rules
    • Structure should indicate what the application is, not how it does it
    • Application specific business rules

    View Slide

  22. @yot88
    Interface adapters
    • Set of adapters
    o Convert data
    § from the format most convenient for the use cases and entities,
    § to the format most convenient for some external agency such as the Database or the Web
    • In a MVC architecture :
    o Presenters, Views, and Controllers

    View Slide

  23. @yot88
    Frameworks & drivers
    • Frameworks and tools such as
    o Database
    o Web Framework
    Glue code that communicates to the next circle inwards.
    • This layer is where all the details go
    o Keep these things on the outside where they can do little harm.

    View Slide

  24. @yot88
    The dependency rule
    Source code dependencies can only point inwards.

    View Slide

  25. @yot88
    The dependency rule

    View Slide

  26. @yot88
    Clean architecture kata
    Objective
    • Learn and practice the concepts behind clean architecture
    The features of the application are basic crud :
    • Create a user
    • Find a user
    • List all users
    • Login a user with their password

    View Slide

  27. @yot88
    Clean architecture kata
    How to
    • Clone the repository at : https://github.com/ythirion/clean-architecture-example
    • Discover the code
    • Draw the sequence diagram of the create User

    View Slide

  28. @yot88
    Clean architecture kata

    View Slide

  29. @yot88
    What benefits do you see?
    Pros and cons ?
    La “Clean architecture” c’est la vie ?

    View Slide

  30. @yot88
    Pros and cons
    • Learning curve – I don’t know why, but people tend
    to mess up splitting responsibilities between layers,
    especially harming the domain model
    • Indirection – interfaces everywhere!
    • Potentially heavy
    • Plays well with DDD – architecture that builds everything on top
    of a domain model
    • Directed coupling – the most important code in our application
    depends on nothing, everything depends on it
    • Flexibility – from an inner-layer perspective, you can swap out
    anything in any of the outer layers and things should work just
    fine
    • Testability – since your application core does not depend on
    anything else, it can be easily and quickly tested in isolation

    View Slide

  31. @yot88
    Archunit – Unit test your java architecture
    https://sequencediagram.org/
    To enforce your architecture
    https://www.archunit.org/

    View Slide

  32. @yot88
    What about Fp ?

    View Slide

  33. @yot88
    In oo world
    Scott Wlaschin
    F# guru

    View Slide

  34. @yot88
    Functional core, imperative shell
    functional core
    • pure functions : in / out
    • easy to test without any mocks
    imperative shell or reactive
    • service logic
    • integration tests

    View Slide

  35. @yot88
    In fp world
    https://increment.com/software-architecture/primer-on-functional-architecture/
    Scott Wlaschin
    F# guru

    View Slide

  36. @yot88
    In fp world
    Property-based testing
    Scott Wlaschin
    F# guru

    View Slide

  37. @yot88
    Pure domain
    Combined with immutable data structures this means you can be sure
    the same inputs will give the same outputs.
    Pure functions don’t refer to any global state.
    The same inputs will always get the same output.
    let Double i = i * 2

    View Slide

  38. @yot88
    dependencies

    View Slide

  39. @yot88
    conclusion
    Liked Learned
    Lacked Longed for

    View Slide

  40. @yot88
    To go further
    Scott Wlaschin
    F# guru
    https://fsharpforfunandprofit.com/

    View Slide

  41. @yot88
    To go further
    https://yoan-thirion.gitbook.io/knowledge-base/

    View Slide