title: Dependency Injection in C# Web API
body: <p>I am trying to create a rest service in C# Web API.</p>
  
  <p>At the moment I'm not considering any DB decisions, hence I'm adding a mock class library.</p>
  
  <p>I'm creating a model interface and implementing the model in the mock class library.</p>
  
  <pre><code>public interface IUser
  {
      int userId { get; set; }
      string firstName { get; set; }
      string lastName { get; set; }
      string email { get; set; }
  
      List&lt;IUser&gt; getAllUsers();
      IUser getUser(int ID);
      bool updateUser(IUser user);
      bool deleteUser(int ID);
  }
  </code></pre>
  
  <p>and implementing this in the mock class library</p>
  
  <pre><code>public class User : IUser
  {
      public string email { get; set; }
      public string firstName { get; set; }
      public string lastName { get; set; }
      public int userId { get; set; }
  
      public bool deleteUser(int ID)
      {
          throw new NotImplementedException();
      }
  
      public List&lt;IUser&gt; getAllUsers()
      {
          throw new NotImplementedException();
      }
  
      public IUser getUser(int ID)
      {
          throw new NotImplementedException();
      }
  
      public bool updateUser(IUser user)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>Now the mock library references the service application to implement the interface.</p>
  
  <p>Now I need to call the mock implementation from the controller class in the service application.</p>
  
  <p>How do I do that without creating a cyclic dependency. I did some research and came up with a solution that DI is the way to go.</p>
  
  <p>Can someone help me to implement this with code samples?</p>
  
  <p>Many thanks.</p>