title: IRepository and Related Data?
body: <p>a couple of weeks ago i just got a test for a job interview, the thing is that i've resolved almost everything except one point, that at this point, i guess it's impossible to resolve. but for sure i'm wrong.</p>
  
  <p>This problem deals with related data.  Our server API, IRepository, will only return a node with its immediate children (for example imagine that FakeRepository is loading from a Database.  You must not change the code in FakeRepository for this test).</p>
  
  <p>Imagine that the NodeManager is client side code. </p>
  
  <blockquote>
    <p>We want to make sure that there is only ever one instance of a specific object on the client, so that  our client has a consistent view of data in our application. The test “LoadingNodeB_ShouldAlwaysReturnTheSameInstance()” demonstrates this problem.  Modify the code in the NodeManager so that this test passes.</p>
  </blockquote>
  
  <p>so, we have this IRepo</p>
  
  <pre><code>namespace TestRecruitment.Repository
  {
      public interface IRepository
      {
          Node GetNodeAndImmediateChildren(string nodeName);
      }
  }  
  </code></pre>
  
  <p>and in a test project we have a fakerepository that loads the information.</p>
  
  <pre><code>using System;
  
  using TestRecruitment.Repository;
  
  namespace TestRecruitment.Test.Repository
  {
      public class FakeRepository : IRepository
      {
          /// &lt;summary&gt;
          /// YOU MUST NOT CHANGE THIS METHOD
          /// &lt;/summary&gt;
          public Node GetNodeAndImmediateChildren(string nodeName)
          {
              switch (nodeName)
              {
                  case "Node A":
                      // do not modify this
                      var nodeB = new Node { Name = "Node B" };
                      var nodeC = new Node { Name = "Node C" };
                      return new Node { Name = "Node A", ImmediateChildren = new[] { nodeB, nodeC } };
                  case "Node B":
                      // do not modify this
                      var nodeD = new Node { Name = "Node D" };
                      var nodeE = new Node { Name = "Node E" };
                      return new Node { Name = "Node B", ImmediateChildren = new[] { nodeD, nodeE } };
                  case "Node E":
                      var nodeA = new Node { Name = "Node A" };
                      var nodeF = new Node { Name = "Node F" };
                      return new Node { Name = "Node E", ImmediateChildren = new[] { nodeA, nodeF } };
                  default:
                      throw new ApplicationException("Unknown node");
              }
          }
      }
  }
  </code></pre>
  
  <p>the thing is that you only can change the repository, you can't touch any other code.</p>
  
  <pre><code>using System.Linq;
  
  namespace TestRecruitment.Repository
  {
      public class NodeManager
      {
          private readonly IRepository _nodeRepository;
  
          public NodeManager(IRepository nodeRepository)
          {
              _nodeRepository = nodeRepository;
          }
  
          public Node GetNodeAndImmediateChildren(string name)
          {
              return _nodeRepository.GetNodeAndImmediateChildren(name);
          }
      }
  }
  </code></pre>
  
  <p>well, that's it.</p>
  
  <p>any help will be appreciated, i just want to know, how to resolve it.</p>
  
  <p>i've created a github repo <a href="https://github.com/rothariger/TestRecruitment" rel="nofollow noreferrer">https://github.com/rothariger/TestRecruitment</a></p>
  
  <p>Regards!</p>