title: Strategy pattern creates a dependency with Controller class in Asp.net MVC 3.0
body: <p>I am working with a CRM product that uses ASP.net MVC 3.0, Entity Framework and Windsor for IOC container. </p>
  
  <p>I have injected the services which are work with repository layer to controller through Windsor.</p>
  
  <p>However, I have implemented the strategy pattern for my <strong><code>ContactController</code></strong> to facilitates                                               <strong><code>MyProfile</code></strong>, <strong><code>CustomerProfile</code></strong>, <strong><code>CompanyProfile</code></strong>.      </p>
  
  <p>Now my <strong><code>ContactContext</code></strong> class is depended on my <strong><code>ContactController</code></strong> class so that i can't inject <strong><code>ContactContext</code></strong> using Windsor as a result of that i have created instance of <strong><code>ContactContext</code></strong> in <strong><code>ContactController</code></strong>.</p>
  
  <p><strong><code>ContactController</code></strong> class implementation</p>
  
  <pre><code> public class ContactController:BaseController
   {
      private ContactContext _contactContext;
  
      public ContactController(PersonService personService, CompanyService customerService)
      {
          _contactContext = new ContactContext(personService, customerService, this);
      }
  
      public ActionResult ContactProfile(string profileId, string profileType)
      {
         base.ValidateContactProfileIdAndProfileTypeInfo(profileType, profileId);
         return _contactContext.RenderContactProfile(ProfileType, ProfileId);
      }
    }
  </code></pre>
  
  <p><strong><code>ContactContext</code></strong> implementation</p>
  
  <pre><code>public class ContactContext
  {
      private Dictionary&lt;ProfileType, IContactStrategy&gt; _strategies =
      new Dictionary&lt;ProfileType, IContactStrategy&gt;();
  
      private BaseController _controller;
  
      public ContactContext(PersonService personService, CompanyService companyService, BaseController controller)
      {
          _strategies.Add(ProfileType.MyProfile, new MyProfileStrategy(personService));
          _strategies.Add(ProfileType.CustomerProfile, new PersonStrategy(personService));
          _strategies.Add(ProfileType.CompanyProfile, new CompanyStrategy(companyService));
  
          _controller = controller;
      }
  
      public ActionResult RenderProfileInfo(ProfileType profileType, long profileId)
      {
          return _strategies[profileType].GenerateProfileInfoView(profileId, _controller);
      }
  
      public ActionResult RenderPeopleInfo(ProfileType profileType, long profileId)
      {
          return _strategies[profileType].GeneratePeopleInfoView(profileId, _controller);
      }
   }
  </code></pre>
  
  <p>Strategies go like this</p>
  
  <pre><code>public class PersonStrategy:IContactStrategy
  {
      private PersonService _personService;
  
      public PersonStrategy(PersonService personService)
      {
          _personService = personService;
      }
  
  
      #region Implementation of IContactStrategy
  
      public ActionResult GenerateProfileInfoView(long profileId, BaseController controller)
      {
          //TODO: Load Profile info from service
          PersonDetailsViewModel personDetailsViewModel = new PersonDetailsViewModel();
          personDetailsViewModel.Name = "Robert Martin";
  
          return controller.RenderPartialView("ProfileInfo", personDetailsViewModel);
      }
  
      public ActionResult GeneratePeopleInfoView(long profileId, BaseController controller)
      {
          //TODO: Load people from service
          return controller.RenderPartialView("PeopleView", new List&lt;PersonLiteViewModel&gt;());
      }
    }
  
  public class CompanyStrategy : IContactStrategy
  {
      private CompanyService _companyService;
  
      public CompanyStrategy(CompanyService companyService)
      {
          _companyService = companyService;
      }
  
      #region Implementation of IContactStrategy
  
      public ActionResult GenerateView(long profileId, BaseController controller)
      {
          throw new NotImplementedException();
      }
  
      public ActionResult GenerateProfileInfoView(long profileId, BaseController controller)
      {
          throw new NotImplementedException();
      }
   }
  </code></pre>
  
  <p><strong>Question:</strong> How can i get rid of <strong><code>ContactContext</code></strong> dependency with <strong><code>ContactController</code></strong>?</p>