title: Castle Windsor and how to avoid service locator pattern?
body: <p>I'm using Castle Windsor, and in most cases I'm using DI via class constructors. However there are times where I find myself using a service locator to resolve an instance of a type, which I know is an anti-pattern. I believe you must also release transient objects resolved in this manner, as Windsor won't do this for you?</p>
  
  <p>An example scenario would be a class that simulates a TV remote. The UI has dozens of buttons, and clicking one results in the class instantiating and executing a particular "command" object. Injecting all these concrete commands via the constructor clearly isn't feasible, so I would use a service locator, something like this:-</p>
  
  <pre><code>private void PowerButtonOnClick()
  {
      var command = ServiceLocator.Current.Resolve&lt;IPowerOnCommand&gt;();
      command.Execute();
  }
  </code></pre>
  
  <p>How would I refactor my code to get rid of the service locator, <em>and</em> ensuring that transient types are released when finished with (if this is indeed required by Windsor)?</p>
  
  <p>(I realise the above scenario could be solved using a "command" design pattern. It was just an example scenario - there are other situations where I'm using a service locator).</p>