I’m working on bettering my strategy of working with classes and objects.
What is the best way of passing an object down a through a chain of specific classes to keep the code organized.
example: working with a ZedGraph object (note) this may not be the best example but it will get the idea across.
class Graphhandler
{
private ZedGraphControl ZGC;
private SubGraphController PortionofGraph;
public class GraphHandler(ZedGraphControl _ZGC)
{
ZGC = _ZGC;
initializeGraph();
}
private void initializeGraph()
{
// notice I am putting the ZGC Object into another class
// and likely that ZGC object will go into another class
PortionofGraph = new SubGraphController(ZGC);
}
}
class SubGraphController
{
private ZedGraphControl ZGC;
private DeeperSubGraphController PortionofGraph;
public class SubGraphController(ZedGraphControl _ZGC)
{
ZGC = _ZGC;
initializeSubGraph();
}
private void initializeSubGraph()
{
PortionofGraph = new DeeperSubGraphController(ZGC);
// is there a better way?
}
}
Is there a better way of passing a yop level object down through all these calls to manipulate the data?
Normally, the answer is to pass fully-formed dependencies into your objects. For example:
Rather than constructing the
DeeperSubGraphControllerdirectly in you subgraph controller. Nowadays, you usually orchestrate all this using a dependency injection framework.(See also:
Dependency Injection Myth: Reference Passing)