Share
Embed
From CRUD to Hexagonal & DDD, how Meetic saved its codebase
Frameworks give us a CRUD mindset, not suited to handle business complexity.
Discover patterns you can re-use on your project, that really helped us, and tips to spread a new culture amongst your colleagues.
Hosted by Polymorphic meetup group : https://www.meetup.com/fr-FR/Polymorphic/events/260342370/
Jean-Marie Lamodière
May 09, 2019
Transcript
58 slides
-
From CRUD to Hexagonal & DDD “How Meetic saved its
codebase” Icons made by Icongeek26, Freepik from www.flaticon.com Jean-Marie Lamodière - Backend tech lead @jmlamodiere -
stackshare.io/meetic @meetictech microservice calls Kafka messages production deployments 3 500
000 000 250 000 000 25 / day -
But… Monolithic Rest API = new Legacy v2 Icons made
by Freepik from www.flaticon.com -
What’s wrong ? We use a modern web framework !
We follow its best practices ! Icons made by Freepik from www.flaticon.com -
WHY ? MVC Framework = CRUD Create Read Update Delete
= Database mindset Icons made by Smashicon from www.flaticon.com -
WHY ? MVC Framework = CRUD Typical tutorial : 1.
Generate entities + getters + setters 2. Generate Database 3. Expose as REST (or web <form>) -
WHY ? MVC Framework = CRUD N-tiers architecture Presentation Business
logic Data access Framework focus Framework focus ...YOLO ! Icons made by Freepik, Smashicon from www.flaticon.com -
WHY ? Tests… with code coverage in mind Complex Class
Icons made by Pause08, Freepik from www.flaticon.com = No refactoring Test coupled with internal details + 100% -
What is a useful test ? How to code business
complexity ? Icons made by Freepik from www.flaticon.com -
WHY ? Let’s try buzzwords ! Test Driven Development Behaviour
Driven Development Command Bus Hexagonal architecture Domain Driven Design Command Query Separation Icons made by Roundicons from www.flaticon.com Microservices -
WHY ? 3 years later... • Inconsistant code designs •
Differing opinions amongst developers Fewer libraries! More libraries! Get sh*t done! Refactor everything! Fewer libraries! More libraries! Refactor everything! -
WHY ? Evangelism Pro-tips • Lead by example Pull Requests,
Pair Programming • Organize debates Teach, show code, vote • Code example as documentation • Use (anti-)pattern names -
Hexagonal Architecture = Ports & Adapters Dr. Alistair Cockburn -
2005 Hexagonal Architecture Command / Query Separation Domain Driven Design Testing Strategy Gotchas Pro-Tips -
WHY ?What is “Decoupling” ? Low coupling High cohesion “Allow
unrelated things to change independently” Icons made by Roundicons from www.flaticon.com -
WHY ?Hexagonal architecture Low coupling High cohesion “Decoupling business and
technical choices” Icons made by Roundicons from www.flaticon.com -
WHY ?Hexagonal architecture “Techno agnostic domain” Infrastructure • Framework (+
version) • Libraries (ORM, http client) • Database • REST, SOAP, Command line ? Domain (Business logic) -
WHY ? Where to put my code ? Infrastructure Framework,
libraries, techno Domain (Business logic) Icons made by Icongeek26 from www.flaticon.com Product Owner Business expert Architect Dev, Ops (Who decides ?) -
WHY ?Domain inside hexagon RepositoryInterface Entity No framework Icons made
by Freepik from www.flaticon.com /Domain MemberRepository Member Screaming architecture -
WHY ? Port = simplest interface meeting use-case needs UseCase
RepositoryInterface Entity Start inside hexagone, free your mind from infrastructure No framework Icons made by Freepik from www.flaticon.com -
WHY ? Port = simplest interface meeting use-case needs Icons
made by Roundicons, Freepik from www.flaticon.com repo.persist(member).flush (framework details) repo.add(member) repo.get(id) A repository can be seen as a list -
WHY ? Port = simplest interface meeting use-case needs Icons
made by Roundicons, Freepik from www.flaticon.com Intention revealing interface getInactiveMembers() getDefaultChoices(gender) increaseCounter() -
WHY ? Adapter = techno-specific “driver” UseCase RepositoryInterface Entity MysqlRepository
HttpRepository No framework Framework welcome Icons made by Freepik from www.flaticon.com TestRepository -
WHY ? Adapter = techno-specific “driver” MysqlRepository Icons made by
Freepik from www.flaticon.com /Domain MemberRepository Member /Infrastructure MysqlMemberRepo... Framework welcome -
WHY ? MysqlMemberRepository implements MemberRepositoryInterface { getInactiveMembers() {...} } Adapter
= techno-specific “driver” Techno name Icons made by Freepik from www.flaticon.com -
WHY ? Adapter = techno-specific “driver” UseCase RepositoryInterface Entity MysqlRepository
@Throws UserNotFound Icons made by Freepik from www.flaticon.com Convert infra exceptions to domain exceptions -
WHY ? Adapter = techno-specific “driver” UseCase RepositoryInterface Entity AggregatorRepository
MysqlDao* CacheDecoratorDao HttpDao *Data Access Object (No impact here) Icons made by Freepik from www.flaticon.com (Complex infra) -
WHY ? Difference with n-tiers architecture Service MysqlDao1* Dto1* Dao2Interface
*Data Access Object *Data Transfert Object CacheDecoratorDao2 HttpDao2 Dto2 (Aware of 2 Dao) (Mimic database structure) -
WHY ? Port & Adapters : entrypoints UseCase RepositoryInterface Entity
RestController Test CliController No framework Framework welcome Icons made by Freepik from www.flaticon.com -
RestController Framework welcome Icons made by Freepik from www.flaticon.com /Infrastructure
/Symfony MemberController MemberSerializer WHY ? Port & Adapters : entrypoints -
WHY ? Ex. of adapter refacto @Meetic Symfony 2 Database
Database Oracle Icons made by Freepik from www.flaticon.com Symfony 4 External REST API Config files PostgreSQL -
WHY ?Layers inside hexagone UseCase RepositoryInterface Entity Icons made by
Freepik from www.flaticon.com Application Use cases (do, get…) Domain Modeling business reality (Use case agnostic) -
Command Query Separation Command (do) : produces an effect Query
(get) : gets data, no state change Same as “Query” vs. “Mutation” in GraphQL Hexagonal Architecture Command / Query Separation Domain Driven Design Testing Strategy Gotchas Pro-Tips -
WHY ? Command / Query Separation RepositoryInterface Entity Icons made
by Freepik, Smashicons from www.flaticon.com Query Handler Command Handler -
WHY ? Command / Query Separation /Application /Command RegisterMember RegisterMemberHandler
/Query GetNewMembers GetNewMembersHandler /Domain MemberRepository Member -
WHY ? Command / Query Separation Command, Query • simple
DTO • user intention • primitive values /Application /Command RegisterMember RegisterMemberHandler /Query GetNewMembers GetNewMembersHandler -
WHY ? Steps 1. Infrastructure\Controller Converts HTTP request to Command/Query
and call Handler 2. Application\CommandHandler Apply logic, act on Entity through Repository 3. Infrastructure\Controller Send HTTP response -
WHY ? Command / Query Separation Command Handler RepositoryInterface Entity
Icons made by Freepik, Smashicons from www.flaticon.com Query Handler DaoInterface DTO (with validation) (no logic, readonly) Same database (≠ CQRS) -
WHY ? Command / Query Separation /Application /Command RegisterMember RegisterMemberHandler
/Query GetContactPictures GetContactPicturesHandler /Domain MemberRepository Member /Read ContactDao ContactPicture -
WHY ? Command / Query Separation Create custom Read DAO
only when it simplifies code Ex : custom DTO with less fields -
Domain Driven Design Eric Evans - 2004 Icons made by
Icongeek26, Freepik from www.flaticon.com Hexagonal Architecture Command / Query Separation Domain Driven Design Testing Strategy Gotchas Pro-Tips -
WHY ? Domain Driven Design • Talk with domain experts
• Use domain words in your code • Focus on events instead of data Icons made by Icongeek26 from www.flaticon.com -
WHY ? Before DDD : fake entities... Anemic domain model
Temporal coupling memberEntity .setStatus(“active”) .setActivationDate(now) .setCanTalk(true) Icons made by Roundicons from www.flaticon.com Feature envy -
WHY ? Same (anti)patterns with REST PUT /member/123 {“status”: “active”,
“activDate”:”now”“canTalk”:true} Icons made by Roundicons, Freepik from www.flaticon.com POST /member/123 {“action”: “activate”} Logic on caller = feature envy POST /member/123/activate -
WHY ? DDD Entity Rich domain model Icons made by
Roundicons from www.flaticon.com memberEntity .activate() • State always valid (check invariants) • Explicit mutators • Emit domain events -
WHY ? Before DDD : primitive types only... Icons made
by Roundicons from www.flaticon.com Primitive obsession productEntity .setPriceAmount(20.50) .setCurrenty(“EUR”) (same concern) -
WHY ? DDD Value Object price = new Price(20.50, “EUR”)
productEntity .setPrice(price) • Explicit • Immutable • Valid -
WHY ? DDD Value Object @ Meetic age = new
Age(23) … = age .toSearchBirthdayMax() • Factorize validation • Factorize features Must be >= 18 -
Testing strategy Hexagonal Architecture Command / Query Separation Domain Driven
Design Testing Strategy Gotchas Pro-Tips Icons made by Freepik from www.flaticon.com -
WHY ? Inside hexagon: unit tests Command Handler RepositoryInterface Entity
Icons made by Freepik, Smashicons from www.flaticon.com • Mock Infra only • Don’t mock entities / value objects Test Mock -
WHY ? Outside hexagon: integration tests Icons made by Freepik,
Smashicons from www.flaticon.com Don’t mock a library API The test should allow refactoring : • QueryBuilder -> SQL • Change Http library MysqlRepository Docker mysql HttpRepository Wiremock -
Gotchas ? Pro-Tips ? Hexagonal Architecture Command / Query Separation
Domain Driven Design Testing Strategy Gotchas Pro-Tips Icons made by Freepik from www.flaticon.com -
WHY ? Do not stack new layers Lava flow Refacto
“vertical” business slices Icons made by Freepik, Pixel Perfect, Roundicons from www.flaticon.com New layer Old layer 1 Old layer 2 -
WHY ? Gotchas, Pro-tips • Hexagon overkill when no domain
logic. CRUD / framework magic more suited. • Start inside hexagon, not with database. Otherwise, infra complexity will leak into domain • To choose layer => whose decision is it ? Product Owner Business expert Architect Dev, Ops @jmlamodiere Icons made by Icongeek26 from www.flaticon.com Thanks for coming !