IOC Background Introduction
the traditional implementation :
- The internal code of the program controls the relationship between the class and the class (for example, a method that calls another class in a specific Class).
- Use the new keyword to implement a combination of relationships between two classes.
- This implementation creates a coupling between classes.
implementation of the IOC :
- It refers to the inter-class relationship from inside the program to the outer container, that is, the container dynamically injects some kind of dependency between classes in the class at run time.
- Object A relies on object B, and when object a needs to use object B, the IOC container immediately creates an object B for the object A.
- The IOC container is an object manufacturing plant, what you need, it will be sent to you, you directly use it, and no longer have to care about how you use the things are made, do not care how the final is destroyed, all of these are arranged by the IOC container.
The IOC principle of--spring
Applications in the project
- Configuring in the application configuration file
<configsections> <sectiongroup name="Spring"> < section name="context" type=" Spring.Context.Support.ContextHandler, Spring.core " /> < section name= "objects" type=" Spring.Context.Support.DefaultSectionHandler, Spring.core " /> </sectiongroup> </configsections>
<spring> <context> <resource uri="config://spring/objects" /> </Context> <objects xmlns="Http://www.springframework.net"> <object id="bidsystementities" type=" Lfbidsystem.model.bidsystementities,lfbidsystem.model " singleton=" false " /> <!--notes to the dbsession layer- - <object id="dbsession" type="LFBidSystem.DAL.DbSession, Lfbidsystem.dal " singleton=" false "> <!--adding attribute injection, pointing to the D-layer injection-- < property name="T_biddal"ref="T_biddal"/> </Object> <!--D-layer annotations -- <object id="T_biddal"type="LFBidSystem.DAL.T_BidDal, Lfbidsystem.dal " singleton=" false " /> </Objects> </Spring>
- Getting objects in a program
SpringHelper.GetObject<ICoreDbSession>("DBSession");
Springhelper is a class that is encapsulated in a project.
In the above configuration file, the way to create an object and assign a value to an object's properties is shown.
Here are the highlights:
- Configuration content can be written to the application configuration file (Web. config), or it can be written to an XML file.
- In the background, we mentioned that the IOC container is the equivalent of a factory, help us to produce objects, there are many ways to produce objects, in particular, refer to the following links:
[Spring.net IoC] III: getting objects
The way the object is produced, determines when the object is created, and that's what we're going to introduce here.
Execution process
This is a reference to the execution flow of SPRINGIOC in Java.
- Application starts execution
- Creating an IOC container
- Parsing the configuration file
- Initializing an object instance configured in the IOC container
However, there are special cases when we configure an object in a configuration file and set it to "Singleton=false", the initialization process of the object is deferred until we get the object, that is, when we call the GetObject method, it is instantiated.
This is when we execute the following line of code in the project:
dbSession = SpringHelper.GetObject<ICoreDbSession>("DBSession");
The program obtains an instantiated object of the class Dbsession, and assigns a value to each property in the dbsession based on the attributes in the configuration file. The value assigned to each property is an instance of a class in the specific Dal layer, so the constructor for each dal is called immediately thereafter. The Setdbcontext () method is called in the constructor of each D-layer class, so how many times the Setdbcontext () method is executed when there are many properties in your dbsession.
publicpartialclass DbSession : IDbSession { publicgetset; } }}
Summarize
The IOC acts as a third party to maintain dependencies between objects, lifting the coupling between objects, and making our project architecture more flexible.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
1.1-point Architecture (iii)--spring.net IOC