title: How to change the dependency at runtime using simple injector
body: <p>I am new to simple injector. I have data access layer that has dependency on Force Client. I have register the <code>ForceClient</code> dependency. I want to replace the default value of <code>ForceClient</code> once user login into the application.</p>
  
  <p>Please let me know, how i can change the default values at run time.</p>
  
  <pre><code>Ioc.ServiceContainer.Register(() =&gt; new ForceClient(
      "test",
      "test",
      "test"));
  </code></pre>
  
  <p>Here is the complete detail about the requirement. I have DAL in our Xamarin project that retrieve data from sales force using Developerforce.Force apis. I am writing unit test cases using MOQ to test the DAL. </p>
  
  <p>DAL Code.</p>
  
  <pre><code>public CustomerRepository(IForceClient client)
  {
      _client = client;                       
  }
  
  public async Task&lt;long&gt; GetTotalContacts()
  {
      string totalContactCountQry = "some query"
  
      var customerCount =  await _client.QueryAsync&lt;ContactsTotal&gt;(totalContactCountQry);
      var firstOrDefault = customerCount.Records.FirstOrDefault();
  
      return firstOrDefault != null ? firstOrDefault.Total : 0;
  }
  </code></pre>
  
  <p>Unit Test Case Code.</p>
  
  <pre><code>[SetUp]
  public void Init()
  {
      forceClientMock = new Mock&lt;IForceClient&gt;();
      forceClientMock.Setup(x =&gt; x.ForceClient(It.IsAny&lt;string&gt;(), 
          It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;(), It.IsAny&lt;HttpClient&gt;()))
          .Return(new ForceClient(It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;(), 
              It.IsAny&lt;string&gt;(), It.IsAny&lt;HttpClient&gt;()));              
      forceClientMock.Setup(x =&gt; x.QueryAsync&lt;ContactsTotal&gt;(It.IsAny&lt;string&gt;()))
          .ReturnsAsync(new QueryResult&lt;ContactsTotal&gt;());
      forceClientMock.Setup(x =&gt; x.QueryAsync&lt;ContactsTotal&gt;(It.IsAny&lt;string&gt;()))
          .ReturnsAsync(new QueryResult&lt;ContactsTotal&gt;() { Records=new List&lt;ContactsTotal&gt;() });            
  }
  
  [Test]
  public void GetTotalContacts()
  {              
      ICustomerRepository customerRepostory = new CustomerRepository(forceClientMock.Object);
      Assert.AreEqual(customerRepostory.GetTotalContacts().Result,0);
  }
  </code></pre>
  
  <p>Simple Injector Registry on application initialization</p>
  
  <pre><code>container.Register&lt;IForceClient&gt;((() =&gt; new ForceClient(
      UserState.Current.ApiBaseUrl,
      UserState.Current.AuthToken.AccessToken,
      UserState.Current.ApiVersion)), Lifestyle.Transient);
  </code></pre>
  
  <p>The instance of ForceClient that i am creating during the registry is being created with all default valued of UserState. The actual value gets assigned once user login into the application. </p>
  
  <p>I except ForceClient instance to have the updated value after login to access the sales force to retrieve the data but the program is giving error on below line DAL</p>
  
  <pre><code>var customerCount =  await _client.QueryAsync&lt;ContactsTotal&gt;(totalContactCountQry);
  </code></pre>
  
  <p>The reason is that the forceClient still contain default values. How can i make sure that the FoceClient instance get created after login to use the actual value of UserState</p>