.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)

2022-10-07,,,,

依赖倒置原则(dip)

依赖倒置(dependency inversion principle,缩写dip)是面向对象六大基本原则之一。他是指一种特定的的解耦形式,使得高层次的模块不依赖低层次的模块的实现细节,依赖关系被颠倒(反转),从而使得低层次模块依赖于高层次模块的需求抽象.

该原则规定:

  • 高层次的模块不应该依赖低层次模块,二者都应该依赖其抽象接口.
  • 抽象接口不应该依赖于具体实现,而具体实现则应该依赖于抽象接口.

通过如下一个简单的示例,我们来看一下,我们通过一个简单地下单流程向我们的用户发送相关的短信或者邮件.

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);
 }
}

这样看我们的代码没问题,目前只要我们完成了订单操作那么,那么则会触发发送功能,但是他却违反了dip,因为ordering类依赖于sendingemail类,而sendingemail类不是抽象类,而是一个具体的类.那我们再来想一个如果这时候业务口的人过来向我们提出了一个新的需求,要求我们改为短信而不是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);
  }
  
 }
}

根据上述需求我们不得不创建更多的类,并且在ordering类中声明他,最后我们还需要使用if else语句来决定使用sms还是使用电子邮件.但是当我们有更多这种处理操作后,那么可能比现在还混乱,这就意味着我们必须在ordering类中声明更多新的具体类的实例.

我们需要抽离出来一种方式,让高级模块去依赖于抽象,用它来代替我们实现类,该抽象将映射到实现类.

控制反转(ioc)

控制反转(inversion of control,缩写为ioc)是面向对象中的设计原则,他可以帮助我们使高层模块依赖于抽象,而不是底层模块的具体实现.换句话说,他有助于实现(依赖倒置原则——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
 }
}

我们再来修改一下ordering类以使用该抽象接口

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);
  }
  
 }
}

通过如上修改我们做的控制反转更符合dip.现在我们的高级模块只需要依赖于抽象,而不用去依赖实现.

依赖注入(di)

依赖注入(depeondency injection,缩写为di)是实现控制反转的一种方式.常用的依赖注入方法有3种:

  • 构造函数注入
  • 方法注入
  • 属性注入

虽然说通过上面代码我们实现了ioc,并且ordering类依赖于icustomercommunication抽象,但我们仍然在ordering类中使用了实现类,这使用我们无法在类于类之间完全解耦.

 if(issendingsms){
 if(_customercomm == null)
 {
  _customercomm=new sendingsms();
 }
  _customercomm.send(message);
 }else{
  if(_customercomm == null)
  {
   _customercomm=new sendingemail();
  }
  _customercomm.send(message);
 }

那我们再来说说di,di主要帮助我们将实现注入到抽象的类(icustomercommunication接口)中.di的主要减少类之间的耦合,并且将抽象和具体实现的绑定移除依赖类.

构造函数注入

通过构造函数注入我们将实现类的对象传递给依赖类的构造函数,并将其分配给这个接口.

public class ordering
{
 icustomercommunication _customercomm=null;
 public ordering(icustomercommunication customercomm){
  _customercomm=customercomm;
 }
 public void order(string message){
  _customercomm.send(message);
 }
}

在上面的代码中,构造函数将采用实现类对象绑定到接口中.如果我们将sendingsms的实现传递给这个类,我们要做的就是声明一个sendingsms类的实例,然后将其传递给ordering的构造函数,如下所示:

方法注入

通过使用构造函数注入,我们将不得不在ordering类的生存期内使用实现类的实例sendingsms或sendingemail类.现在如果要在每次调用该方法时传递实现类的实例,则必须使用方法注入.

public class ordering
{
 public void order(icustomercommunication customercomm,string message){
  _customercomm=customercomm;
  _customercomm.send(message);
 }
}

调用方式如下所示

sendingsms sendingsms=new sendingsms();
ordering ordering=new ordering(sendingsms);
ordering.order(sendingsms,"msg");

属性注入

通过如上描述我们知道了构造函数注入方法在整个生命周期中使用依赖类,而方法注入是将我们的注入直接去限于该方法中,然后我们再去了解一下属性注入

public class ordering
{
 public icustomercommunication customercomm {get;set;}
 public void order(string message){
  _customercomm.send(message);
 }
}

调用方式如下所示

sendingsms sendingsms=new sendingsms();
ordering ordering=new ordering(sendingsms);
ordering.customercomm=sendingsms;
ordering.order("msg");

其实构造函数注入是实现di最常用的方法.如果需要在每个方法调用上传递不同的依赖关系,则可以使用方法注入属性注入的使用还是比较少的.

reference

到此这篇关于.net ioc模式依赖反转(dip)、控制反转(ioc)、依赖注入(di)的文章就介绍到这了,更多相关.net ioc模式依赖反转、控制反转、依赖注入内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI).doc》

下载本文的Word格式文档,以方便收藏与打印。