Giter Site home page Giter Site logo

go-clean-architecture's Introduction

go-clean-architecture

The right way to implement clean architecture on golang (I think) ๐Ÿ˜‹. Promotion stuff: to help you easier writing code you can check https://kodingless.com . This platform was made inspired from this go-clean-architecture

Description

The Clean Architecture is a software architecture proposed by Robert C. Martin (better known as Uncle Bob). In this repository, the contents are list of examples of implementation of Clean Architecture in Golang. The examples will using real world scenario.

New in clean architecture? I suggest you to look at examples/video-rest-api: https://github.com/herryg91/go-clean-architecture/tree/main/examples/video-rest-api

To understand more about Clean Architecture: https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html

Dependency Rule

Based on Uncle Bob, there are 4 layers:

  • Entity
  • Use Cases
  • Interface Adapters
  • Frameworks and Drivers.

In this repository, we also using 4 layers (with modification) like this:

  • Entity.
  • Use Cases (Implementation).
  • Interface Adapters. Will be splitted into two:
    • Repository Interface. Bridging repository implementation and use cases layer.
    • Use Case Interface. Bridging handler and use cases layer.
  • Driver Layer
    • Handler
    • Repository implementation

Design Structure

Diagram

drawing

Folder Structure

.
โ”œโ”€โ”€ app                             # inner layer of the pattern. we focus on writting the logic here
โ”‚   โ”œโ”€โ”€ repository                  # repository interface. The software in this layer is a set of adapters that convert data from/to the format most convenient for the use cases and entities, from/to the format most convenient for some external agency such as the Database or the Web
โ”‚       โ”œโ”€โ”€ video_repo.go
โ”‚       โ”œโ”€โ”€ user_repo.go
โ”‚   โ”œโ”€โ”€ usecase                     # usecase layer
โ”‚       โ”œโ”€โ”€ usecase1
โ”‚           โ”œโ”€โ”€ errors.go
โ”‚           โ”œโ”€โ”€ implement.go
โ”‚           โ”œโ”€โ”€ interface.go
โ”‚       โ”œโ”€โ”€ usecase2
โ”‚           โ”œโ”€โ”€ errors.go
โ”‚           โ”œโ”€โ”€ implement.go
โ”‚           โ”œโ”€โ”€ interface.go
โ”œโ”€โ”€ client                          # third party, api client, grpc client/wrapper
โ”‚   โ”œโ”€โ”€ test1-api
โ”‚       โ”œโ”€โ”€ dto.go
โ”‚       โ”œโ”€โ”€ client.go
โ”‚   โ”œโ”€โ”€ test2-api
โ”‚   โ”œโ”€โ”€ test3-grpc
โ”‚   โ”œโ”€โ”€ test4-grpc
โ”œโ”€โ”€ config
โ”œโ”€โ”€ entity                          # encapsulate Enterprise wide business rules (struct with methods)
โ”‚   โ”œโ”€โ”€ entity1.go
โ”‚   โ”œโ”€โ”€ entity2.go
โ”‚   โ”œโ”€โ”€ ...
โ”œโ”€โ”€ handler                         # outermost layer / drivers which driving the business logic. implementation http/grpc, worker handler, etc.
โ”‚   โ”œโ”€โ”€ api.go
โ”‚   โ”œโ”€โ”€ subscribe.go
โ”‚   โ”œโ”€โ”€ worker.go
โ”œโ”€โ”€ pkg                             # Supporting library / script
โ”‚   โ”œโ”€โ”€ helpers                     
โ”‚   โ”œโ”€โ”€ password                    
โ”‚   โ”œโ”€โ”€ ...                    
โ”œโ”€โ”€ repository                      # implementation of repository. in this repository we can aggregate from db, client, external agency, etc. into convenient format for this service
โ”‚   โ”œโ”€โ”€ video_repository_v1
โ”‚       โ”œโ”€โ”€ model.go
โ”‚       โ”œโ”€โ”€ repository.go
โ”‚   โ”œโ”€โ”€ video_repository_v2
โ”‚       โ”œโ”€โ”€ model.go
โ”‚       โ”œโ”€โ”€ repository.go
โ”‚   โ”œโ”€โ”€ user_repository_v1
โ”‚       โ”œโ”€โ”€ model.go
โ”‚       โ”œโ”€โ”€ repository.go
โ”œโ”€โ”€ go.mod
โ”œโ”€โ”€ go.sum
โ”œโ”€โ”€ main.go
โ”œโ”€โ”€ service.yaml
โ””โ”€โ”€ README.md

Guidelines

Step-by-step writing code using this pattern

  • Setup skeleton of the microservices (including: main.go, migrations, config, pkg and handler folder)
  • Defining the entities
  • Defining usecase (interface & implementation) in folder app. We're gonna focus in this folder since the business logic will be written here.
  • When the usecase need to communicate to the external agency (Database, other apis, etc) then write it to the repository interface
  • After the usecase layer was done, now time to write repository implementation
  • Put it up together + register to the main.go and handler folder

How to Define Entity

Entities encapsulate Enterprise wide business rules. An entity can be an object with methods, or it can be a set of data structures and functions. Therefore, we can got a clue to defining the entity by look into the business rule. My suggestion is analyze the outermost layer of the service (handler folder), then look into the param and the output struct.

example:

GET http://{host}/profile/{Id}

return 200
{
    "id": 1,
    "name: "test",
    "age: 20,
}

Look, the output. Actually it's the indication that we need `Profile` entity. In golang we can write it like this:
type Profile struct{
    Id int
    Name string
    Age int
}

How to Define Repository

Repository in this design pattern had a task to convert data from/to the format most convenient entities and usecases, from/to the format most convenient for external agencies such as Database, Api/Grpc/ etc. By that definition, my suggestion is to define the repository by domain/aggregate root. 1 domain/aggregate root = 1 repository

To define the domain itself, we can list down the entities and group it together by looking to the common requirement/terminology/functionality (aggregate root). https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-design

Example, we have entity like this:

  • user
  • user_contact
  • user_parent
  • login_token Then, we can put user, user_contact, user_parent into domain user and create user_repository. We can put login_token into token_repository

How to Define UseCase

Like it's name, usecase strongly tied to user story/journey. Therefore the point of view is user centric. To get a clue about how to define the usecase, I suggest you to look up into the Product Requirement or UseCase diagram or List of endpoint you need to serve in your microservice.

Example, we have users-api which need to serve endpoint api like this:

  • Login (POST http://{host}/auth/login)
  • Logout (POST http://{host}/auth/logout)
  • Register (POST http://{host}/auth/register)
  • ShowProfile (POST http://{host}/profile)
  • EditProfile (PUT http://{host}/profile)

then, based on the product requirement we can split it into 2 usecase: AuthenticationUsecase (Login, Logout, Register) and ProfileUsecase (ShowProfile, EditProfile)

Future examples

  • Rest API service with external dependency
  • Rest API service with background worker
  • Rest API service with event driven system (pubsub)
  • Clean architecture on Command line interface (CLI)
  • Clean architecture in Contract Driven Development
  • etc.

go-clean-architecture's People

Contributors

herryg91 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.