DEV Community

Cover image for Using Domain-Driven Design(DDD)in Golang

Using Domain-Driven Design(DDD)in Golang

Steven Victor on February 22, 2020

Domain-Driven Design pattern is the talk of the town today. Domain-Driven Design(DDD) is an approach to software development that simplifies the c...
Collapse
 
ejemba profile image
Epo Jemba

Hi Steven, thank you for the article, there is not much DDD articles so this is good thing.

However, I have some observations regarding implementation details.

1) You start by "DDD comprises of 4 Layers" Domain, Infrastructure, Application, Interfaces then ... You put Infrastructure inside Domain ! Infrastructure package should be on the same level as Domain, Application and Interfaces. That is because Infrastructure layer brings support to all 3 others layers, not only domain. You can define go service interface inside your application package then have its implementation inside infrastructure because the implementation brings some weird dependencies.

2) packages "utils" :S in DDD you should not have an "utils" package. Everything have a place.
Auth and Security are infrastructure stuff

Middleware and FileUpload are Interfaces stuff

3) You talk about Entities but not about Agregates, you separate Repositories from Entities, their should be colocated in the same package , the "agregate" which name should be the aggregate name. Modularity is a big principle of DDD. Interchangeable component losely coupled with big cohesion.

4) In food_app.go in the application layer, there is a Service Interface , good. But there is no Factory for it, the struct implementation is just public to everyone "type FoodApp struct{...}" , it should be private and be called something like "type foodService struct {}" Factories are important because they help construct the object (inject services if needed, instrument it, etc )

5) The factory of your repository should
a) be inside the infrastructure package
func NewUserRepository(db *gorm.DB) repository.UserRepository
b) it takes an infrastructure object gorm.DB that the caller should not handle

6) You could use the DomainRegistry pattern to handle the restitution of the different factories (service, repo, aggregates). DomainRegistry interface in domain root then the DomainRegistry implementation in the infrastructure

In fact, there are other concerns but I think you get the point. Tactical patterns of DDD are direction to follow this is not strict if we respect certain rules.

Don't get me wrong this is just a fair critics. Because every one is learning, me the first. But from what I see the article can confuse first time DDD players because you break some principles.

If you want I can fork you repository and propose something we can discuss on.

Collapse
 
stevensunflash profile image
Steven Victor

Great. Thanks, a lot Epo for the feedback.

For the first point, The Infrastructure Layer is actually on the same level as the Domain. This was not the case in the first push. It had long been corrected. Please check.

For the second point, I'm currently refactoring to have the Auth in the infrastructure(have been a bit busy at work), I will probably update before the week runs out.

For the third point, I will refactor to that. Thanks.

For the fourth point, that was an oversight. The change has been effected now.

For the fifth point, I didn't quite get you. But this no longer applies in the implementation above.
func NewUserRepository(db *gorm.DB) repository.UserRepository
The article is currently in a constant state of editing as constructive feedback such as yours are given. If you don't mind, can you elaborate on your fifth point?

Please go ahead and fork the repository, your changes are welcome.

Thanks again for the feedback.

Collapse
 
kolkov profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
Andrey Kolkov

damianopetrungaro.com/posts/ddd-us...
I think it's the best articles about DDD and Go.

Collapse
 
cardozo17 profile image
José Antonio Cardozo • Edited

Hello Victor. First of all I wanted to thank you because your project have been very helpful as an example for implementing a project of my own. I have a question for you about the code tho. Why in the api in the refresh token method you just erase the refresh token and not the previous generated access token in the redis db. It seems like it will always remain an orphan access token in the redis database if you do this I think. Is there any reason for you to do that?. Thanks again.

Collapse
 
stevensunflash profile image
Steven Victor

Hi Jose,

For we to ever use the Refresh Token, it means the Access Token must have expired(and Redis deletes it automatically).

Collapse
 
cardozo17 profile image
José Antonio Cardozo

Hi Steven. Thanks for the answer. That makes sense. I will check it out it might be something on my implementation. The other thing is I don't see in the code in the frontend where you are encripting the password with the Bcrypt. Is that missing? or am I just don't seeing it?.

Thank you.

Thread Thread
 
stevensunflash profile image
Steven Victor

Hi,
The password is encrypted on the backend.
Go to this path: infrastructure/security/password.go
The function that hashes the password is defined there.

Collapse
 
nicolasassi profile image
Nicolas Augusto Sassi

Hey there! I got little confused with the Application layer in your example... It just feels a bit pointless as it only reproduces the Repository and doesn't actually do anything different than that. Could you please explain or give some example of how the application layer could grow in a way that it gets more "usefull"?

Thanks!

Collapse
 
tranluongtuananh profile image
TranLuongTuanAnh

Hi Steven, thank you for the article, I have a question!

Why do you pass a repository as param when init interface?
Such as services.Food is passed as params of interfaces.NewFood initialization.
foods := interfaces.NewFood(services.Food, services.User, fd, redisService.Auth, tk)

I think that pass a application is better choice! How do you think about it?

Collapse
 
stevensunflash profile image
Steven Victor

Hi bro, Sorry for the late reply. Just seeing this now. interfaces.NewFood() is a constructor, so we inject dependencies of other services. This is considered best practice, so you avoid the usage of globals in your code.

Collapse
 
hades32 profile image
Martin Rauscher

Why did you choose to return interfaces e.g. in your repository creation functions? Isn't that against recommended Go best practices?

Collapse
 
stevensunflash profile image
Steven Victor

Hi Martin. Thanks for the feedback.
In order to answer your question properly, can you include a code snippet to a comment where you observed this?

Thanks.

Collapse
 
alikhil profile image
Alik Khilazhev

For those who are looking for articles about DDD in Go, I would recommend reading this

Collapse
 
kolkov profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
Andrey Kolkov

damianopetrungaro.com/posts/ddd-us... very good article about DDD with Golang.

Collapse
 
martinsaporiti profile image
martinsaporiti

Great article. I have a question. Is it correct that the entity "User" knows about gorm? Should the entities know about de persistence strategy? thanks in advance.

Collapse
 
rpoletaev profile image
Roman Poletaev

I can't understand why people choose golang on this way

Collapse
 
kolkov profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
Andrey Kolkov

What's problem with Golang and DDD?
damianopetrungaro.com/posts/ddd-us...

Some comments have been hidden by the post's author - find out more