I'm trying to apply DDD to a Golang service and sometimes I find that I have too many structs.
For example, I need to satisfy the requirements (for simplicity, I reduce the number of required fields):
- Create user with name, email and password
- Update user name
- Tracking creation time and modification time on database
So if I want to have only the necessary fields for each operation and layer, my code seems like this:
Package user (the domain) with two files
user.go
package user
import "time"
type Repository interface {
Create(u CreateUserInput)
Update(u UpdateUserInput)
}
type CreateUserInput struct {
Name string
Email string
Password string
CreationTime time.Time
ModificationTime time.Time
}
type UpdateUserInput struct {
ID string
Name string
ModificationTime time.Time
}
service.go
package user
import "time"
type Service interface {
Create(u CreateUserCmd)
Update(u UpdateUserCmd)
}
type CreateUserCmd struct {
Name string
Email string
Password string
}
type UpdateUserCmd struct {
ID string
Name string
}
type service struct {
rep Repository
}
func (s service) Create(u CreateUserCmd) {
s.rep.Create(CreateUserInput{
Name: u.Name,
Email: u.Email,
Password: u.Password,
CreationTime: time.Now(),
ModificationTime: time.Now(),
})
}
func (s service) Update(u UpdateUserCmd) {
s.rep.Update(UpdateUserInput{
ID: u.ID,
Name: u.Name,
ModificationTime: time.Now(),
})
}
As you can see I need 4 data structs for two operations. Is this the best approach in order to have safe and maintainable code?
Should I unify structs in repository layer (only one User struct with all fields)? In this case, repository implementer should to know which fields to persist in each operation.