title: ASP.Net WebApi: How to inject repository by Unity DI
body: <p>please discuss with code sample that how to inject repository dynamically in controller by Unity DI</p>
  
  <p>now I am doing things like this way without Unity DI.</p>
  
  <pre><code>public class CustomerController : ApiController
  {
      static readonly ICustomerRepository repository = new CustomerRepository();
  
      public IEnumerable&lt;Customer&gt; GetAllCustomers()
      {
          return repository.GetAll();
      }
  
      public Customer GetCustomer(string customerID)
      {
          Customer customer = repository.Get(customerID);
          if (customer == null)
          {
              throw new HttpResponseException(HttpStatusCode.NotFound);
          }
          return customer;
      }
  
      public IEnumerable&lt;Customer&gt; GetCustomersByCountry(string country)
      {
          return repository.GetAll().Where(
              c =&gt; string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
      }
  
      public HttpResponseMessage PostCustomer(Customer customer)
      {
          customer = repository.Add(customer);
          var response = Request.CreateResponse&lt;Customer&gt;(HttpStatusCode.Created, customer);
  
          string uri = Url.Link("DefaultApi", new { customerID = customer.CustomerID });
          response.Headers.Location = new Uri(uri);
          return response;
      }
  
      public void PutProduct(string customerID, Customer customer)
      {
          customer.CustomerID = customerID;
          if (!repository.Update(customer))
          {
              throw new HttpResponseException(HttpStatusCode.NotFound);
          }
      }
  
      public void DeleteProduct(string customerID)
      {
          Customer customer = repository.Get(customerID);
          if (customer == null)
          {
              throw new HttpResponseException(HttpStatusCode.NotFound);
          }
          repository.Remove(customerID);
      }
  }
  </code></pre>
  
  <blockquote>
    <p>static readonly ICustomerRepository repository = new
    CustomerRepository();</p>
  </blockquote>
  
  <p>here my repository is hard code. how can i inject it by unity.</p>
  
  <p>please show me the way. thanks</p>