Think about architecture in python

Introduction

This article is the 24th day of the 2020 RevComm Advent Calendar. The day before was @ zoetaka38's "Create a serverless API with Swagger with Serverless/Flask".

I'm @ qii-purine, a lead engineer at RevComm. I am usually involved in the development of MiiTel, ranging from creating small functions to code review and product management.

What kind of architecture do you use for development? What did you think about that choice?

This time, I would like to think about the application architecture in a certain situation.

What is architecture?

What do you associate with architecture? There are MVC, layered architecture, clean architecture, VIPER and so on. In a broad sense, it extends to DDD, DI, CQRS, object-oriented, aspect-oriented, and design patterns.

In any case, it is devised to easily solve problems during development and operation, and is used in various systems.

The conditions for selection are

Well, I said that I thought about the architecture quickly, but I think that there are various conditions depending on the product.

The conditions this time are like this.

--The language is python. --The framework is django (drf). --Create a Restful API, nothing that returns html. --UnitTest does. --More than 10 people make changes.

Candidate

This time as a general candidate Consider three things: a pattern that uses drf's ModelViewSet, a layered architecture, and a clean architecture.

modelViewSet of drf

Here is the reference. https://www.django-rest-framework.org/api-guide/viewsets/#modelviewset

When building complex logic, the following layered architecture mechanism will be partially introduced.

--Code to create

Layered architecture

Use ViewSet, which is the Base Class of ModelViewSet. https://www.django-rest-framework.org/api-guide/viewsets/#viewset-actions

--Code to create --ViewSet (Application layer) --Serializer (Application layer) --UseCase (Domain layer) --Model (Infrastructure layer)

Clean architecture

It's an architecture you've heard a lot over the last few years. By using DI well, you can reverse the dependencies of the infrastructure part.

--Code to create

Solved by mock

The python mock is reasonably powerful and allows you to mock global variables. This makes it possible to test without depending on infrastructure parts such as DB without reversing the dependencies like the clean architecture.

Don't think

You don't have to think about changing frameworks or infrastructure that are unlikely to be encountered. Also, creating a common understanding within the team is necessary no matter what you do. If it's not too difficult, the cost to pay is almost the same.

Minimum Viable

Whichever you choose, there are advantages and disadvantages, but the layered architecture seems to be good in terms of code volume. The disadvantage of being difficult to test can also be solved with mock. Also, DI is a mechanism you want depending on the mounting location, so let's make it available.

In summary, is it like this?

--Code to create

in conclusion

What do you think. This time, it fits in the form of layered architecture + DI, but I think that the selection will change depending on the scale of the product, the team structure, and the development period. If you are a few elite and everyone is familiar with drf, ModelViewSet may be sufficient. Alternatively, a clean architecture may be suitable if you have enough time to operate it for a long period of time.

Choose the architecture that suits you.

Tomorrow is the last day. This is an article by @zomaphone. I look forward to working with you.

Recommended Posts

Think about architecture in python
About __all__ in python
Think about depth-priority and width-priority searches in Python
Think about building a Python 3 environment in a Mac environment
About "for _ in range ():" in python
Sort in Python. Next, let's think about the algorithm.
What beginners think about programming in 2016
About dtypes in Python and Cython
I learned about processes in Python
About python slices
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python
About python yield
About python, class
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
About python inheritance
Programming in python
About python, range ()
Plink in Python
Constant in python
About python decorators
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
About python reference
Puyo Puyo in python
python in virtualenv
PPAP in Python
About Python decorators
Practical example of Hexagonal Architecture in Python
[Python] About multi-process
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Import-linter was useful for layered architecture in Python