Sharing reference from main object to its children

Viewed 51

I have a problem of understanding how is it better to share references between objects.

Sorry for bad example, but I really don't understand how to demonstrate it better.

public class Program
{
   static void Main(string[] args)
   {
      var test1 = new TestProgram1();
      test1.DoSomething();
   }
}

public class TestProgram1
{
   public string Name { get; set; } = "TESTING NAME";

   public void DoSomething()
   {
      var test2 = new TestProgram2();
      test2.DoSomething();
   }
}

public class TestProgram2
{
   public void DoSomething()
   {
      var test3 = new TestProgram3();
      test3.DoSomething();
   }
}

public class TestProgram3
{
   public void DoSomething()
   {
      var test4 = new TestProgram4();
      test4.DoSomething();
   }
}

public class TestProgram4
{
   public void DoSomething()
   {
      var test5 = new TestProgram5();
      test5.DoFinal();
   }
}

public class TestProgram5
{
   public void DoFinal(TestProgram1 testProgram1)
   {
      Console.WriteLine(testProgram1.Name);
   }
}

As you understand, code will newer compile. Because,

public class TestProgram5
{
   public void DoFinal(TestProgram1 testProgram1)
   {
      Console.WriteLine(testProgram1.Name);
   }
}

TestProgram5 needs a reference to TestProgram1

This is my question: is there really no other options then, only to send reference to TestProgram1 through all children objects until TestProgram5 through constructor?

Example:

public class Program
{
   static void Main(string[] args)
   {
      var test1 = new TestProgram1();
      test1.DoSomething();
   }
}

public class TestProgram1
{
   public string Name { get; set; } = "TESTING NAME";

   public void DoSomething()
   {
      var test2 = new TestProgram2(this);
      test2.DoSomething();
   }
}

public class TestProgram2
{
   private TestProgram1 TestProgram1 { get; }

   public TestProgram2(TestProgram1 testProgram1)
   {
      TestProgram1 = testProgram1;
   }

   public void DoSomething()
   {
      var test3 = new TestProgram3(TestProgram1);
      test3.DoSomething();
   }
}

public class TestProgram3
{
   private TestProgram1 TestProgram1 { get; }

   public TestProgram3(TestProgram1 testProgram1)
   {
      TestProgram1 = testProgram1;
   }

   public void DoSomething()
   {
      var test4 = new TestProgram4(TestProgram1);
      test4.DoSomething();
   }
}

public class TestProgram4
{
   private TestProgram1 TestProgram1 { get; }

   public TestProgram4(TestProgram1 testProgram1)
   {
      TestProgram1 = testProgram1;
   }

   public void DoSomething()
   {
      var test5 = new TestProgram5();
      test5.DoFinal(TestProgram1);
   }
}

public class TestProgram5
{
   public void DoFinal(TestProgram1 testProgram1)
   {
      Console.WriteLine(testProgram1.Name);
   }
}

I understand that this is a very and very bad example, but it happens when in the process of codding, you realize that in deep children classes, you need a reference to some first parent.

Maybe there is another, better solution for this?

1 Answers

My solution would be to add the reference with a property. To resolve the circular reference problem. In your example only Program5 has a dependency to Program1. Because it's the only class which really does something with Program1.

class Program
{
    static void Main(string[] args)
    {
        
        var testProgram5 = new TestProgram5();
        var testProgram4 = new TestProgram4(testProgram5);
        var testProgram3 = new TestProgram3(testProgram4);
        var testProgram2 = new TestProgram2(testProgram3);
        var testProgram1 = new TestProgram1(testProgram2);
        testProgram5.TestProgram1 = testProgram1;

        testProgram1.DoSomething();
    }
}

public class TestProgram1
{
    private readonly TestProgram2 _testProgram2;
    public string Name { get; set; } = "TESTING NAME";

    public TestProgram1(TestProgram2 testProgram2)
    {
        _testProgram2 = testProgram2;
    }

    public void DoSomething()
    {
        _testProgram2.DoSomething();
    }
}

public class TestProgram2
{
    private readonly TestProgram3 _testProgram3;


    public TestProgram2(TestProgram3 testProgram3)
    {
        _testProgram3 = testProgram3;
    }

    public void DoSomething()
    {
        _testProgram3.DoSomething();
    }
}

public class TestProgram3
{
    private readonly TestProgram4 _testProgram4;

    public TestProgram3(TestProgram4 testProgram4)
    {
        _testProgram4 = testProgram4;
    }

    public void DoSomething()
    {
        _testProgram4.DoSomething();
    }
}

public class TestProgram4
{
    private readonly TestProgram5 _testProgram5;

    public TestProgram4(TestProgram5 testProgram5)
    {
        _testProgram5 = testProgram5;
    }

    public void DoSomething()
    {

        _testProgram5.DoFinal();
    }
}

public class TestProgram5
{
    public TestProgram1 TestProgram1 { get; set; }

    public TestProgram5()
    {
        
    }

    public void DoFinal()
    {
        Console.WriteLine(TestProgram1.Name);
    }
}
Related