In all tutorials on using datastore with appengine standard go environment suggest that I should obtain appengine context from the http request and use this context when manipulating the datastore. Golang appengine datastore introduction
This is unfortunate because it makes it hard for me to use dependency injection of data services into my controllers (handlers). I can't create one instance of aservice while initializing the controller, but I have to pass requests/contexts to every data related operation.
func createHandler(dbService db.DBService) http.HandleFunc
return func (req http.Request, resp http.Response) {
entity := bindEntity(req)
/*
I undertand that I can pass whole request to the dbService
to at least remove dependency on datastore from this handler
but it doesn't seem right to pass request everywhere
*/
ctx := appengine.NewContext(req)
dbService.StoreEntity(ctx, entity)
}
}
Is there a way of getting the appengine context from somewhere else except for request? Or is there some common known design pattern how to separate the responsibility of handling request and manipulating data in app engine?
dbService := db.CreateServcie(somehowObtainedContext)
func createHandler(dbService db.DBService) http.HandleFunc
return func (req http.Request, resp http.Response) {
entity := bindEntity(req)
dbService.StoreEntity(entity)
}
}
This would make the code clearer and testing easier.