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

5 Best Practices for your successful Angular Applications @BASTA 2019 in Mainz, Germany

5 Best Practices for your successful Angular Applications @BASTA 2019 in Mainz, Germany

Manfred Steyer

September 24, 2019
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer Contents 1) Unidirectional Dataflow and Change Detection 2) OnPush

    w/ Immutables and Observables 3) Smart vs. Dumb Components 4) Cutting Modules and Module Configuration 5) Facades and State Management
  2. @ManfredSteyer About me… • Manfred Steyer • SOFTWAREarchitekt.at Angular Trainings

    and Consultancy • Google Developer Expert (GDE) • Trusted Collaborator for Angular Page ▪ 3 Manfred Steyer Public: Frankfurt, München, Wien In-House: everywhere http://softwarearchitekt.at/workshops
  3. @ManfredSteyer Component-Tree in Angular 2+ Page ▪ 6 Komponente für

    App Komponente (z. B. Liste) Komponente (z. B. Eintrag) Komponente (z. B. Eintrag)
  4. @ManfredSteyer Rules for Property-Bindings • Data flews top/down • Parent

    can pass data to its children • Children cannot pass data to their parents • Dependency-Graph is a tree • Angular just needs one digest to traverse tree Page ▪ 7
  5. @ManfredSteyer Property-Binding Page ▪ 8 model item item {{ item.title

    }} {{ item.title }} [http://victorsavkin.com/post/110170125256/change-detection-in-angular-2]
  6. @ManfredSteyer Event Bindings (One-Way, Bottom/Up) • No digest needed •

    But: Events can change data → Property Binding Page ▪ 10
  7. @ManfredSteyer Property- and Event-Bindings Page ▪ 11 Execute Property-Binding Execute

    Event Handler Event occours App is ready all event handlers executed Properties bound
  8. @ManfredSteyer View Page ▪ 12 <button [disabled]="!von || !nach" (click)="search()">

    Search </button> <table> <tr *ngFor="let flight of flights"> <td>{{flight.id}}</td> <td>{{flight.date}}</td> <td>{{flight.from}}</td> <td>{{flight.to}}</td> <td><a href="#" (click)="selectFlight(flight)">Select</a></td> </tr> </table> <td [text-content]="flight.date"></td>
  9. @ManfredSteyer Recap • Property-Binding: One-Way; Top/Down • Event-Binding: One-Way; Bottom/Up

    • Two-Way-Binding? • Two-Way = Property-Binding + Event-Binding Page ▪ 14
  10. @ManfredSteyer Property and Event-Bindings Page ▪ 16 <input [ngModel]="from" (ngModelChange)="from

    = $event"> Property + Change Changed Value <input [(ngModel)]="from">
  11. @ManfredSteyer OnPush flights flight flight {{ flight.id }} {{ flight.id

    }} FlightSearch Card Card Angular just checks when “notified”
  12. @ManfredSteyer "Notify" about change? • Change bound data (@Input) •

    OnPush: Angular just compares the object reference! • e. g. oldFlight === newFlight • Raise event within the component • Notify a bound observable • {{ flights$ | async }} • Trigger it manually • Don't do this at home ;-) • At least: Try to avoid this
  13. Thought experiment • What if <flight-card> would be "smarter"? •

    Executing Use Case logic? • Talking to the backend? • Traceability? • Performance? • Reuse?
  14. Smart vs. Dumb Components Smart Component •Drives the "Use Case"

    •Usually a "Container" Dumb •Independent of Use Case •Reusable •Usually a "Leaf"
  15. @ManfredSteyer Typical Module Structure Page ▪ 28 AppModule … …

    … SharedModule Root Module Feature Modules Shared Module
  16. @ManfredSteyer Use Case Services Use Case Service Component Component Resource

    Service Resource Service Use Case Service Component
  17. @ManfredSteyer More about this on Wed … • Wartbare Angular-Anwendungen

    dank State Management Cornelia Rauch 11:30, Gutenberg 4 • Angular, Monorepos und Strategic Domain-driven Design für nachhaltige Architekturen Manfred Steyer 18:00, Forum West
  18. @ManfredSteyer Summary • Event --> Properties • No Cycles allowed

    Change Detection • Immutables • Observables OnPush • Smart: Knows Use Case • Dumb: Does NOT know Use Case --> Reusable Smart vs. Dumb Components
  19. @ManfredSteyer Summary • 1 Module per Use Case • Shared

    Modules Cutting Modules • Facade per Use Case • Introduce State Libs behind Facade when needed Facades and State Management