how to implement domain driven design in golang
For example, I want to build a todo list application.
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.
- add task by title
- list all tasks
- delete task
- modify task
Account: define a user
Task: as a representation of all tickets
graph account task
I follow Standard Go Project Layout
graph user:((user)) --> presentation:(presentation layer) --> biz:(business layer) --> repo:(repository layer) --> data:[(data source)]
The presentation layer is the highest layer of the software. It can be a Restful even Grpc, graphQL, and CLI.
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
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.
flowchart TD root-->/internal/app/domain/account/biz/impl.go root-->/pkg/entity/domain/account/biz/interface.go
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.