title: Simple Injector Conditionally Register With Delegates
body: <p>I'm writing an app that makes use of classes defined by the following interface:</p>
  
  <pre><code>public interface ICredentialProvider
  {
      string GetUsername();
      string GetPassword();
  }
  </code></pre>
  
  <p>This is done so methods can be called to retrieve the username/password instead of it being statically held in memory and defined in code/config file.</p>
  
  <p>Currently in development I'm using this very simple implementation:</p>
  
  <pre><code>public class FakedCredentialProvider : ICredentialProvider
  {
      private readonly string _username;
      private readonly string _password;
  
      public FakedCredentialProvider(string username, string password)
      {
          _username = username;
          _password = password;
      }
  
      public string GetUsername()
      {
          return _username;
      }
  
      public string GetPassword()
      {
          return _password;
      }
  }
  </code></pre>
  
  <p>...which accepts two strings in the constructor. Future (actual) implementations of <code>ICredentialProvider</code> will likely need to accept certain strings for connecting to the appropriate external password vault or service (or possibly both; at this point in the project that's unclear).</p>
  
  <p>Further, the project uses multiple instances of <code>ICredentialProvider</code> for the various accounts it needs to connect to (such as SharePoint, ActiveDirectory, and Windows Graph).</p>
  
  <p>To these ends I know how to register via a delegate:</p>
  
  <pre><code>container.RegisterWebApiRequest(() =&gt;
      new FakedCredentialProvider(fakedSharePointUsername, fakedSharePointPassword));
  </code></pre>
  
  <p>...And how to conditionally register...</p>
  
  <pre><code>container.RegisterConditional(typeof(ICredentialProvider), typeof(FakedCredentialProvider),
          context =&gt;
          context.Consumer.Target.Name.ToLower().Contains("sharepoint")
      );
  </code></pre>
  
  <p>...But not how to combine the two.</p>
  
  <p>How does one combine these two approaches? Or alternatively: Is there an better design for situations such as these?</p>