ARAF KARSH HAMID Co-Founder / CTO Metamagic Global Inc., NJ, USA Domain @Arafkarsh Arafkarsh Driven Design Agenda
Total Page:16
File Type:pdf, Size:1020Kb
ARAF KARSH HAMID Co-Founder / CTO MetaMagic Global Inc., NJ, USA Domain @arafkarsh arafkarsh Driven Design Agenda 1 Hexagonal Architecture 3 Case Study • Hexagonal Architecture • Shopping Portal App • Shopping Portal Example • RESTful Guidelines 2 Domain Driven Design • Ubiquitous Language • Bounded Context • Context Maps • Aggregate Roots • Entities and Value Objects • Factories • Repositories • Domain Events 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 2 1 A Adapters OrderService Hexagonal Architecture Interface Ports & Adapters Services OrderService for UI REST Service Implementation The layer between the Adapter and Use Case Boundary Bounded Context the Domain is identified as the Ports OrderProcessing Web Interface A A layer. The Domain is inside the port, Services adapters for external entities are on p Order Data OrderProcessing p Validation the outside of the port. Domain Service (Business Rules) Implementation Domain Layer The notion of a “port” invokes the A OS idea that any device that adheres Domain to a known protocol can be plugged Models into a port. Similarly many adapters External A Apps A may use the Ports. Others p Ports Order Tracking • Reduces Technical Debt Repository Data Interface • Dependency Injection A A • Auto Wiring Store Order Tracking Source : http://alistair.cockburn.us/Hexagonal+architecture JPA Repository https://skillsmatter.com/skillscasts/5744-decoupling-from-asp-net-hexagonal-architectures-in-net File Implementation system Database 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 3 1 Shopping Portal Modules – Code Packaging Auth Customer Products Cart Order Packaging Structure Domain Layer Domain Layer Bounded Context Domain Layer • Models • Models Domain Models • Models • Repo • Repo (Entities, Value Objects, DTOs) • Repo • Services • Services Interfaces (Ports) • Services • Factories • Factories (Repositories, Business Services, Web Services) • Factories Entity Factories Adapters Adapters Adapters • Repo • Repo Implementation • Repo • Services • Services (Repositories, Business Services, Web Services) • Services • Web Services • Web Services • Web Services 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 4 1 Shopping Portal Design based on Hexagonal Architecture 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 5 1 RESTful Guidelines 1. Endpoints as nouns, NOT verbs 3. Documenting Ex. /catalogues 4. Paging /orders 5. Use SSL /catalogues/products and NOT 6. HTTP Methods GET / POST / PUT / DELETE / OPTIONS / HEAD /getProducts/ 7. HTTP Status Codes (Effective usage) /updateProducts/ 8. Versioning 2. Use plurals Use Media Type Version GET /account/5555 HTTP/1.1 Ex. /catalogues/{catalogueId} Accept: application/vnd.catalogues.v1+json and NOT Instead of URL path version /catalogue/{catalogueId} https://domain/v1/catalogues/products 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 6 1 RESTful Guidelines – Query Examples Search Products By Catalogue ID Search All Search Products By Products Catalogue ID & Product ID 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 7 1 RESTful Guidelines – Query Examples Two different implementation of same query 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 8 1 RESTful Guidelines – Get & Create Example URL Remains the same. HTTP Methods Get / Post Defines the action 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 9 1 RESTful Guidelines – Update & Delete Example 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 10 Domain Driven Design • Strategic Design • Tactical Design 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 11 2 Bounded Context – Strategic Design • Bounded Context is a Specific Business Process / Concern. • Components / Modules inside the Bounded Context are context specific. • Multiple Bounded Contexts are linked using Context Mapping. • One Team assigned to a Bounded Context. • Each Bounded Context will have it’s own Source Code Repository. • When the Bounded Context is being developed as a key strategic initiative of your organization, it’s called the Core Domain. • Within a Bounded Context the team must have same language called Ubiquitous language for Spoken and for Design / Code Implementation. 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 12 2 DDD: Bounded Context – Strategic Design Semi Cooked An App User’s Journey can run across Recipes Frozen multiple Bounded Context / Micro Chef Steward Raw Services. uses Materials Tables Dinning User Journey X Order Appetizer Veg Main Course Reservation uses Veg Menu Appetizer Non Bounded Bounded Bounded Veg Context Context Context Main Course Soft Drinks Hot Drinks Desserts Non Veg Dinning Semi Cooked Kitchen Recipes Frozen Context Areas of the domain treated Context Chef independently Steward Raw uses Materials Tables Dinning Order Appetizer Veg Main Course Discovered as you assess Reservation uses Veg requirements and build language Menu Appetizer Non Veg Main Course Soft Drinks Hot Drinks Desserts Non Veg Source: Domain-Driven Design App (DDD) of a Restaurant Bounded Context Understanding 8/10/2018 Reference by Eric Evans Menu Context 13 2 DDD: Ubiquitous Language: Strategic Design Ubiquitous Vocabulary shared by Used in all forms of spoken / Language all involved parties written communication Restaurant Context – Food Item : Role-Feature-Reason Matrix Analyst Developers Eg. Food Item (Navrathnakurma) As an Restaurant Owner Domain I want to know who my Customers are can have different meaning or Expert properties depends on the So that I can serve them better context. BDD Construct • In the Menu Context it’s a Ubiquitous QA Veg Dish. Language Given Customer John Doe exists • In the Kitchen Context it’s is recipe. When Customer orders food • And in the Dining Context it will have more info Assign customer preferences Design Test Cases Then related to user feed back Docs as Veg or Non Veg customer etc. Code BDD – Behavior Driven Development 10 August 2018 14 2 DDD : Context Map – Strategic Design Context Map defines the relationship August 2018 10 of Bounded Contexts Source: Domain-Driven Design Reference by Eric Evans (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 15 2 Domain Driven Design – Tactical Design 8/10/2018 Source: Domain-Driven Design Reference by Eric Evans (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 16 2 125 Understanding Aggregate Root Domain Driven • An aggregate will have one of its component Design objects be the aggregate root. Any references Aggregate Root Customer from outside the aggregate should only go to the aggregate root. The root can thus ensure the integrity of the aggregate as a whole. Order * Line Item Line Item • Aggregates are the basic element of transfer of Line Item data storage - you request to load or save whole aggregates. Transactions should not cross Shipping aggregate boundaries. Address Payment • Aggregates are sometimes confused with Strategy collection classes (lists, maps, etc.). Bank Transfer • Aggregates are domain concepts (order, clinic visit, playlist), while collections are generic. An aggregate will often contain multiple collections, Cash together with simple fields. Credit Card Source: Martin Fowler : Aggregate Root (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 17 2 Designing and Fine Tuning Aggregate Root Aggregate Root - #1 Aggregate Root - #2 10 August August 2018 10 Super Dense Single Aggregate Root Super Dense Aggregate Root is split into 4 Results in Transaction concurrency issues. different smaller Aggregate Root in the 2nd Iteration. Working on different design models helps the developers to come up with best possible design. Source : Effective Aggregate Design Part 1/2/3 : Vaughn Vernon http://dddcommunity.org/wp-content/uploads/files/pdf_articles/Vernon_2011_1.pdf (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 18 2 Rules for Building Aggregate Roots 1. Protect True Invariants in Consistency Boundaries. This rule has the added implication that you should modify just one Aggregate instance in a single transaction. In other words, when you are designing an Aggregate composition, plan on that representing a transaction boundary. 2. Design Small Aggregates. The smallest Aggregate you can design is one with a single Entity, which will serve as the Aggregate Root. 3. Reference Other Aggregates Only By Identity. 4. Use Eventual Consistency Outside the Consistency Boundary. This means that ONLY ONE Aggregate instance will be required to be updated in a single transaction. All other Aggregate instances that must be updated as a result of any one Aggregate instance update can be updated within some time frame (using a Domain Event). The business should determine the allowable time delay. 5. Build Unidirectional Relationship from the Aggregate Root. 8/10/2018 (C) COPYRIGHT METAMAGIC GLOBAL INC., NEW JERSEY, USA 19 2 Data Transfer Object vs. Value Object A small simple object, like money or a date range, whose equality isn’t based on identity. Data Transfer Object Value Object A DTO is just a data container which is used A Value Object represents itself a fix set of to transport data between layers and tiers. data and is similar to a Java enum. 486 It mainly contains of attributes and it’s a A Value Object doesn't have any identity, it is P of EAA serializable object. entirely identified by its value and is immutable. DTOs are anemic in general and do not A real world example would be Color.RED, contain any business logic. Color.BLUE, Currency.USD Java EE 7