title: Dependency injection vs events and resolving cyclic dependencies with events
body: <p>I've been working on refactoring an application to make it more manageable with dependency injection and all that good stuff. When doing this I have encountered cyclic dependencies more than once.</p>
  
  <p>So here is a typical example of cyclic dpendency:</p>
  
  <pre><code>interface IA
  {
      int Data { get; }
  }
  interface IBefore
  {
      void DoStuffBefore();
  }
  class A: IA
  {
      public int Data { get; private set; }
      IBefore before;
      public A(IBefore before)
      {
          this.before = before;
      }
      public void Increment()
      {
          before.DoStuffBefore();
          Data++;
      }
  }
  class B: IBefore
  {
      IA a;
      public B(IA a)
      {
          this.a = a;
      }
      public void WriteADataToConsole()
      {
          Console.Write(a.Data);
      }
      public void DoStuffBefore() //From IBefore
      {
          WriteADataToConsole();
      }
  }
  </code></pre>
  
  <p>I cannot create neither of the classes since they require eachother. Now the standard(?) thing to do in this case would be to separate A's data from A:</p>
  
  <pre><code>public interface IA
  {
      int Data { get; set; }
  }
  public interface IBefore
  {
      void DoStuffBefore();
  }
  class AData : IA
  {
      public int Data { get; set; }
  }
  class A
  {
      public IA Data { get; private set; }
      IBefore before;
      public A(IA data, IBefore before)
      {
          this.Data = data;
          this.before = before;
      }
      public void Increment()
      {
          before.DoStuffBefore();
          Data.Data++;
      }
  }
  class B : IBefore
  {
      IA a;
      public B(IA a)
      {
          this.a = a;
      }
      public void WriteADataToConsole()
      {
          Console.Write(a.Data);
      }
      public void DoStuffBefore() //From IBefore
      {
          WriteADataToConsole();
      }
  }
  </code></pre>
  
  <p>The above solves the circular dpendency because I can now create AData first and then inject it into B and inject B into A. But I could also put an event i IA that B can listen to:</p>
  
  <pre><code>public interface IA
  {
      int Data { get; }
      event Action BeforeEvent;
  }
  
  class A: IA
  {
      public int Data { get; private set; }
      public event Action BeforeEvent;
      public void Increment()
      {
          BeforeEvent();
          Data++;
      }
  }
  
  class B
  {
      IA a;
      public B(IA a)
      {
          this.a = a;
          a.BeforeEvent += new Action(WriteADataToConsole);
      }
      void WriteADataToConsole() //Event listener
      {
          Console.Write(a.Data);
      }
  }
  </code></pre>
  
  <p>This is sonething I stumbled upon because I was trying to convert the event approach to dependency injection and realised that by doing so I had gotten myself a circular dependency.</p>
  
  <p>Some of the questions that's been troubleing my brain are:</p>
  
  <ul>
  <li>Both solutions solve the circular dependency(right?) and as far as I can see they open up for extension of A to an equal degree, but which would be considered the best design?</li>
  <li>What would be some guidelines for when to use events and when to use DI to solve circular dependencies and in general? </li>
  <li>Obviously events are not good if A would need a return value from B. Does that mean that events are always preferrable when void is returned?</li>
  <li>What are the pros and cons of each solution?</li>
  </ul>