clean-arch

module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2023 License: MPL-2.0

README

Clean Architecture

Go Report Card Go Reference Release

This project demonstrates an example Go (Golang) realization of the Clean Architecture using the Gin Gonic web framework. The GORM library and a PostgreSQL DBMS are used for data persistence and podman based testing codes are provided too.

Four main layers are seen as depicted in the following component diagram. These layers are usually drawn as co-centered circles. The inner-most layer is the Entities layer which is also known as the Models or Domain layer. This layer contains the entity types which are defined in the business domain and may be used by a high-level implementation of interesting use cases, without technology-specific implementation details. These models are expected to have the least foreseeable changes since their changes are likely to propagate to other layers. As an example entity in this project, we can mention the model.Car type. These models may be used by both of the Use Cases and Adapters layers. The standard language libraries are treated similarly too.

The Use Cases or Use Case Interactors layer contains the core business logic of a system. This code may use the defined models for different purposes such as data passing, requests/responses formatting, or data persistence. This layer can be seen as a facade for the entire system too as it defines the acceptable public use cases of a system. The high-level implementation of this layer makes it relatively stable as it only depends on the domain entities and does not need to change whenever a third-party library is updated or when a new technology is supposed to be adapted. As an example type from this layer, take a look at the carsuc.UseCase type.

Clean Architecture Layers

A concrete method is required in order to wire up the use cases of a system to the use cases of other systems, allowing them to interact based on their expected and exposed public interfaces. For example, a web framework such as Gin Gonic requires a series of HandlerFunc functions in order to call them when a web request is recevied which may be different from how they are managed in Echo. Conversely, use cases require to employ functionalities of an ORM like the GORM in order to store or search among models, having a database management system server. The Adapters layer which is also know as Controllers or Gateways is responsible to fill these gaps without making the frameworks and third-party libraries on one hand and the use case interactors on the other hand dependent on each other. The use cases layer contains repo.Cars example interface in order to show its expectations from a cars repository. This interface is realized by carsrp.Repo from the adapters layer, where it uses GORM and Pgx libraries for interaction with a PostgreSQL DBMS server. The adapters layer depends on both of our high-level logics and other libraries provided/required interfaces in order to adapt them together. To be precise, the Controllers is not a suitable alias for this layer because adapters should be thin and focus on converting interfaces or simple serialization/deserialization tasks, while the use cases layer contains the more complex business-level flow controls. Parsing configuration such as config.Config and instantiating components from other layers during the system startup are also a part of this layer.

The outmost layer contains Frameworks and Libraries which are usually implemented independent of the main project. Their codes are also independent of our system and may change from time to time, or we may need to replace them with alternatives as new technologies are introduced. Because it is desired to keep our codes immune to changes in the APIs of those libraries, adapters layer has to hide their details by realizing the use cases layers interfaces and implementing any missing functionality or mismatched expectations. Indeed, these frameworks are parallels to our use cases layer, but in their own project.

Read More

For further reading, you may check

  • Martin, R. C., Grenning J., & Brown S. (2017). Clean architecture.
  • Lano, K., & Yassipour Tehrani, S. (2023). Introduction to Clean Architecture Concepts. In Introduction to Software Architecture: Innovative Design using Clean Architecture and Model-Driven Engineering (pp. 35-49). Cham: Springer Nature Switzerland.

Directories

Path Synopsis
cmd
caweb
Package main is the main entry point of the clean-arch web project.
Package main is the main entry point of the clean-arch web project.
internal
test/dbcontainer
Package dbcontainer is an internal helper for the test packages.
Package dbcontainer is an internal helper for the test packages.
pkg
adapter/config
Package config is an adapter which allows users to write a yaml configuration file and allow the caweb to instantiate different components, from the adapter or use cases layers, using those configuration settings.
Package config is an adapter which allows users to write a yaml configuration file and allow the caweb to instantiate different components, from the adapter or use cases layers, using those configuration settings.
adapter/db/postgres
Package postgres provides an adapter for a PostgreSQL database in order to expose the interfaces which are required in the github.com/momeni/clean-arch/pkg/core/repo package.
Package postgres provides an adapter for a PostgreSQL database in order to expose the interfaces which are required in the github.com/momeni/clean-arch/pkg/core/repo package.
adapter/db/postgres/carsrp
Package carsrp is the adapter for the cars repository.
Package carsrp is the adapter for the cars repository.
adapter/restful/gin
Package gin wraps the gin-gonic web framework and lists the middlewares which are expected to be enabled/disabled by the configuration settings.
Package gin wraps the gin-gonic web framework and lists the middlewares which are expected to be enabled/disabled by the configuration settings.
adapter/restful/gin/carsrs
Package carsrs realizes the cars resource, allowing the cars manipulation REST APIs to be accepted and delegated to the cars use cases respectively.
Package carsrs realizes the cars resource, allowing the cars manipulation REST APIs to be accepted and delegated to the cars use cases respectively.
adapter/restful/gin/routes
Package routes contains all resource packages and facilitates instantiation and registration of all repo, use case, and resource packages based on the user provided configuration settings.
Package routes contains all resource packages and facilitates instantiation and registration of all repo, use case, and resource packages based on the user provided configuration settings.
adapter/restful/gin/serdser
Package serdser contains the reusable serialization/deserialization logics in order to be used by the resource packages.
Package serdser contains the reusable serialization/deserialization logics in order to be used by the resource packages.
core/cerr
Package cerr represents the core layer errors.
Package cerr represents the core layer errors.
core/model
Package model defines the inner most layer of the Clean Architecture containing the business-level models, also called entities or domain.
Package model defines the inner most layer of the Clean Architecture containing the business-level models, also called entities or domain.
core/repo
Package repo specifies the expected interfaces for management of repositories, including a database connections pool which can be used concurrently by several goroutines, how individual connections and transactions may be obtained from it, which repositories exist, and which actions may be performed on them.
Package repo specifies the expected interfaces for management of repositories, including a database connections pool which can be used concurrently by several goroutines, how individual connections and transactions may be obtained from it, which repositories exist, and which actions may be performed on them.
core/usecase/carsuc
Package carsuc contains the cars UseCase which supports the cars related use cases.
Package carsuc contains the cars UseCase which supports the cars related use cases.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL