Coder sans peur du changement avec la meme pas mal hexagonal architecture

  • View
    1.034

  • Download
    0

  • Category

    Software

Preview:

Citation preview

@cyriux @tpierrain #MemePasMal

Coder sans peur du changement, avec la "même pas mal !" architecture hexagonale

Cyrille MARTRAIRE (@cyriux) Arolla !

Thomas PIERRAIN (@tpierrain) Société Générale

@cyriux @tpierrain #MemePasMal

CODER  SANS  PEUR  DU  CHANGEMENT,  AVEC  LA  "MÊME  PAS  MAL  !"  ARCHITECTURE  HEXAGONALE

Cyrille MARTRAIRE (@cyriux) Arolla !

Thomas PIERRAIN (@tpierrain) Société Générale

Architecte

Société Générale

Thomas Pierrain

6

7

Passionate developer

PARIS Since 1999

!

@cyriux

Cyrille Martraire

Paris Software Craftsmanship Community

http://www.meetup.com/paris-software-craftsmanship/

TDDBDDDDDLegacy

Architecte Technique

Central

Architecte Technique

Central

Your own Sweets Box Online!Use-cases

1. Choose your box and the sweets to put inside

2. Get a price 3. Order, & consult

past orders

Let’s start

As usual: Chose the DB &

Design the DB schema!

STOP !

Alistair Cockburn

Uncle Bob Martin

Hexagonal

100% Domain Inside

Thin layer of Use-cases

Ports/Adapters all around

Adapter

Dependencies always towards the inside!

X

Allowed way for dependencies

Dependencies always towards the inside!

Vers  l’intérieur,  qu’il  

dit  le  monsieur  !

Ports/Adapters?“REST” Port

“REST” Adapter

“EMS“ Adapter

“Tibco EMS”

Port

Hexagonal?

Users Vs. Providers

THEY NEED ME

I NEED THEM

API (Application Provider Interface)

SPI (Service Provider Interface)

Tests Vs. Production

Deliver value to PROD

Verify behavior in TESTTest Robots Fakes, Mocks

Clients Stores etc.

Scenario-Driven…

Given a sweets box of size XL And a selection of 500g of M&Ms And assuming the following prices | item | price | currency | | XL Box | 2.50 | EUR | | M&M (100g)| 5.00 | EUR | !

When I ask for a price Then the price is 27.50 EUR

Modeling the domain…

Sweets

CustomSweetsBox

SweetsBoxPricerAllOrders

Catalog Order

ShippingCostEstimator

Caramels

Nougat

Fudge

Modeling the domain…

Sweets

CustomSweetsBox

SweetsBoxPricerAllOrders

Catalog Order

ShippingCostEstimator

Caramels

Nougat

Fudge

Beware of

Modelism!

I need to call external services, but I don’t want to know about

them

DIP principle

X

DIP principle

Interface

Adapter

interface

interface

REPOSITORY

interface

Start with mocks

MOCK

Add an html web app…

“REST” Adapter

We can demo!

Quick Win 1

Focus on what really

matters

Implement the repository

Implement the repository

On the

cloudz!

Implement actual persistence

interface

Amazon

Adaptor

Même pas mal !(“that didn't hurt one bit“)

Quick Win 2

Choose the technology last

(when we know best)

47

MAX IGNORANCE

48

Who’s  that  architect  who  can’t  decide  the  technologies  early?

Ebay as additional retailer?

Ebay as additional retailer!

“REST” Adapter

Ebay Adapter

Même pas …

New shipping provider?

interface

FEDEX

Adaptor

New shipping provider?!

Again:

Même pas mal !!!

Quick Win 3

Embrace the changes

X

Allowed way for dependencies

Hexagonal architecture

Quick Win 4

Low couplingHigh cohesion

Before (legacy architecture)

After (Hexagonal Architecture)

Quick Win 5Our business code is not victim of IT fads

anymore!

61

Hexagonal Architecture

Use & Abuse!

Need other areas of behaviors?

• Business intelligence? • Supply Chain management • CRM? • …

Même pas ! ;-)

Hexagonal all the things

New Bounded Contexts? New Micro-services?

Main benefits !

1.Focus on what really matters (business value first!) ➔ have you ever tried DDD?

!

2.Choose the technology last (when we know best our concrete needs) !

3.Embrace the changes (“Même pas mal !” / “that didn't hurt one bit“)

Thanks!Cyrille MARTRAIRE @cyriux

Thomas PIERRAIN @tpierrain

@cyriux @tpierrain #MemePasMal

CODER  SANS  PEUR  DU  CHANGEMENT,  AVEC  LA  "MÊME  PAS  MAL  !"  ARCHITECTURE  HEXAGONALE

Cyrille MARTRAIRE (@cyriux) Arolla !

Thomas PIERRAIN (@tpierrain) Société Générale

70

FUZZY

FUZZY

FUZZY

Pattern

Port Vs

Adapter?

Port = Existing Techno Adapter = Your code

You disagree?

Not that important.

Symmetries?

80

Not that important.

Inside vs.

Outside

Who’s talking about hexagonal?

Alistair Cockburn

84

”Uncle Bob” Martin

85

Steeve Freeman & Nat Pryce (GOOS)

86

Vaughn Vernon (IDDD)

87

Jeffrey Palermo (Onion Architecture)

88

h;p://pragprog.com/magazines/2009-­‐12/going-­‐naked

Depends  on  nothing

Pragmatic Programmers

Many notations!

Still, not well-known

+--------------+!| presentation |!|--------------|!| domain |!|--------------|!| persistence |!+--------------+

Layers?

+-----+-------------+----------+!| gui | file system | database |!|-----+-------------+----------+!| domain |!|------------------------------+

Infra the other way round!

Layering

http://matteo.vaccari.name/blog/archives/154

Inside vs.

Outside

Un Dedans et

Un Dehors

Un Dedans et

Un Dehors#des reperes pour nos enfants

For real

98

99

Ted Neward

We’re launching a startup, man.

Sentiment Analysis !

Automatically extract the sentiment of English sentences submitted on the web, using a given lexicon. Keep track of the

submitted sentences.

Your Turn !

5mn Small groups

Any technology / approach

Propose an architecture on paper !

Then we’ll debrief a selection of them

Sentiment Analysis !

Automatically extract the sentiment of English sentences submitted on the web, using a given lexicon. Keep track of the

submitted sentences.

105

How to enforce?

108http://books.sonatype.com/mvnref-book/reference/flex-dev-sect-creating-with-archetype.html

Maven multi-

module

109

http://clarkware.com/software/JDepend.html

JDepend

110https://leanpub.com/livingdocumentation/

111

Living Diagrams

4  novel  coupling  metrics  1. Undesirable  Dependency  Count  2. Undesirable  Distinct  Dependency  Count  3. Undesirable  Dependency  Max  Repetition  4. Undesirable  Distinct  Assembly  Count  

!  

Bad

Good

●Provoke conversations

Conformist vs.

Hexagonal

Conformist (accidental)

Hexagonal Architecture

Hexadecimal Architecture:

Need at least 16 layers in the code

Architecture = ?

Architecture !

”The key technology choices”BOF…

Architecture !

”What everybody should know”

Architecture !

”What’s hard to change”

Architecture !

”What’s irreversible”

Reversible decisions FTW!

Defer technology decisions

Change your

mind easily

50% TECH 50% COMM

50% TECH 50% COMM (10% MATHS)

Communication FTW!

AGRE

E

ON

MAXIM

S

”Le principe du nombril : on regarde vers l’intérieur”

Repository

Persistence Ignorance

!

!

•You  can  defer  decisions  about  persistence  (and  UI)  for  a  long  ^me  •Your  domain  must  NOT  care!

Un  service,  la  facade  coté  mé^er  d’un  stockage  !

Sans  jamais  parler  de  base  de  données.

Repository

h;p://www.andeka.co.cc/2011/07/postbox-­‐251.html

Repository

h;p://www.andeka.co.cc/2011/07/postbox-­‐251.html

Interface

DAO

Repository

In Legacy

Bubble Context

• Create a little bubble of innovation !

• Brand new model !

• As clean as we like • Highly tested • High hygiene standards

Bubble Context

• ACL to protect from the legacy code !

• Translates legacy objects & services into new, cleaner ones

Anti-corruption layer

Module (lib)

<<ValueObject<<ValueObject<<ValueObject>> <<ValueObject

<<ValueObject>>

<<Service>>  <<SPI>>

<<Service>>  <<API>>

<<En7ty>>  <<Aggregate>>

Module  ==  

library

Accès  aux  services  extérieurs

Module  ==  library

Accès  aux  services  extérieurs

Hexagonal Architecture

Scenario  test  data

Adapters

Bounded Contexts

Bounded Contexts

Il  n’est  pas  possible  de  convenir  du  sens  précis  des  mots  u7lisés  par  un  grand  nombre  de  personnes.  

!

Il  faut  accepter  ce  fait,  et  donc  définir  dans  quel  contexte  un  langage  est  clairement  défini  sans  ambiguité.

Different

MY activity is not the same as YOUR event

Merci !

hFp://cathy313.centerblog.net/539-­‐bisounours

Wrap-up

• Concepts & Messages – Subir -> Délibéré  – Intérieur Vs Extérieur  – Gérer ses dépendances  – Langage interne / externe  – DIP  • Exemples • Kata

Content• When not to go Hexagonal?  – Conformist – Facebook game – Too small (connector…)  !

• Advanced aspects  – The adapter pattern  – Tech ports diversity – 1 Adapter – many interfaces  – 1 interface – many adapters – Data push  – Session management  – Adapter as a module indeed

Content 2Domain model as a Pure Functions (100% no Entity inside) vs as Aggregates !

-- The Repository pattern Reuse slides

Hexagonal for a Bubble Context within a legacy !

Ports = surrounding legacy Adapters = repository / adapter !

Cloakroom / clandestine passenger patterns

Content 3How to enforce? !Discipline Maven multi modules, .Net projects Static analysis: JDepend rules, NDepend, Sonar plugins — Bounded Context Reuse slides !Conformist vs ACL isolation Same language or not? !Autonomous Components / Polyglot persistence !Influence of sync vs async / push vs pull on the inside of the hexagon !Examples?

Living Documentation Convention (YES)? Annotation on packages? !The Bounded Contexts / the Domain models vs the rest (ici un seul Bounded Context) !Domain model -> inside Everything else - that implements something from the domain model -> at the right - everything else that only calls -> at the left !

Apply DDD!

153

http://www.lexicalscope.com/blog/category/patterns/

http://matteo.vaccari.name/blog/archives/154

Problem:  write  a  program  that  1.  Loads  a  set  of  employee  records  from  a  flat  file    2.  Sends  a  greetings  email  to  all  employees  whose  birthday  is  today    !The  flat  file  is  a  sequence  of  records,  separated  by  newlines;  this  are  the  first  few  lines:  last_name, first_name, date_of_birth, email !Doe, John, 1982/10/08, john.doe@foobar.com !Ann, Mary, 1975/09/11, mary.ann@foobar.com !The  greetings  email  contains  the  following  text:  Subject:  Happy  birthday!  Happy  birthday,  dear  John!    with  the  first  name  of  the  employee  substituted  for  “John”  !3.  REST  service  to  query  past  greetings  !HINT:  today  is  a  tech  port!  !!

+-----+-------------+----------+!| gui | file system | database |!|-----+-------------+----------+!| domain |!|------------------------------+

+--------------+!| presentation |!|--------------|!| domain |!|--------------|!| persistence |!+--------------+

Exercise

Postgres Sql + Gui (+ myBatis ?) Audit et log dans le domain + Metrics