title: Difference between Dependency Injection and Repository Pattern
body: <p>I am trying to understand Difference between Dependency Injection and Repository Pattern.</p>
  
  <p>According to my understanding Repository Pattern does everything which Dependency Injection is doing, except in Dependency Injection we are using Constructor Injection.</p>
  
  <p>Ok, Let me try to explain giving an example:  (Please note I am using Unity Framework for DI)</p>
  
  <p><strong>Create Product Class</strong></p>
  
  <pre><code>public class Product
  {
   public int Id { get; set; }
   public string Name { get; set; }
   public string Category { get; set; }
   public decimal Price { get; set; }
  }
  </code></pre>
  
  <p><strong>Create Interface on Model</strong></p>
  
  <pre><code>public interface IProductRepository
  {
   IEnumerable&lt;Product&gt; GetAll();
   Product Get(int id);
   Product Add(Product item);
   bool Update(Product item);
   bool Delete(int id);
  }
  </code></pre>
  
  <p><strong>Implement Interface</strong></p>
  
  <pre><code>//ProductRepository.cs
  public class ProductRepository : IProductRepository
  {
   private List&lt;Product&gt; products = new List&lt;Product&gt;();
   private int _nextId = 1;
  
   public ProductRepository()
   {
   // Add products for the Demonstration
   Add(new Product { Name = "Computer", Category = "Electronics", Price = 23.54M });
   Add(new Product { Name = "Laptop", Category = "Electronics", Price = 33.75M });
   Add(new Product { Name = "iPhone4", Category = "Phone", Price = 16.99M });
   }
  
   public IEnumerable GetAll()
   {
   // TO DO : Code to get the list of all the records in database
   return products;
   }
   public Product Get(int id)
   {
   // TO DO : Code to find a record in database
   return products.Find(p =&gt; p.Id == id);
   }
   public Product Add(Product item)
   {
   if (item == null)
   {
   throw new ArgumentNullException("item");
   }
  
   // TO DO : Code to save record into database
   item.Id = _nextId++;
   products.Add(item);
   return item;
   }
   public bool Update(Product item)
   {
   if (item == null)
   {
   throw new ArgumentNullException("item");
   }
  
   // TO DO : Code to update record into database
   int index = products.FindIndex(p =&gt; p.Id == item.Id);
   if (index == -1)
   {
   return false;
   }
   products.RemoveAt(index);
   products.Add(item);
   return true;
   }
   public bool Delete(int id)
   {
   // TO DO : Code to remove the records from database
   products.RemoveAll(p =&gt; p.Id == id);
   return true;
   }
  }
  </code></pre>
  
  <p><strong>For DI Initialize Dependency in Bootstrap.cs</strong></p>
  
  <pre><code>private static IUnityContainer BuildUnityContainer()
  {
   var container = new UnityContainer();
  
   // register all your components with the container here
   // it is NOT necessary to register your controllers
  
   container.RegisterType&lt;IProductRepository, ProductRepository&gt;();
  
   // e.g. container.RegisterType&lt;ITestService, TestService&gt;(); 
  
   RegisterTypes(container);
  
   return container;
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public class ProductController : Controller
  {
   readonly IProductRepository repository;
  
   //inject dependency
   public ProductController(IProductRepository repository)
   {
   this.repository = repository; 
   }
  
   public ActionResult Index()
   {
   var data = repository.GetAll();
   return View(data);
   }
   //Other Code
  }
  </code></pre>
  
  <p><strong>My Questions</strong></p>
  
  <ul>
  <li>What is difference between Repository Pattern and DI</li>
  <li>What is advantage of Constructory Injection</li>
  <li>Appreciate If someone can explain Bootstrap.cs file code and Controller class code with maximum possible details.</li>
  </ul>
  
  <p>Source of code: <a href="http://www.dotnet-tricks.com/Tutorial/dependencyinjection/632V140413-Dependency-Injection-in-ASP.NET-MVC-4-using-Unity-IoC-Container.html" rel="noreferrer">http://www.dotnet-tricks.com/Tutorial/dependencyinjection/632V140413-Dependency-Injection-in-ASP.NET-MVC-4-using-Unity-IoC-Container.html</a></p>