Spring. NET provides many functions, such as dependency injection, Aspect-oriented programming (AOP), data access abstraction, and ASP. NET extension. The core function of dependency injection is dependency injection. The biggest benefit of dependency injection is that it can reduce the coupling between objects in an application.
Inversion of control and dependency injection are abbreviated as IOC and Di. They are actually different descriptions of the same concept, the concept of control reversal is vague (it may be understood as the Container Control Object layer, and it is difficult to think of who will maintain the object relationship ), so Martin flower, a master-level character in 2004, gave a new name: "dependency injection". relative control reversal, "dependency injection" also describes the objects injected since the IOC container is configured.
IOC is also known as the Dependency inversion principle ("Dependency inversion principle") or the Dependency inversion principle ". Among the several principles of object-oriented editing, the "Dependency inversion principle" is very important. Almost all frameworks use the "Inverted injection" technique (mentioned in Martin Fowler's paper published in 2004, this is an application of the IOC principle. C ++, Java, and various. Net Language object-oriented programming language programmers have used these principles.
2.1The familiar code has a potential crisis.
This is the method we used to write code. Here we use garden for example:
Now we need to get the articles from all the figures in the school. Now we provide an ireaderdal data access interface for getting the articles, and an ireaderservice business logic interface for getting the articles, obtain the document by calling the methods of the Data Access Implementation class in the business logic implementation class.
1 namespace cnbloglesson_2_1 2 {3 /// <summary> 4 /// data access interface for reading articles 5 /// </Summary> 6 public interface ireaderdal 7 {8 // /<summary> 9 // Method 10 for reading the article /// </Summary> 11 void getarticle (); 12} 13}
1 namespace cnbloglesson_2_1 2 {3 /// <summary> 4 /// business logic interface for reading the article 5 /// </Summary> 6 public interface ireaderservice 7 {8 /// <summary> 9 // Method 10 for reading the article /// </Summary> 11 void getarticle (); 12} 13}
1 using system; 2 namespace cnbloglesson_2_1 3 {4 /// <summary> 5 // read the data in Article 6 // </Summary> 7 public class readerdal: ireaderdal 8 {9 /// <summary> 10 /// Method for reading the article 11 /// </Summary> 12 public void getarticle () 13 {14 console. writeline ("Get blog spring. net series blog "); 15} 16} 17}
1 using system; 2 namespace cnbloglesson_2_1 3 {4 // <summary> 5 // read the business logic Class 6 of the article /// </Summary> 7 public class readerservice: ireaderservice 8 {9 /// <summary> 10 // The Data Reading Class Object of the Article 11 /// </Summary> 12 private ireaderdal Dal = new readerdal (); 13 14 /// <summary> 15 /// reader's name 16 /// </Summary> 17 private string name {Get; set ;} 18 19 /// <summary> 20 /// Method for reading the article 21 /// </Summary> 22 public void getarticle () 23 {24 Dal. getarticle (); 25} 26} 27}
The above is the data category and business logic category of the bloggers. Let's take a look at our call methods.
1 using System; 2 namespace CnblogLesson_2_1 3 { 4 class Program 5 { 6 static void Main(string[] args) 7 { 8 IReaderService service = new ReaderService(); 9 service.GetArticle();10 Console.ReadKey();11 }12 }13 }
Disadvantages of the above method:
The ireaderdal object is created and maintained in readerservice, which relies heavily on specific implementations rather than interfaces. Without changing the code, it is very difficult to expand and maintain. For example, the method for getting garden articles has changed. We used the getarticle method of readerdal to get it. Now we use WebService to get it. What is better, only the internal code of readerservice can be modified again.
Solution:
The so-called IOC (control inversion) means that the application itself is not responsible for the creation and maintenance of dependent objects (the benefit is that the dependencies between the decoupled objects ), assign the power to create and maintain dependent objects to external containers. For example, we will hand over the specific objects to the configuration file. The configuration file is used to determine whether to use the readerdal class or WebService to obtain the document from the garden. You only need to modify the configuration when you need to switch the retrieval method, without modifying the code. In this way, the control is directed to the external container, which is called the control inversion. From the runtime perspective, the external container dynamically injects the dependent object into the application. It can also be understood as a dynamic way to pass the article retrieval method into readerservice through xml configuration. This method is called di (dependency injection ).
2.2How to decouple dependency injection and control Inversion
We changed the creation and maintenance of the ireaderdal object and changed to the dependency injection method. The ireaderdal is dynamically injected to the application through the configuration file, in other words, the power to create and maintain ireaderdal objects is transferred from the ireaderservice to the external and handed over to the configuration file for control.
At this time, let's review the description of control inversion and dependency injection in Chapter 2.1, so that we can better understand their meaning.
1 using spring. context. support; 2 namespace cnbloglesson_2_2 3 {4 // <summary> 5 // read the business logic Class 6 of the article // </Summary> 7 public class readerservice: ireaderservice 8 {9 /// <summary> 10 // The data category object for reading the article 11 /// </Summary> 12 private ireaderdal; 13 14 public readerservice () {15 16 // use iapplicationcontext to configure 17 iapplicationcontext context = contextregistry. getcontext (); 18 Dal = (ireaderdal) context. getObject ("readerdal"); 19} 20 21 /// <summary> 22 // reader's name 23 /// </Summary> 24 private string name {Get; set;} 25 26 /// <summary> 27 // Method for reading the article 28 /// </Summary> 29 public void getarticle () 30 {31 Dal. getarticle (); 32} 33} 34}
Friends, the above uses spring. net Framework IOC container, but here we only discuss the idea of dependency injection and control inversion, which can be traced back to the dependency inversion principle. The dependency inversion principle says "abstraction should not depend on details. Details should be dependent and abstract ". Currently, do not worry about how to configure and set up the IOC container of spring. net. We will discuss these issues one by one in the following chapter.
Sample Code: http://download.csdn.net/detail/cilence6788/5077849
[Transfer] Chapter 2 control reversal and dependency Injection