how to implement domain driven design in golang
For example, I want to build a todo list application.
Introduction
I want to design a product with frontend, backend, and infrastructure. So I want to build a microservice for the product. The first question is that I will need to know how to split a service, so I want to use Domain-Driven Design to break down each service.
Product description
Functional
Main
- add task by title
- list all tasks
- delete task
- modify task
Architecture design
Domain-Driven Design
Account
: define a userTask
: as a representation of all tickets
graph account task
Implementation
Project layout
I follow Standard Go Project Layout
Layered architecture
graph user:((user)) --> presentation:(presentation layer) --> biz:(business layer) --> repo:(repository layer) --> data:[(data source)]
Presentation layer
The presentation layer is the highest layer of the software. It can be a Restful even Grpc, graphQL, and CLI.
folder layout:
flowchart TD root-->/cmd /cmd-->/restful /restful-->/account/main.go /restful-->/task/main.go /cmd-->/grpc /grpc-->account1:["/account/main.go"] /cmd-->/cronjob /cmd-->/graphql
Business layer
As the name of this layer, most of the logic of the application will be placed in this layer. For example, if we have TaskBiz
we expect that create, complete, and delete task happens there like in a real life. he Biz layer allows developers to design applications that support multiple user interfaces, this minimizes the chances of needless code duplication. Usually, this layer is divided into smaller pieces that are easy to manage.
folder layout:
flowchart TD root-->/internal/app/domain/account/biz/impl.go root-->/pkg/entity/domain/account/biz/interface.go
Repository layer
The repository layer is the layer that implements communication with the data source (usually a database). No matter if we’re fetching the data from the DB or saving/modifying objects, it’s the place where SQL queries or ORM operations should be placed.
folder layout: internal/app/domain/account/biz/repo/interface.go