.NET IoC模式依賴反轉(zhuǎn)(DIP)、控制反轉(zhuǎn)(Ioc)、依賴注入(DI)
依賴倒置原則(DIP)
依賴倒置(Dependency Inversion Principle,縮寫DIP)是面向?qū)ο罅蠡驹瓌t之一。他是指一種特定的的解耦形式,使得高層次的模塊不依賴低層次的模塊的實(shí)現(xiàn)細(xì)節(jié),依賴關(guān)系被顛倒(反轉(zhuǎn)),從而使得低層次模塊依賴于高層次模塊的需求抽象.
該原則規(guī)定:
- 高層次的模塊不應(yīng)該依賴低層次模塊,二者都應(yīng)該依賴其抽象接口.
- 抽象接口不應(yīng)該依賴于具體實(shí)現(xiàn),而具體實(shí)現(xiàn)則應(yīng)該依賴于抽象接口.
通過如下一個(gè)簡(jiǎn)單的示例,我們來看一下,我們通過一個(gè)簡(jiǎn)單地下單流程向我們的用戶發(fā)送相關(guān)的短信或者郵件.
public SendingEmail { public void Send(string message){ //do something } } public Ordering { SendingEmail _sendingEmail=null; public void Order(string message){ //Order business operation if(_sendingEmail == null) { _sendingEmail=new SendingEmail(); } _sendingEmail.Send(message); } }
這樣看我們的代碼沒問題,目前只要我們完成了訂單操作那么,那么則會(huì)觸發(fā)發(fā)送功能,但是他卻違反了DIP,因?yàn)镺rdering類依賴于SendingEmail類,而SendingEmail類不是抽象類,而是一個(gè)具體的類.那我們?cè)賮硐胍粋€(gè)如果這時(shí)候業(yè)務(wù)口的人過來向我們提出了一個(gè)新的需求,要求我們改為短信而不是Email,那么我們需要怎么改?
public class SendingSMS { public void Send(string message){ //do something } } public Ordering { SendingEmail _sendingEmail=null; SendingSMS _sendingSMS=null; bool isSendingSMS=true; public void Order(string message){ //Order business operation if(isSendingSMS){ if(_sendingSMS == null) { _sendingSMS=new SendingSMS(); } _sendingSMS.Send(message); }else{ if(_sendingEmail == null) { _sendingEmail=new SendingEmail(); } _sendingEmail.Send(message); } } }
根據(jù)上述需求我們不得不創(chuàng)建更多的類,并且在Ordering類中聲明他,最后我們還需要使用IF ELSE語句來決定使用SMS還是使用電子郵件.但是當(dāng)我們有更多這種處理操作后,那么可能比現(xiàn)在還混亂,這就意味著我們必須在Ordering類中聲明更多新的具體類的實(shí)例.
我們需要抽離出來一種方式,讓高級(jí)模塊去依賴于抽象,用它來代替我們實(shí)現(xiàn)類,該抽象將映射到實(shí)現(xiàn)類.
控制反轉(zhuǎn)(IoC)
控制反轉(zhuǎn)(Inversion of Control,縮寫為IOC)是面向?qū)ο笾械脑O(shè)計(jì)原則,他可以幫助我們使高層模塊依賴于抽象,而不是底層模塊的具體實(shí)現(xiàn).換句話說,他有助于實(shí)現(xiàn)(依賴倒置原則——DIP).
public interface ICustomerCommunication { void Send(string message); } 然后我們修改SendingEmail和SendingSMS類以從ICustomerCommunication接口繼承. public class SendingEmail:ICustomerCommunication { public void Send(string message){ //do something } } public class SendingSMS:ICustomerCommunication { public void Send(string message){ //do something } }
我們?cè)賮硇薷囊幌翺rdering類以使用該抽象接口
public Ordering { ICustomerCommunication _customerComm=null; bool isSendingSMS=true; public void Order(string message){ //Order business operation if(isSendingSMS){ if(_customerComm == null) { _customerComm=new SendingSMS(); } _customerComm.Send(message); }else{ if(_customerComm == null) { _customerComm=new SendingEmail(); } _customerComm.Send(message); } } }
通過如上修改我們做的控制反轉(zhuǎn)更符合DIP.現(xiàn)在我們的高級(jí)模塊只需要依賴于抽象,而不用去依賴實(shí)現(xiàn).
依賴注入(DI)
依賴注入(Depeondency Injection,縮寫為DI)是實(shí)現(xiàn)控制反轉(zhuǎn)的一種方式.常用的依賴注入方法有3種:
- 構(gòu)造函數(shù)注入
- 方法注入
- 屬性注入
雖然說通過上面代碼我們實(shí)現(xiàn)了IoC,并且Ordering類依賴于ICustomerCommunication抽象,但我們?nèi)匀辉贠rdering類中使用了實(shí)現(xiàn)類,這使用我們無法在類于類之間完全解耦.
if(isSendingSMS){ if(_customerComm == null) { _customerComm=new SendingSMS(); } _customerComm.Send(message); }else{ if(_customerComm == null) { _customerComm=new SendingEmail(); } _customerComm.Send(message); }
那我們?cè)賮碚f說DI,DI主要幫助我們將實(shí)現(xiàn)注入到抽象的類(ICustomerCommunication接口)中.DI的主要減少類之間的耦合,并且將抽象和具體實(shí)現(xiàn)的綁定移除依賴類.
構(gòu)造函數(shù)注入
通過構(gòu)造函數(shù)注入我們將實(shí)現(xiàn)類的對(duì)象傳遞給依賴類的構(gòu)造函數(shù),并將其分配給這個(gè)接口.
public class Ordering { ICustomerCommunication _customerComm=null; public Ordering(ICustomerCommunication customerComm){ _customerComm=customerComm; } public void Order(string message){ _customerComm.Send(message); } }
在上面的代碼中,構(gòu)造函數(shù)將采用實(shí)現(xiàn)類對(duì)象綁定到接口中.如果我們將SendingSMS的實(shí)現(xiàn)傳遞給這個(gè)類,我們要做的就是聲明一個(gè)SendingSMS類的實(shí)例,然后將其傳遞給Ordering的構(gòu)造函數(shù),如下所示:
方法注入
通過使用構(gòu)造函數(shù)注入,我們將不得不在Ordering類的生存期內(nèi)使用實(shí)現(xiàn)類的實(shí)例SendingSMS或SendingEmail類.現(xiàn)在如果要在每次調(diào)用該方法時(shí)傳遞實(shí)現(xiàn)類的實(shí)例,則必須使用方法注入.
public class Ordering { public void Order(ICustomerCommunication customerComm,string message){ _customerComm=customerComm; _customerComm.Send(message); } }
調(diào)用方式如下所示
SendingSMS sendingSMS=new SendingSMS(); Ordering ordering=new Ordering(sendingSMS); ordering.Order(sendingSMS,"msg");
屬性注入
通過如上描述我們知道了構(gòu)造函數(shù)注入方法在整個(gè)生命周期中使用依賴類,而方法注入是將我們的注入直接去限于該方法中,然后我們?cè)偃チ私庖幌聦傩宰⑷?/p>
public class Ordering { public ICustomerCommunication customerComm {get;set;} public void Order(string message){ _customerComm.Send(message); } }
調(diào)用方式如下所示
SendingSMS sendingSMS=new SendingSMS(); Ordering ordering=new Ordering(sendingSMS); ordering.customerComm=sendingSMS; ordering.Order("msg");
其實(shí)構(gòu)造函數(shù)注入是實(shí)現(xiàn)DI最常用的方法.如果需要在每個(gè)方法調(diào)用上傳遞不同的依賴關(guān)系,則可以使用方法注入屬性注入的使用還是比較少的.
Reference
https://zh.wikipedia.org/wiki/控制反轉(zhuǎn)
https://zh.wikipedia.org/zh-hans/依賴反轉(zhuǎn)原則
到此這篇關(guān)于.NET IoC模式依賴反轉(zhuǎn)(DIP)、控制反轉(zhuǎn)(Ioc)、依賴注入(DI)的文章就介紹到這了,更多相關(guān).NET IoC模式依賴反轉(zhuǎn)、控制反轉(zhuǎn)、依賴注入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
HTTP錯(cuò)誤500.19解決方法(定義了重復(fù)的節(jié)點(diǎn))
HTTP 錯(cuò)誤 500.19 - Internal Server Error 無法訪問請(qǐng)求的頁面,因?yàn)樵擁摰南嚓P(guān)配置數(shù)據(jù)無效2013-06-06asp.net 更改gridview pageSize的方法
asp.net 更改gridview pageSize的方法,需要的朋友可以參考下。2011-07-07asp.net GridView中使用RadioButton單選按鈕的方法
這篇文章主要介紹了asp.net GridView中使用RadioButton單選按鈕的方法,結(jié)合實(shí)例形式總結(jié)分析了三種GridView中使用RadioButton單選按鈕的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07ASP.NET Core擴(kuò)展庫之Http請(qǐng)求模擬功能的使用
這篇文章主要介紹了ASP.NET Core擴(kuò)展庫之Http請(qǐng)求模擬功能的使用方法,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04asp.net開發(fā)與web標(biāo)準(zhǔn)的沖突問題的一些常見解決方法
Visual Studio .net從2003到現(xiàn)在的2008,一路走來慢慢強(qiáng)大……從以前的vs2003能自動(dòng)改亂你的html代碼到現(xiàn)在在vs2008中都能直接對(duì)html代碼進(jìn)行w3c標(biāo)準(zhǔn)驗(yàn)證并提示了,非常不易。2009-02-02完美解決Could not load file or assembly AjaxPro.2 or one of its
Could not load file or assembly AjaxPro.2,經(jīng)排查原來是mcafee限制了2007-08-08