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. 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
  2. @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
  3. @yot88 Alistair Cockburn Jeffrey Palermo What characteristics do these architectures

    have in common? Onion architecture hexagonal architecture
  4. @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(); } }
  5. @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
  6. @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
  7. @yot88 Alistair Cockburn Jeffrey Palermo What characteristics do these architectures

    have in common? Onion architecture hexagonal architecture
  8. @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?
  9. @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.
  10. @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
  11. @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
  12. @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.
  13. @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
  14. @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
  15. @yot88 What benefits do you see? Pros and cons ?

    La “Clean architecture” c’est la vie ?
  16. @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
  17. @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
  18. @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