hexagonal architecture for the web

49
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0/ Hexagonal Architecture for the Web Leganés, 11 y 12 de febrero Jesús Espejo

Upload: jesus-espejo-hidalgo

Post on 13-Jan-2017

310 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Hexagonal architecture for the web

Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0/

Hexagonal Architecture for the Web

Leganés, 11 y 12 de febrero

Jesús Espejo

Page 2: Hexagonal architecture for the web

Technical Telecom Engineer ESP: Telematics (UJAEN, 2012)

Doing web stuff since 2007Probably a bit earlier…

Back-end Engineer at LOOPSince 2013. Between Salzburg and Berlin.

jespejo89jespejo89

Passionate about Web Services / APIsAnd skydiving.

Jesús Espejo

ABOUT ME

Page 3: Hexagonal architecture for the web

STRATEGYSET THE FRAME

DESIGN AND UXADD SOUL

TECHNOLOGYMAKE IT WORK

SOCIALMAKE ‘EM TALK

MOBILEFIRST CHOICE

INNOVATIONALL SET FOR 2020

MOTIONAND PHOTOGRAPHY

CONTENTAND INFLUENCERS

ACTIVATIONTARGET THEM

ANALYTICSUNTIL IT‘S DONE

ABOUT LOOP

Page 4: Hexagonal architecture for the web

HOW DID EVERYTHING START?

Page 5: Hexagonal architecture for the web
Page 6: Hexagonal architecture for the web

1ST GENERATION

PLAIN-HTML-STATIC PAGES

Page 7: Hexagonal architecture for the web

2ND GENERATION

DYNAMIC PAGES

Page 8: Hexagonal architecture for the web

3RD GENERATION

DYNAMIC PAGES 2.0

Page 9: Hexagonal architecture for the web

4TH GENERATION

DYNAMIC PAGES 2.0MULTIPLE DEVICES

SERVICES

Page 10: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

MODEL

ENCAPSULATESDATA

VIEW

INTERACTS WITH USER

CONTROLLER

PROCESSES REQUESTS

SEPARATES RESPONSIBILITIES

Page 11: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

BROWSER CONTROLLER

VIEW MODEL DATABASE

CONCERNS ARE CLEAR

Aren’t they?

Page 12: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

ACL System...

Data validation...

File processing...

File upload...

Email sending... ?Geolocate user...

Page 13: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

BROWSER

CONTROLLERACL SYSTEMFILE UPLOAD

DATA VALIDATIONEMAIL SENDING

FILE PROCESSINGGEOLOCATE USER

VIEW MODEL DATABASE

Page 14: Hexagonal architecture for the web
Page 15: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

Mobile API...

Admin CMS...

Push notifications... ?

Page 16: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

BROWSER

VIEW

MODEL

DATABASE

APICONTROLLER

MOBILE APP

FRONTEND CONTROLLER

LIBRARIES

BACKENDCONTROLLER

BROWSER

VIEW

Page 17: Hexagonal architecture for the web
Page 18: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

Cronjobs...

External services... ?

Page 19: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

BROWSER

VIEW

MODEL

DATABASE

APICONTROLLER

MOBILE APP

FRONTEND CONTROLLER

LIBRARIES

BACKENDCONTROLLER

BROWSER

VIEW

CRONJOB /CONTROLLER

EXTERNAL SERVICES

Page 20: Hexagonal architecture for the web
Page 21: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

BROWSER

VIEW

DATABASE

APICONTROLLER

MOBILE APP

FRONTEND CONTROLLER

LIBRARIES

BACKENDCONTROLLER

BROWSER

VIEW

CRONJOB /CONTROLLER

EXTERNAL SERVICES

MODEL MODEL MODEL. . .

REAL LIFE

Page 22: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

?Add a new feature...

Upgrade a library...

Page 23: Hexagonal architecture for the web

MVC ARCHITECTURAL PATTERN

BROWSER

VIEW

DATABASE

APICONTROLLER

MOBILE APP

FRONTEND CONTROLLER

LIBRARIES

BACKENDCONTROLLER

BROWSER

VIEW

CRONJOB /CONTROLLER

EXTERNAL SERVICES

MODEL MODEL MODEL. . .

REAL LIFE

Page 24: Hexagonal architecture for the web

ADDING A NEW FEATURE

Page 25: Hexagonal architecture for the web

UPDATING THE FACEBOOK SDK

Page 26: Hexagonal architecture for the web

TECHNICAL DEBT

TIME

CO

ST

OF

CH

AN

GE

MAX. ALLOWED

Page 27: Hexagonal architecture for the web

The complexity of the architecture grows along with the complexity

of our app.

Page 28: Hexagonal architecture for the web

Sometimes MVC isnot enough

Page 29: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

ALSO KNOWN AS PORTS AND ADAPTERS

INTRODUCTION TO THE

Page 30: Hexagonal architecture for the web

Identify the aspects that vary and separatethem from what stays the same

Page 31: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

DATABASEINFRASTR.

LOGGINGINFRASTR.

EMAILINFRASTR.

OTHERSERVICES

MOBILEAPI

USERINTERFACE

COMMANDLINE

TESTS

PUBLICAPI

DOMAIN

APPLICATION SERVICES

FRAMEWORK

Page 32: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

DOMAIN

BEHAVIORS AND

CONSTRAINTS

What the application can and cannot do.

Set of entities and how they interact.

Page 33: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

DOMAIN

APPLICATION SERVICES

ORCHESTRATES THE

DOMAIN

Uses domain entities and other application services to provide

higher-level services.

Page 34: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

ADAPTS REQUESTS

Defines how external services can interact to our app, and how our

app interacts with external services.

DOMAIN

APPLICATION SERVICES

FRAMEWORK

Page 35: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

BOUNDARIES

Interfaces that define how

the inner layer can be accessed by the

outer layer.

DOMAIN

APPLICATION SERVICES

FRAMEWORK

Page 36: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

DEPENDENCY INVERSION

Dependency always flows

from outside in.

“Hollywood principle”

DOMAIN

APPLICATION SERVICES

FRAMEWORK

Page 37: Hexagonal architecture for the web

• Flexibility to changes.

• Code stable and easily testable.

• Low coupling.

• Code easy to maintain.

• Independence from UI.

• Independence from infrastructures.• Framework independence.

• Reusable code.

LowTechnical Debt

WHAT ACHIEVE WITH HEXAGONAL ARCHITECTURE

Page 38: Hexagonal architecture for the web

EXAMPLEQUICK

WITH NO CODE

Page 39: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

DOMAIN

User

Media

Comment

…User-> attachMedia( Media )

Media->attachComment( Comment )

DOMAIN

APPLICATION SERVICES

FRAMEWORK

Page 40: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

BOUNDARIES

UserRepositoryInterface

UserDAOInterface

MediaRepositoryInterface

CommentRepositoryInterface

DOMAIN

APPLICATION SERVICES

FRAMEWORK

Page 41: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

SERVICES PROVIDED(AND USED)

ValidationService

NotificationService

FilesystemService…

UploadMediaCommand

CreateMediaCommand

AddCommentCommand(using services above)

DOMAIN

APPLICATION SERVICES

FRAMEWORK

Page 42: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

BOUNDARIES

Each service or command

would implement their own interface, if it proceeds.

DOMAIN

APPLICATION SERVICES

FRAMEWORK

Page 43: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

ADAPTS REQUESTS

From outside > IN

MediaAPIController

LikeAPIController

MediaWebController

TestMediaUploadCommand

NotifyMediaUploadCLIAdapter

DOMAIN

APPLICATION SERVICES

FRAMEWORK

Page 44: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

ADAPTS REQUESTS

From inside > OUT

RedisCachingServices

MediaMongoDBRepository

UsersMySQLRepository

MandrillAppNotificationService

AmazonS3Service

DOMAIN

APPLICATION SERVICES

FRAMEWORK

Page 45: Hexagonal architecture for the web

HEXAGONAL ARCHITECTURE

UploadMediaCommand

USER MEDIA-­>attachMedia(                                  )

ValidationService

NotificationService

FilesystemService

ValidationLib

AmazonS3Adapter Amazon S3

DOMAINAPP. SERVICESFRAMEWORK

LEGEND:

INFRASTRUCT.

PusherAdapter Pusher

MediaWebController

MediaAPIController

MobileDevice Tests

WebBrowser

??

CreateMediaMEDIA =

Page 46: Hexagonal architecture for the web

The app is not tied to a UI, a protocol or a framework.

WHAT WE HAVE ACHIEVED

The app is not built upon specific systems or

infrastructures, but it rather uses them as services.

Modules in the app are not coupled so they can be easily exchangeable and tested in isolation.

Page 47: Hexagonal architecture for the web

LET’S RECAP

• How people consume web services has evolved.

• MVC has dominated the web world.• Sometimes MVC is not enough.• Hexagonal architecture might be useful.• Identify the aspects that vary and separate them from what

stays the same.

• Don’t depend on frameworks, systems or infrastructures: Apply Dependency Inversion instead.

Page 48: Hexagonal architecture for the web

QUESTIONS

Page 49: Hexagonal architecture for the web

THANK YOUFOR YOUR ATTENTION

VISIT OUR WEBSITE

www.agentur-loop.com