Constructing services, dependencies during server creation #17
-
Hey, I'm an "advanced beginner" that's relatively new to golang, and I had a question.
How would your approach to constructing services and dependencies ( |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @Kamariw95 The initialization process would be similar; if the concrete repositories use the same underlying datastore (literally the same host) then they would be initialized with that variable, for example (assuming postgresql-related types): user := postgresl.NewUser(conf.DB)
subtask := postgresl.NewSubtask(conf.DB) Those instances then would be sent to the corresponding svcUser := service.NewUser(user)
svcSubtask := service.NewSubtask(subtask) The "tricky" part of all of this is to properly organize the repositories in a way they make sense for the data they are using behind the scenes, for example, let's assume the hypothetical domain types
If you're using a relational database for storing this, this is a "one to many" relationship, so in this case we have at least two options, either:
// option 1
type TaskRepository interface {
// Create / Delete / Find / Update
DeleteSubtask(ctx context.Context, taskId, subTaskId string) error
FindSubtask(ctx context.Context, taskId, subTaskId string) (internal.Subtask, error)
UpdateSubtask(ctx context.Context, ....) error
} // option 2
type SubtaskRepository interface {
Delete(ctx context.Context, taskId, subTaskId string) error
Find(ctx context.Context, taskId, subTaskId string) (internal.Subtask, error)
Update(ctx context.Context, ....) error
} Both have their tradeoffs (like not being able to share transactions or too large interface types) and choosing one or the other depends on your case; I will cover this is more detail in the future because it gets complicated really quickly when dealing with domain types that include too many types that can't be accessed without their parent. |
Beta Was this translation helpful? Give feedback.
Hello @Kamariw95
The initialization process would be similar; if the concrete repositories use the same underlying datastore (literally the same host) then they would be initialized with that variable, for example (assuming postgresql-related types):
Those instances then would be sent to the corresponding
service
types to initialize the corresponding type, for example:The "tricky" part of all of this is to properly organize the repositories in a way they make sense for the data they are using behind the scenes, for example, let's assume t…