title: Is there a good/proper way of solving the dependency injection loop problem in the ASP.NET MVC ContactsManager tutorial?
body: <p>If you don't know what I'm talking about either go through <a href="http://www.asp.net/learn/mvc/tutorial-30-cs.aspx" rel="noreferrer">the tutorial</a> and try to add dependency Injection yourself or try your luck with my explanation of the problem.</p>
  
  <p><strong>Note:</strong> This problem isn't within the scope of the original tutorial on ASP.NET. The tutorial only suggests that the patterns used are dependency injection friendly.</p>
  
  <p>The problem is basically that there is a dependency loop between the Controller, the ModelStateWrapper and the ContactManagerService.</p>
  
  <ol>
  <li>The ContactController constuctor takes an IContactManagerService.</li>
  <li>The ContactManagerService constructor takes an IContactManagerRepository <em>(not important)</em> and an IValidationDictionary <em>(which ModelStateWrapper implements)</em>.</li>
  <li>The ModelStateWrapper constructor takes a ModelStateDictionary <em>(which is a property called "ModelState" on the controller)</em>.</li>
  </ol>
  
  <p>So the dependency cycle goes like this: Controller > Service > ModelStateWrapper > Controller</p>
  
  <p>If you try to add dependency injection to this, it will fail. So my question is; what should I do about it? Others have posted this question, but the answers are few, different, and all seem kinda "hack-ish".</p>
  
  <p>My current solution is to remove the IModelStateWrapper from the IService constructor and add an Initialize method instead like this:</p>
  
  <pre><code>public class ContactController : Controller
  {
      private readonly IContactService _contactService;
  
      public ContactController(IContactService contactService)
      {
          _contactService = contactService;
          contactService.Initialize(new ModelStateWrapper(ModelState));
      }
  
      //Class implementation...
  }
  
  public class ContactService : IContactService
  {
      private IValidationDictionary _validationDictionary;
      private readonly IContactRepository _contactRepository;
  
      public ContactService(IContactRepository contactRepository)
      {
          _contactRepository = contactRepository;
      }
  
      private void Initialize(IValidationDictionary validationDictionary)
      {
          if(validationDictionary == null)
              throw new ArgumentNullException("validationDictionary");
  
          _validationDictionary = validationDictionary;
      }
  
      //Class implementation...
  }
  
  public class ModelStateWrapper : IValidationDictionary
  {
      private readonly ModelStateDictionary _modelState;
  
      public ModelStateWrapper(ModelStateDictionary modelState)
      {
          _modelState = modelState;
      }
  
      //Class implementation...
  }
  </code></pre>
  
  <p>With this construct I can configure my unity container like this:</p>
  
  <pre><code>public static void ConfigureUnityContainer()
  {
      IUnityContainer container = new UnityContainer();
  
      // Registrations
      container.RegisterTypeInHttpRequestLifetime&lt;IContactRepository, EntityContactRepository&gt;();
      container.RegisterTypeInHttpRequestLifetime&lt;IContactService, ContactService&gt;();
  
      ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory(container));
  }
  </code></pre>
  
  <p>Unfortunately this means that the "Initialize" method on the service has to be called <em>manually</em> by the controller constructor. Is there a better way? Maybe where I somehow include the IValidationDictionary in my unity configuration? Should I switch to another DI container? Am I missing something?</p>