【ASP.NET MVC 學習筆記】- 04 依賴注入(DI)

來源:互聯網
上載者:User

標籤:mil   mvc   ges   depend   ace   public   category   color   ret   

本文參考:http://www.cnblogs.com/willick/p/3223042.html

1、在一個類內部,不通過建立對象的執行個體而能夠獲得某個實現了公開介面的對象的引用。這種"需要",就稱為依賴注入(Dependency Injection)。

2、依賴注入和和所謂的控制反轉(Inversion of Control )是一個意思。

3、依賴注入是通過介面實現松耦合的設計模式,是解耦的重要手段。

4、依賴注入分為兩步:移除對組件的依賴;通過類的構造器(或setter訪問器)來傳遞實現了公開介面的組件的引用。

5、樣本:

  • 產品類定義
    public class Product    {        public string Name { get; set; }        public string Category { get; set; }        public decimal Price { get; set; }    }

 

  • 公開介面定義
public interface IValueCalculator {    decimal ValueProducts(params Product[] products);}

 

  • 公開介面實作類別
    public class LinqValueCalculator : IValueCalculator    {        public decimal ValueProducts(params Product[] products)        {            return products.Sum(p => p.Price);        }    }

 

  • 使用介面的類(不依賴注入)
   public class ShoppingCart    {        //計算購物車內商品總價錢        public decimal CalculateStockValue()        {             Product[] products =             {                new Product {Name = "西瓜", Category = "水果", Price = 2.3M},                new Product {Name = "蘋果", Category = "水果", Price = 4.9M},                new Product {Name = "空心菜", Category = "蔬菜", Price = 2.2M},                new Product {Name = "地瓜", Category = "蔬菜", Price = 1.9M}             };            IValueCalculator calculator = new LinqValueCalculator();            //計算商品總價錢             decimal totalValue = calculator.ValueProducts(products);            return totalValue;                    }    }

 

  • 使用介面的類(依賴注入)
    public class ShoppingCart    {        IValueCalculator calculator;        //建構函式,參數為實現了IValueCalculator介面的類的執行個體        public ShoppingCart(IValueCalculator calcParam)        {            calculator = calcParam;        }        //計算購物車內商品總價錢        public decimal CalculateStockValue()        {            Product[] products = 
{ new Product {Name = "西瓜", Category = "水果", Price = 2.3M}, new Product {Name = "蘋果", Category = "水果", Price = 4.9M}, new Product {Name = "空心菜", Category = "蔬菜", Price = 2.2M}, new Product {Name = "地瓜", Category = "蔬菜", Price = 1.9M} }; //計算商品總價錢 decimal totalValue = calculator.ValueProducts(products); return totalValue; } }

 

    這樣就徹底斷開了ShoppingCart和LinqValueCalculator之間的依賴關係,使用實現了IValueCalculator介面的類(樣本中的LinqValueCalculator)的執行個體引用作為參數,傳遞給ShoppingCart類的建構函式。ShoppingCart類不知道也不關心這個實現了IValueCalculator介面的類是什麼,更沒有責任去操作這個類。

   ShoppingCart、LinqValueCalculator和IValueCalculator之間的關係:

  

 

【ASP.NET MVC 學習筆記】- 04 依賴注入(DI)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.