When DI goes too deep - what is another architecture/design pattern you should use? [duplicate]
I like using dependency injection but sometimes I find that I have to nest some dependencies so deep that I would almost rather use a global variable (despite this being bad practice). Here is an example, for the context of this example, consider that I am making a game. // in Main Renderer rend = new Renderer(); // a rendering Object, contains calls like rend.DrawCircle(); GameObject go = new GameObject(rend); ShapeDrawerer shape = new ShapeDrawer(rend); go.AddComponent(shape); // in ShapeDrawerer Circle circle = new Circle(this.rend); // in Circle Text text = new Text(this.rend); As you can see there are 2 main annoyances: lots of stuff rely on Renderer. You must nest Renderer very deep in the hierarchy Am I wrong to use DI here? Or maybe there is something about my architecture itself independent of DI. Should I use a different design pattern when this manifests? Regardless I know that there is some weirdness going on here
I like using dependency injection but sometimes I find that I have to nest some dependencies so deep that I would almost rather use a global variable (despite this being bad practice). Here is an example, for the context of this example, consider that I am making a game.
// in Main
Renderer rend = new Renderer(); // a rendering Object, contains calls like rend.DrawCircle();
GameObject go = new GameObject(rend);
ShapeDrawerer shape = new ShapeDrawer(rend);
go.AddComponent(shape);
// in ShapeDrawerer
Circle circle = new Circle(this.rend);
// in Circle
Text text = new Text(this.rend);
As you can see there are 2 main annoyances:
- lots of stuff rely on Renderer.
- You must nest Renderer very deep in the hierarchy
Am I wrong to use DI here? Or maybe there is something about my architecture itself independent of DI. Should I use a different design pattern when this manifests? Regardless I know that there is some weirdness going on here