Asp.net
依賴注入和儲存庫模式之間的區別
我試圖了解依賴注入和儲存庫模式之間的區別。
根據我的理解,儲存庫模式完成了依賴注入所做的一切,除了在依賴注入中我們使用的是建構子注入。
好的,讓我試著舉個例子來解釋一下:(請注意我正在使用 Unity Framework for DI)
創建產品類
public class Product { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public decimal Price { get; set; } }在模型上創建介面
public interface IProductRepository { IEnumerable<Product> GetAll(); Product Get(int id); Product Add(Product item); bool Update(Product item); bool Delete(int id); }實現介面
//ProductRepository.cs public class ProductRepository : IProductRepository { private List<Product> products = new List<Product>(); 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 => 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 => 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 => p.Id == id); return true; } }Bootstrap.cs 中的 DI 初始化依賴
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<IProductRepository, ProductRepository>(); // e.g. container.RegisterType<ITestService, TestService>(); RegisterTypes(container); return container; }控制器
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 }我的問題
- 儲存庫模式和 DI 有什麼區別
- 構造注入的優點是什麼
- 感謝是否有人可以盡可能詳細地解釋 Bootstrap.cs 文件程式碼和控制器類程式碼。
它們確實無法比較,儲存庫是您可以通過依賴注入註入的東西。DI 的目的是使您的應用程序鬆散耦合。您可以指定一個介面來定義一個實現必須履行的契約,而不是指定一個具體的實現。這樣,您可以更輕鬆地換出實現。
Martin Fowler 定義的儲存庫模式將您的域與關心如何實現儲存隔離開來,因此所有檢索到的對像都可以被視為記憶體中的集合。您可以擁有一個基於數據庫、XML 文件、文本文件或任何東西的儲存庫。應用程式碼本身並不關心。這使得它對於測試非常有用,因此連接到 TDD。
您將(注入)依賴項傳遞給控制器。這些可以是儲存庫、服務或您的控制器需要的任何東西。您的 IoC 容器在執行時將所有這些連接在一起。這本身就非常強大,我們在 SaaS 應用程序中大量使用 DI,在該應用程序中,客戶擁有自己的實現,這些實現取決於客戶端有條件地註入。
我建議您閱讀.NET 中的依賴注入。Mark Seemann 可以比我更好地解釋這一點,並且很好地介紹了您應該如何以及為什麼應該使用 DI 和各種IoC 容器,例如 Unity