title: How Can I Improve this Translator Object Factory to simplify unit testing?
body: <p>In a project of mine, I have a number of classes based upon an ITranslator interface as follows:</p>
  
  <pre><code>interface ITranslator&lt;TSource, TDest&gt;
  {
      TDest Translate(TSource toTranslate);
  }
  </code></pre>
  
  <p>These classes translate a data object into a new form. To obtain an instance of a translator, I have an ITranslatorFactory with a method <code>ITranslator&lt;TSource, TDest&gt; GetTranslator&lt;TSource, TDest&gt;()</code>. I could not think of any way to store a collection of functions based upon a wide range of generics (only common ancestor here is <code>Object</code>), so the <code>GetTranslator</code> method is currently just using Unity to resolve an <code>ITranslator&lt;TSource, TDest&gt;</code> that matches the requested translator.</p>
  
  <p>This implementation feels very awkward. I have read that the Service Locator <a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/" rel="nofollow noreferrer">is an anti-pattern</a>, and regardless of if it is or is not, this implementation is making unit testing much more difficult because I must supply a configured Unity container to test any code which depends on the translator.</p>
  
  <p>Unfortunately, I cannot think of a more effective strategy to obtain the proper translator. Does anyone have any suggestions on how I can refactor this setup into a more elegant solution?</p>