SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Hexagonal Design in Django
Maarten van Schaik
Django
• Rapid application development
• ./manage.py startapp polls
• Define some models, forms, views and we’re
in business!
Applications live and grow
• More features are added:
– API access
– Reporting
– Command Line Interface
– Integration with other applications
– Who knows what else…
Connected Design
• Components can access all other components
• When you need to access a piece of data or a
piece of logic, just import and use it
• Development is fast
Modular design
• Access to other components goes through
well-defined interfaces (API’s) using well-
defined protocols
• Components have high cohesion
• Components are loosely
coupled
Connected vs Modular
Ports and Adapters
Ports and Adapters
• Specific adapter for each use of the
application, e.g. web view, command line,
message queue, etc.
• Each adapter connects to a port of the
application
• Mock adapters and test harnesses facilitate
testing
• Testing becomes easier and faster
Typical Django app
• __init__.py
• admin.py
• forms.py
• models.py
• tests.py
• urls.py
• views.py
App
Uh oh…
• Where is my application?
??
Models
Views
Forms
Refactoring Django apps
• Rules
– Core domain model cannot depend on Django
– Tell objects, ask values
Implications
• Core model cannot depend on framework
– Core model cannot derive from models.Model
– Communication with Django goes through
adapters
• Tell, don’t ask
– Views should render from immutable values
– So no vote.save() in views.py!
Example – Poll
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(
pk=request.POST*‘choice’+)
except (KeyError, Choice.DoesNotExist):
return render(request, …, ,error: …-)
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(…)
Example – Poll
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(
pk=request.POST*‘choice’+)
except (KeyError, Choice.DoesNotExist):
return render(request, …, ,error: …-)
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(…)
Mutating data!
Example – Poll (2)
def vote(request, poll_id):
try:
poll_engine.register_vote(poll_id, request.POST*‘choice’+)
except Exception as e:
return render(request, …, ,error: …-)
else:
return HttpResponseRedirect(…)
Example – Poll (2)
## Poll engine
def register_vote(poll_id, choice_id):
p = Poll.objects.get(pk=poll_id)
selected_choice = p.choice_set.get(pk=choice_id)
selected_choice.votes += 1
selected_choice.save()
Example – Poll (2)
## Poll engine
def register_vote(poll_id, choice_id):
p = Poll.objects.get(pk=poll_id)
selected_choice = p.choice_set.get(pk=choice_id)
selected_choice.votes += 1
selected_choice.save()
Dependency on Django models
Example – Poll (3)
## Poll engine
def register_vote(poll_id, choice_id):
if not poll_repository.choice_exists(poll_id, choice_id):
raise PollException(…)
poll_repository.increment_vote_count(choice_id)
Example – Poll (3)
## Django model adapter
def choice_exists(poll_id, choice_id):
return Choice.objects.filter(
poll_id=poll_id, pk=choice_id).exists()
def increment_vote_count(choice_id)
Choice.objects.filter(pk=choice_id).update(
votes=F(‘votes’)+1)
Conclusions
• Hexagonal design will help keep speed of
adding new features constant
• Encourages modularity and encapsulation
• Encourages clean and well-organized
applications
• Tests become faster when using plain objects
and data
• Django models are not that useful without
coupling with them
That’s it
Thanks and references
• Matt Wynne: Hexagonal Rails
• Kent Beck: To Design or Not To Design?
• Alistair Cockburn: Hexagonal Architecture
• Django tutorial

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (8)

ABulletin
ABulletinABulletin
ABulletin
 
Www.deprojectinrichter.com
Www.deprojectinrichter.comWww.deprojectinrichter.com
Www.deprojectinrichter.com
 
Foglio 23 13a
Foglio 23 13aFoglio 23 13a
Foglio 23 13a
 
V mware cdcr-11-q3-en-case-study
V mware cdcr-11-q3-en-case-studyV mware cdcr-11-q3-en-case-study
V mware cdcr-11-q3-en-case-study
 
Eksistensi manusia
Eksistensi manusia Eksistensi manusia
Eksistensi manusia
 
Nog meer handige auto apps
Nog meer handige auto appsNog meer handige auto apps
Nog meer handige auto apps
 
Pdf motorway
Pdf motorwayPdf motorway
Pdf motorway
 
AB Zuid-Holland
AB Zuid-HollandAB Zuid-Holland
AB Zuid-Holland
 

Ähnlich wie Hexagonal Design - Maarten van Schaik

External Data in Puppet 4
External Data in Puppet 4External Data in Puppet 4
External Data in Puppet 4ripienaar
 
PuppetConf. 2016: External Data in Puppet 4 – R.I. Pienaar
PuppetConf. 2016: External Data in Puppet 4 – R.I. PienaarPuppetConf. 2016: External Data in Puppet 4 – R.I. Pienaar
PuppetConf. 2016: External Data in Puppet 4 – R.I. PienaarPuppet
 
GDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App EngineGDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App EngineYared Ayalew
 
AOP in Python API design
AOP in Python API designAOP in Python API design
AOP in Python API designmeij200
 
Django Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
Django Introduction Osscamp Delhi September 08 09 2007 Mir NazimDjango Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
Django Introduction Osscamp Delhi September 08 09 2007 Mir NazimMir Nazim
 
Python & Django TTT
Python & Django TTTPython & Django TTT
Python & Django TTTkevinvw
 
PyCon Sweden 2022 - Dowling - Serverless ML with Hopsworks.pdf
PyCon Sweden 2022 - Dowling - Serverless ML with Hopsworks.pdfPyCon Sweden 2022 - Dowling - Serverless ML with Hopsworks.pdf
PyCon Sweden 2022 - Dowling - Serverless ML with Hopsworks.pdfJim Dowling
 
Automate Studio Training: Materials Maintenance Tips for Efficiency and Ease ...
Automate Studio Training: Materials Maintenance Tips for Efficiency and Ease ...Automate Studio Training: Materials Maintenance Tips for Efficiency and Ease ...
Automate Studio Training: Materials Maintenance Tips for Efficiency and Ease ...Precisely
 
django_introduction20141030
django_introduction20141030django_introduction20141030
django_introduction20141030Kevin Wu
 
Utilisation de MLflow pour le cycle de vie des projet Machine learning
Utilisation de MLflow pour le cycle de vie des projet Machine learningUtilisation de MLflow pour le cycle de vie des projet Machine learning
Utilisation de MLflow pour le cycle de vie des projet Machine learningParis Data Engineers !
 
Machine learning in action at Pipedrive
Machine learning in action at PipedriveMachine learning in action at Pipedrive
Machine learning in action at PipedriveAndré Karpištšenko
 
Play framework: lessons learned
Play framework: lessons learnedPlay framework: lessons learned
Play framework: lessons learnedPeter Hilton
 
Automated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesAutomated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesTao Xie
 
Maximizer 2018 API training
Maximizer 2018 API trainingMaximizer 2018 API training
Maximizer 2018 API trainingMurylo Batista
 
Practices and tools for building better APIs
Practices and tools for building better APIsPractices and tools for building better APIs
Practices and tools for building better APIsNLJUG
 
Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)Peter Hendriks
 

Ähnlich wie Hexagonal Design - Maarten van Schaik (20)

Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
External Data in Puppet 4
External Data in Puppet 4External Data in Puppet 4
External Data in Puppet 4
 
PuppetConf. 2016: External Data in Puppet 4 – R.I. Pienaar
PuppetConf. 2016: External Data in Puppet 4 – R.I. PienaarPuppetConf. 2016: External Data in Puppet 4 – R.I. Pienaar
PuppetConf. 2016: External Data in Puppet 4 – R.I. Pienaar
 
GDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App EngineGDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App Engine
 
AOP in Python API design
AOP in Python API designAOP in Python API design
AOP in Python API design
 
IGears: Template Architecture and Principles
IGears: Template Architecture and PrinciplesIGears: Template Architecture and Principles
IGears: Template Architecture and Principles
 
Django Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
Django Introduction Osscamp Delhi September 08 09 2007 Mir NazimDjango Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
Django Introduction Osscamp Delhi September 08 09 2007 Mir Nazim
 
Python & Django TTT
Python & Django TTTPython & Django TTT
Python & Django TTT
 
PyCon Sweden 2022 - Dowling - Serverless ML with Hopsworks.pdf
PyCon Sweden 2022 - Dowling - Serverless ML with Hopsworks.pdfPyCon Sweden 2022 - Dowling - Serverless ML with Hopsworks.pdf
PyCon Sweden 2022 - Dowling - Serverless ML with Hopsworks.pdf
 
Automate Studio Training: Materials Maintenance Tips for Efficiency and Ease ...
Automate Studio Training: Materials Maintenance Tips for Efficiency and Ease ...Automate Studio Training: Materials Maintenance Tips for Efficiency and Ease ...
Automate Studio Training: Materials Maintenance Tips for Efficiency and Ease ...
 
django_introduction20141030
django_introduction20141030django_introduction20141030
django_introduction20141030
 
Utilisation de MLflow pour le cycle de vie des projet Machine learning
Utilisation de MLflow pour le cycle de vie des projet Machine learningUtilisation de MLflow pour le cycle de vie des projet Machine learning
Utilisation de MLflow pour le cycle de vie des projet Machine learning
 
Python Open Session.pptx
Python Open Session.pptxPython Open Session.pptx
Python Open Session.pptx
 
Machine learning in action at Pipedrive
Machine learning in action at PipedriveMachine learning in action at Pipedrive
Machine learning in action at Pipedrive
 
Play framework: lessons learned
Play framework: lessons learnedPlay framework: lessons learned
Play framework: lessons learned
 
Tango with django
Tango with djangoTango with django
Tango with django
 
Automated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and ChallengesAutomated Developer Testing: Achievements and Challenges
Automated Developer Testing: Achievements and Challenges
 
Maximizer 2018 API training
Maximizer 2018 API trainingMaximizer 2018 API training
Maximizer 2018 API training
 
Practices and tools for building better APIs
Practices and tools for building better APIsPractices and tools for building better APIs
Practices and tools for building better APIs
 
Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)
 

Mehr von Byte

Beaumotica.com & Magento 2 - Meet Magento 2016
Beaumotica.com & Magento 2 -  Meet Magento 2016Beaumotica.com & Magento 2 -  Meet Magento 2016
Beaumotica.com & Magento 2 - Meet Magento 2016Byte
 
Hackers traced - Meet Magento 2016
Hackers traced -  Meet Magento 2016Hackers traced -  Meet Magento 2016
Hackers traced - Meet Magento 2016Byte
 
Magento 2 performance - a benchmark
Magento 2 performance - a benchmarkMagento 2 performance - a benchmark
Magento 2 performance - a benchmarkByte
 
Presentatie snelheid ecl
Presentatie snelheid eclPresentatie snelheid ecl
Presentatie snelheid eclByte
 
Workshop New Relic - juni 2015
Workshop New Relic - juni 2015Workshop New Relic - juni 2015
Workshop New Relic - juni 2015Byte
 
Google Webmasters Tools
Google Webmasters ToolsGoogle Webmasters Tools
Google Webmasters ToolsByte
 
APMG juni 2014 - Regular Expression
APMG juni 2014 - Regular ExpressionAPMG juni 2014 - Regular Expression
APMG juni 2014 - Regular ExpressionByte
 
Hypernode Pitch @ Meet Magento 2014
Hypernode Pitch @ Meet Magento 2014Hypernode Pitch @ Meet Magento 2014
Hypernode Pitch @ Meet Magento 2014Byte
 
Joomladagen 2014 - Google Tag Manager
Joomladagen 2014 - Google Tag ManagerJoomladagen 2014 - Google Tag Manager
Joomladagen 2014 - Google Tag ManagerByte
 
Exception Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerException Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerByte
 
Presentatie MUG 27 juni 2013 - Graphite/New Relic
Presentatie MUG 27 juni 2013 - Graphite/New RelicPresentatie MUG 27 juni 2013 - Graphite/New Relic
Presentatie MUG 27 juni 2013 - Graphite/New RelicByte
 
Mm13 nl presentatie byte
Mm13 nl presentatie byteMm13 nl presentatie byte
Mm13 nl presentatie byteByte
 
Site gehacked... hoe op te lossen?
 Site gehacked... hoe op te lossen? Site gehacked... hoe op te lossen?
Site gehacked... hoe op te lossen?Byte
 
Varnish & Magento
Varnish & MagentoVarnish & Magento
Varnish & MagentoByte
 
Redis - Magento User Group
Redis - Magento User GroupRedis - Magento User Group
Redis - Magento User GroupByte
 
Help! My site has been hacked!
Help! My site has been hacked!Help! My site has been hacked!
Help! My site has been hacked!Byte
 
Magento Speed Analysis - Meet Magento 2012
Magento Speed Analysis - Meet Magento 2012Magento Speed Analysis - Meet Magento 2012
Magento Speed Analysis - Meet Magento 2012Byte
 
Hosting & Onderhoud Joomladagen 2012
Hosting & Onderhoud Joomladagen 2012Hosting & Onderhoud Joomladagen 2012
Hosting & Onderhoud Joomladagen 2012Byte
 
10 Joomla vragen - Joomladagen 2010
10 Joomla vragen - Joomladagen 201010 Joomla vragen - Joomladagen 2010
10 Joomla vragen - Joomladagen 2010Byte
 
Magento Hosting, Performance & Stabiliteit - Meet Magento 2011
Magento Hosting, Performance & Stabiliteit - Meet Magento 2011Magento Hosting, Performance & Stabiliteit - Meet Magento 2011
Magento Hosting, Performance & Stabiliteit - Meet Magento 2011Byte
 

Mehr von Byte (20)

Beaumotica.com & Magento 2 - Meet Magento 2016
Beaumotica.com & Magento 2 -  Meet Magento 2016Beaumotica.com & Magento 2 -  Meet Magento 2016
Beaumotica.com & Magento 2 - Meet Magento 2016
 
Hackers traced - Meet Magento 2016
Hackers traced -  Meet Magento 2016Hackers traced -  Meet Magento 2016
Hackers traced - Meet Magento 2016
 
Magento 2 performance - a benchmark
Magento 2 performance - a benchmarkMagento 2 performance - a benchmark
Magento 2 performance - a benchmark
 
Presentatie snelheid ecl
Presentatie snelheid eclPresentatie snelheid ecl
Presentatie snelheid ecl
 
Workshop New Relic - juni 2015
Workshop New Relic - juni 2015Workshop New Relic - juni 2015
Workshop New Relic - juni 2015
 
Google Webmasters Tools
Google Webmasters ToolsGoogle Webmasters Tools
Google Webmasters Tools
 
APMG juni 2014 - Regular Expression
APMG juni 2014 - Regular ExpressionAPMG juni 2014 - Regular Expression
APMG juni 2014 - Regular Expression
 
Hypernode Pitch @ Meet Magento 2014
Hypernode Pitch @ Meet Magento 2014Hypernode Pitch @ Meet Magento 2014
Hypernode Pitch @ Meet Magento 2014
 
Joomladagen 2014 - Google Tag Manager
Joomladagen 2014 - Google Tag ManagerJoomladagen 2014 - Google Tag Manager
Joomladagen 2014 - Google Tag Manager
 
Exception Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerException Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim Muller
 
Presentatie MUG 27 juni 2013 - Graphite/New Relic
Presentatie MUG 27 juni 2013 - Graphite/New RelicPresentatie MUG 27 juni 2013 - Graphite/New Relic
Presentatie MUG 27 juni 2013 - Graphite/New Relic
 
Mm13 nl presentatie byte
Mm13 nl presentatie byteMm13 nl presentatie byte
Mm13 nl presentatie byte
 
Site gehacked... hoe op te lossen?
 Site gehacked... hoe op te lossen? Site gehacked... hoe op te lossen?
Site gehacked... hoe op te lossen?
 
Varnish & Magento
Varnish & MagentoVarnish & Magento
Varnish & Magento
 
Redis - Magento User Group
Redis - Magento User GroupRedis - Magento User Group
Redis - Magento User Group
 
Help! My site has been hacked!
Help! My site has been hacked!Help! My site has been hacked!
Help! My site has been hacked!
 
Magento Speed Analysis - Meet Magento 2012
Magento Speed Analysis - Meet Magento 2012Magento Speed Analysis - Meet Magento 2012
Magento Speed Analysis - Meet Magento 2012
 
Hosting & Onderhoud Joomladagen 2012
Hosting & Onderhoud Joomladagen 2012Hosting & Onderhoud Joomladagen 2012
Hosting & Onderhoud Joomladagen 2012
 
10 Joomla vragen - Joomladagen 2010
10 Joomla vragen - Joomladagen 201010 Joomla vragen - Joomladagen 2010
10 Joomla vragen - Joomladagen 2010
 
Magento Hosting, Performance & Stabiliteit - Meet Magento 2011
Magento Hosting, Performance & Stabiliteit - Meet Magento 2011Magento Hosting, Performance & Stabiliteit - Meet Magento 2011
Magento Hosting, Performance & Stabiliteit - Meet Magento 2011
 

Kürzlich hochgeladen

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Kürzlich hochgeladen (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Hexagonal Design - Maarten van Schaik

  • 1. Hexagonal Design in Django Maarten van Schaik
  • 2. Django • Rapid application development • ./manage.py startapp polls • Define some models, forms, views and we’re in business!
  • 3. Applications live and grow • More features are added: – API access – Reporting – Command Line Interface – Integration with other applications – Who knows what else…
  • 4. Connected Design • Components can access all other components • When you need to access a piece of data or a piece of logic, just import and use it • Development is fast
  • 5. Modular design • Access to other components goes through well-defined interfaces (API’s) using well- defined protocols • Components have high cohesion • Components are loosely coupled
  • 8. Ports and Adapters • Specific adapter for each use of the application, e.g. web view, command line, message queue, etc. • Each adapter connects to a port of the application • Mock adapters and test harnesses facilitate testing • Testing becomes easier and faster
  • 9. Typical Django app • __init__.py • admin.py • forms.py • models.py • tests.py • urls.py • views.py
  • 10. App Uh oh… • Where is my application? ?? Models Views Forms
  • 11. Refactoring Django apps • Rules – Core domain model cannot depend on Django – Tell objects, ask values
  • 12. Implications • Core model cannot depend on framework – Core model cannot derive from models.Model – Communication with Django goes through adapters • Tell, don’t ask – Views should render from immutable values – So no vote.save() in views.py!
  • 13. Example – Poll def vote(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) try: selected_choice = p.choice_set.get( pk=request.POST*‘choice’+) except (KeyError, Choice.DoesNotExist): return render(request, …, ,error: …-) else: selected_choice.votes += 1 selected_choice.save() return HttpResponseRedirect(…)
  • 14. Example – Poll def vote(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) try: selected_choice = p.choice_set.get( pk=request.POST*‘choice’+) except (KeyError, Choice.DoesNotExist): return render(request, …, ,error: …-) else: selected_choice.votes += 1 selected_choice.save() return HttpResponseRedirect(…) Mutating data!
  • 15. Example – Poll (2) def vote(request, poll_id): try: poll_engine.register_vote(poll_id, request.POST*‘choice’+) except Exception as e: return render(request, …, ,error: …-) else: return HttpResponseRedirect(…)
  • 16. Example – Poll (2) ## Poll engine def register_vote(poll_id, choice_id): p = Poll.objects.get(pk=poll_id) selected_choice = p.choice_set.get(pk=choice_id) selected_choice.votes += 1 selected_choice.save()
  • 17. Example – Poll (2) ## Poll engine def register_vote(poll_id, choice_id): p = Poll.objects.get(pk=poll_id) selected_choice = p.choice_set.get(pk=choice_id) selected_choice.votes += 1 selected_choice.save() Dependency on Django models
  • 18. Example – Poll (3) ## Poll engine def register_vote(poll_id, choice_id): if not poll_repository.choice_exists(poll_id, choice_id): raise PollException(…) poll_repository.increment_vote_count(choice_id)
  • 19. Example – Poll (3) ## Django model adapter def choice_exists(poll_id, choice_id): return Choice.objects.filter( poll_id=poll_id, pk=choice_id).exists() def increment_vote_count(choice_id) Choice.objects.filter(pk=choice_id).update( votes=F(‘votes’)+1)
  • 20. Conclusions • Hexagonal design will help keep speed of adding new features constant • Encourages modularity and encapsulation • Encourages clean and well-organized applications • Tests become faster when using plain objects and data • Django models are not that useful without coupling with them
  • 22. Thanks and references • Matt Wynne: Hexagonal Rails • Kent Beck: To Design or Not To Design? • Alistair Cockburn: Hexagonal Architecture • Django tutorial