1
头图
If you can't accept TypeScript, it is not recommended to continue reading this series of articles

This article does not introduce what is clean architecture and MVP mode in detail, check the related articles at the end of the article by yourself.

A rough introduction to clean architecture

The following figure shows the most primitive structure of the clean architecture:
image.png

  1. Entities/Models: Entity layer. Officially, it encapsulates some of the most common logic in the enterprise. It can also be called Models. It may be an object that only contains data fields, or it may contain methods, with some business logic. More in-depth is the content related to domain-driven (DDD) development.
  2. Use Case/Service: Contains core business logic.
  3. Interface Adapter: Adaptation layer/glue layer, responsible for data conversion between the entity and use case of the inner circle and the external interfaces of the outer circle. It is necessary to convert the data of the outer service into the data that the inner entity and usecase can consume, and vice versa.
  4. External Interfaces: Dependent external services, third-party frameworks, and page UIs that need to be pasted all belong to this layer. This layer is completely unaware of any logic in the inner circle, so no matter how this layer changes (the component library is controlled by Element UI Changed to Ant Design Vue, database changed from MySQL to PostgreSQL), should not affect the inner circle.

Isn't this the backend mvc?

benefit

  1. Better unit testing: business logic can be tested without UI, database, web server or any other external infrastructure.
  2. Reduce dependence on UI framework: Cross-end development, the business logic layer can be directly reused, and only the UI layer needs to be adapted.
  3. Infrastructure independence: The inner business logic does not need to care about where the data comes from and where it is submitted, such as LocalStroage, IndexedDB, Web SQL, JSbridge, HTTP, and WebSocket.

Converted to a structure suitable for front-end engineering

image.png

Both Service and Model are pure JS classes/objects, independent of the framework.

The dotted line of the basic service does not directly call the corresponding API, but encapsulates it in a tool library or function.

How to really land?

The presenter/controller creates the model, the service instance, and injects the model into the service. The model is used in the service to do business logic calculations. At the same time, the service method is called in the presenter/controller to process the view event, and the model is returned to the view layer for data binding.

Question: model is a pure JS object, how to trigger view update when model data changes?

mobx, @formily/reactive turns models into responsive; making wheels - publish-subscribe model

How to land in actual projects

image.png

Use vue composition api or react hooks to build models, so model is a custom hooks.

The presenter/controller is also a custom hooks, which can call our own defined hooks internally, as well as third-party open source hooks libraries, such as ahooks, vueuse

Because the model is a custom hook, when the internal state is updated, it will trigger the update of the view.

Reference article

Front-end Architecture 101 (6): Clean Architecture is the destination - Zhihu (zhihu.com)

Clean Frontend Architecture: Clean Frontend Architecture | clean-frontend (phodal.github.io)

Dry Goods | Ctrip Ticket React Native Clean Architecture Practice (qq.com)

Dry Goods | Trip.com Air Ticket React Native Clean Architecture 2.0 Practice - Tencent Cloud Developer Community - Tencent Cloud (tencent.com)

Moving away from ReactJs and VueJs on front-end using Clean Architecture - DEV Community 👩‍💻👨‍💻


若邪
1.5k 声望64 粉丝

划水摸鱼糊屎