How would you handle service/domain relationship? #50
-
Hi @benbjohnson, My question is about what will be the best way to handle the case when the service might need several other services to produce its data? for instance, let's say I have an application that manages Location (like google place or foursquare location) and a user can follow this location or search for a location. I can see something like this in my root package myapp
type Location struct {} // represent a location with ID, name, address etc
type LocationRepository interface { ...} // Database CRUD for location
type LocationSearch interface { ... } // Search a place by using google map API or foursquare API
type follower struct {} user that follow a location (user ID+location ID and meta)
type FollowerRepository interface { ...} // Database CRUD for folloer
type User struct {} // application user ID, name etc but then the question is, how can I model a service that will give my should I create a new struct in the root that combines repository interfaces? type LocationFollower struct {
Location
Follower []*User
}
type LocationFollowerService struct {
location LocationRepository
follower FollowerRepository
}
func (lfs *LocationFollower) fetchLocation(ctx context.Context, locationID string) (LocationFollower, err) {...} Or should I do something different? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @anthonycorbacho. Yes, I'd have a struct in the root that combines both the |
Beta Was this translation helpful? Give feedback.
Hi @anthonycorbacho. Yes, I'd have a struct in the root that combines both the
LocationRepository
& theFollowerRepository
. The only things I'd change about your implementation are: 1) renamefetchLocation()
toFetchLocation()
so it's exported, and 2) maybe find a different name forLocationFollower
. I'm not sure what exactly though. It doesn't feel like a name I'd use to describe it IRL.