Practical example of Hexagonal Architecture in Python

I've been writing a Hexagonal Architecture-based backend in-house recently and it's working pretty well (I'm going to), so I'll try to write the basic essence super-easily.

Implementation example of TagService

Domain layer


from zope.interface import Interface
from .base import Entity, ValueObject

class Post(Entity):
    u""" Post Model """
    ...
    
class Tag(ValueObject):
    u""" Tag Model """    
    ...

class ITagService(Interface):    # specification
    u""" Tag management domain service """
    def add_tag(post, tag):
        u""" add tag to a post """

    def remove_tag(post, tag):
        u""" remove tag from a post """

Please refer to the following articles for ValueObject and zope.interface.

Infrastructure layer

from zope.interface import implementer
from .domain.model import ITagService

@implementer(ITagService)    # implement TagService as described in ITagService
class TagService(object):
    u""" TagService using xxx """
    def add_tag(self, post, tag):
        u""" add tag to a post """
        # do some actual staff

    def remove_tag(self, post, tag):
        u""" remove tag from a post """
        # do some actual staff


@implementer(ITagService)
class DummyTagService(object):      # i.e. memory based implementation
    u""" TagService for tests """
    def add_tag(self, post, tag):
        u""" add tag to a post """
        # do some actual staff

    def remove_tag(self, post, tag):
        u""" remove tag from a post """
        # do some actual staff

Application layer

from .infrastructure import TagService

def add_tag(post, tag):
    srv = TagService()
    srv.add_tag(post, tag)    # use TagService as described in ITagService

test

--Ensure that DummyTagService and TagService behave as expected at the domain tier. --Use DummyTagService when testing objects that use TagService.

About the role of the domain layer

I was asked internally, "Do you need ITagService?", But I think the role of the domain layer is to present "specifications" to each of the application layer and the infrastructure layer. .. With the domain layer, the infrastructure layer can be implemented to provide that interface, and the app layer can use the model according to that interface.

If you compare it with the ECMAScript specification,

--The ECMAScript specification is domain layer, --Google Chrome developers implement the browser according to the ECMAScript specification and --App developers develop JavaScript apps according to the ECMAScript specifications

With the domain layer in between, both feature providers and users can benefit from each other.

Recommended Posts

Practical example of Hexagonal Architecture in Python
Think about architecture in python
Equivalence of objects in Python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Implementation of quicksort in Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
Pixel manipulation of images in Python
Division of timedelta in Python 2.7 series
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Waveform display of audio in Python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
Conversion of string <-> date (date, datetime) in Python
(Bad) practice of using this in Python
General Theory of Relativity in Python: Introduction
Output tree structure of files in Python
Display a list of alphabets in Python 3
Comparison of Japanese conversion module in Python3
Summary of various for statements in Python
Example of 3D skeleton analysis by Python
The result of installing python in Anaconda
Gang of Four (GoF) Patterns in Python
The basics of running NoxPlayer in Python
Bulk replacement of strings in Python arrays
Project Euler # 16 "Sum of Powers" in Python
Traffic Safety-kun: Recognition of traffic signs in Python
Summary of built-in methods in Python list
Non-logical operator usage of or in python
In search of the fastest FizzBuzz in Python
Project Euler # 17 "Number of Characters" in Python
Double pendulum equation of motion in python
Get rid of DICOM images in Python
Status of each Python processing system in 2020
Project Euler # 1 "Multiples of 3 and 5" in Python
Meaning of using DI framework in Python
An example of the answer to the reference question of the study session. In python.
Quadtree in Python --2
Python in optimization
CURL in python
Output the number of CPU cores in Python
Metaprogramming in Python
Draw a graph of a quadratic function in Python
Python 3.3 in Anaconda
Introduction of Python
SendKeys in Python
[Python] Sort the list of pathlib.Path in natural sort
Receive websocket of kabu station ® API in Python
Example of taking Python> function> * args as arguments
Import-linter was useful for layered architecture in Python
Summary of how to import files in Python 3
Project Euler # 10 "sum of prime numbers" in Python
Unattended operation of Google Spreadsheets (etc.) in Python
Get the caller of a function in Python
Match the distribution of each group in Python
Epoch in Python
Discord in Python
Make a copy of the list in Python
Real-time visualization of thermography AMG8833 data in Python
Sudoku in Python