Closed ghost closed 9 years ago
Well, the logs definitely show that all the snapshots, listeners and controllers are typed nils.
I can't really test my assumptions without code that compiles, but I think the issue is that your calling define with a pointer to a pointer of a pointer.
The example code and tests only use one or two layers of pointers, where the pointer can be resolved to a locally scoped variable, which is then populated by either a pointer or a primitive. But your code has &appContainer.Listeners
which is a pointer to Listeners
which is an interface, which accepts pointers (e.g. &ListenerContainer{}
), but is itself a member of appContainer, which is a pointer to an AppContainer
...
So there's a couple of ways you can resolve the references to make this work, but I think the most readable would be something like this:
var (
pingController controller.Controller
feedController controller.Controller
linkController controller.Controller
)
graph.Define(
&pingController,
inject.NewProvider(controller.NewPingController),
)
graph.Define(
&feedController,
inject.NewProvider(controller.NewFeedController),
)
graph.Define(
&linkController,
inject.NewProvider(controller.NewLinkController),
)
controllerContainer := &ControllerContainer{
PingController: pingController,
FeedController: feedController,
LinkController: linkController,
}
and this:
var (
listeners ContainerInterface
snapshots ContainerInterface
controllers ContainerInterface
)
graph.Define(
&listeners,
inject.NewProvider(NewListenerContainer),
)
graph.Define(
&snapshots,
inject.NewProvider(NewSnapshotContainer),
)
graph.Define(
&controllers,
inject.NewProvider(NewControllerContainer),
)
graph.ResolveAll()
appContainer := &AppContainer{
Listeners: listeners,
Snapshots: snapshots,
Controllers: controllers,
}
Another alternative is to put in a dereference:
graph.Define(
&*controllerContainer.PingController,
inject.NewProvider(controller.NewPingController),
)
Let me know of one of those works for you.
On another note, I'm probably recommend against storing the graph as a "global", but I don't think that's your immediate problem.
It's probably a good idea to add constructor arguments to NewAppContainer
and NewControllerContainer
and pass in their dependencies. That way your constructors aren't dictating the implementations of their dependencies, which makes them brittle, less reusable, and less extensible. Instead, the main function should probably define which constructors to use for which variable.
Alternatively you might be able to pass the graph itself as a constructor argument and the constructors could lazily resolve only the objects that they need by type. You would have to supply the exact struct you needed, in this case, because you have multiple dependencies with the same interface. But there is still some benefit to only hardcoding the struct, rather than the constructor, because you're at least deferring the dependencies of your dependencies.
I'm gonna assume that resolved it. Feel free to reopen, if not.
Hi!
Nice package and good idea, but:
My source code:
Sometimes it's everything ok, but eventually - panic (see controllers section)...
As I understand, inject didn't instantiate controller instances...
So, any suggestions or recommendations? Thanks! Alex