title: Require Factory class in Extension Method
body: <p>I am working on a large legacy C# application and a task assigned to me is to remove all usages of a static factory class <code>ServiceLocator.GetObject&lt;T&gt;()</code> and replace with constructor injected dependencies throughout. </p>
  
  <p>For the most part, this is simple, however there are around 50 cases in the application codebase where this is a bit tricky. For instance, <code>Servicelocator</code> is used in a static class, or extension method, or even a WPF MarkupExtension!. </p>
  
  <p>For instance, if you were faced with a code snippet like this, what would you do? (apart from cry)</p>
  
  <pre><code>public static class MyExtensions
  {
      private static ISingletonServiceOne _ServiceOne = null;
      private static ISingletonServiceTwo _ServiceTwo = null; // etc ... 
  
      public static SummaryHeader GetBannerSummary(this IModel rfq, object requester)
      {
          Guard.ArgumentNotNull(rfq, "rfq");
          Guard.ArgumentNotNull(requester, "requester");
  
          if (_ServiceOne == null)
          {
              _ServiceOne = ServiceLocator.GetService&lt;ISingletonServiceOne&gt;(requester);
              Guard.ArgumentNotNull(_ServiceOne, "_ServiceOne");
          }
  
          return _ServiceOne.GetBannerSummary(rfq);
      }
  </code></pre>
  
  <p>In the above the ServiceLocator.GetObject() method has been used in an Extension Method on IModel to locate a singleton registered service and execute a method on that using IModel. </p>
  
  <p>The question is: </p>
  
  <ul>
  <li>Are there any patterns/practices to avoid this sort of thing - a DI container needed in a static class, value-converter or extension method</li>
  <li>Are there any patterns/practices to deal with cyclic dependencies in DI?</li>
  <li>What would you do in the above given there is a trade off between good code and time to delivery?</li>
  </ul>
  
  <p>I am thinking to move the GetBannerSummary() method out of extensions and only IModel in this case, however (don't laugh) there are also cases of the same ServiceLocator being used in ValueConverters (WPF) and MarkupExtensions :0</p>
  
  <p>Your comments/suggestions appreciated</p>