asp. Understanding Inversion of Control in net Text + Code

  • 2021-01-11 01:57:56
  • OfStack

IOC is interpreted as: "Inversion of control is a common characteristic of frameworks, ES11en ES12en ES13en ES14en ES15en ES17en ES18en ES19en ES20en ES21en ES22en ES24en ES26en ES28en ES29en ES30en ES31en ES32en ES34en ES35en"

I would like to perform a personal exposition on this concept to facilitate my understanding. Inversion of control, literally, is the change of control from passive to active to passive, or from passive to active to passive. From this point of view, IOC becomes very easy to understand.
Here's an example: When your supervisor asks you to do one thing, there are so many processes and the supervisor orders you to do something (where the initiative is in the supervisor's hands and you are passive).
You get ordered to do something (when the theme is you, you are the initiative, you are in control) and you get it done (when the theme is still you, you are in control)
Report to the supervisor that something has been done (the initiative has been handed over to the supervisor again)

The whole process above has completed 1 IOC. As can be seen from the above, the basic idea of IOC is the transfer process of control.

Here's a code example:
If there is Class, A, Class, B, there will be a primitive B inside A, calling one of the essences of B


DoMethod public Class B

  {

  public void DoMethod (a) 

  {

  /// do somthing ; 

  }

  }

  public Class A

  {

  public void Excute (a) 

  {

  B b = new B (a); 

  b.DoMethod (a); 

  }

  }

A a = new A (); A a = new A (); a. Excute ();

From these two lines of code, in fact, there is also an IOC process, a -- > b - > a, the key point to understand is the execution of b when Excute is called inside A. Understand the IOC, we look at 1 DI, B A calls from above we can see that in the original 1 A instance, one must also be instantiated B, that is to say, if there is no B or B out of the question, A cannot instantiate, this creates a kind of dependence, is B A dependence, this dependence from a design standpoint is coupling, obviously it is not meet the requirements of high cohesion and low coupling. At this time, we need to decouple, of course, decoupling has many kinds of essentials, and DI is one of them. Either of these decoupling elements does not mean that A and B are not related at all, but rather that the implementation of this connection is implicit, less direct, but easy to implement, and easy to extend, unlike the above code, which directly generates new1 B. So why do we always associate IOC with DI? This is because the basic idea of DI is IOC, and there is another key to reflect the idea of IOC, that is Service Locator, which seems to involve very little. In fact, these are derived from java, although I have not used java for several years, in which Spring all use IOC and DI as if they are closely connected in one block.


Related articles: