SwiftSuspenders 1.6-in simple terms 1

Source: Internet
Author: User
In-depth

This in-depth analysis of SwiftSuspenders mainly involves in-depth analysis of SwiftSuspenders, including analysis of the architecture of SwiftSuspenders, And the lifecycle of an object during the entire injection process, and a detailed explanation of each class of SwiftSuspenders.

Glossary

First, some terms that may be used in the article are listed for clarity. This helps reduce the ambiguity of understanding:

To explain the relationship between terms, we designed the following scenario for an analogy: shining students are ill and need to take an injection.

Term name

English

Explanation

Dependency

Dependency

In simple terms, when some functions of Class A require Class B to assist, Class A is dependent on Class B. If shining students need medicine to cure the disease, then it is said that shining is dependent on medicine.

Injection

Injection

A behavior that establishes dependency. If you want to use the medicine, you must make the medicine into the body (reference relationship ). How can we get the medicine into the body? A needle can be injected to obtain the drug. This is injection. When the dependency between A and B is established by C. B is injected into A by C.

Injector

Injector

It is an instance of the Injector class in SwiftSuspenders, which is equivalent to playing the role of a needle.

Injection Configuration

InjectionConfig

Is the configuration object for an injection behavior. Class containing the injection request and the injection name. Information such as the injector used and the policy for returning results. If a cold is shining, injection of cold medicine is required, and injection of anti-inflammatory drugs is required, then two types of medicine will be requested. The same type of medicine may have different brands, so sometimes you need to add the name of the requested medicine, which is equivalent to the injection name. The injection configuration is equivalent to a prescription ticket.

Injection Point

InjectionPoint

Dependency injection can be divided into several forms, such as constructor injection, property injection, and method injection. These are called injection points. For example, injection, PP or arm. Property injection copies public attributes or setter methods. Method injection is like passing parameters in a function.

Injection description

InjecteeDescription

Contains all the injection points of an injected object. For example, you need to have a shot in your arm and two shots in your ass. It will be described in the injection description. InjecteeDescription is an out-of-package class of Injector.

Return Policy

InjectionResult

It reflects the method in which the injector injects requests, such as directly creating A new B object, injecting A, or creating A new B object in singleton mode, then all the injection using B will share the same object.

 

Architecture

 

Shows the class diagram of SwiftSuspenders. The entire framework is centered on Injector. The InjectionPoint package contains all injection point-related classes, and the InjectionResult package contains class related to the return policy. Other classes work with the Injector class. The explanation of specific classes will be introduced in the next chapter.

The design patterns used mainly include policy patterns and template methods.

The InjectionResult package mainly uses the pass policy mode. The author creates different ing policies for different needs, such as Singleton mode, class mode, and object mode. It also allows developers to develop other required policies.

The InjectionPoint package mainly uses the template method to implement the initializeInjection hook method for different subclasses to implement different injection points.

Object Lifecycle

This chapter mainly analyzes the two main activities of SwiftSuspender internally: The ing and injection processes.

Ing sequence diagram

 

Taking mapClass as an example, the main program first calls the mapClass method of Injector and passes in the required parameters;

Injector then calls its own getMapping method to obtain a corresponding injection configuration object. If the corresponding injection configuration has been created before, it will return directly. If no, A new injection configuration object is created.

After the injection configuration object is obtained, the setResult method of InjectionConfig is called to set the return policy for this configuration. For example, if it is a class ing, it is set to InjectClassResult. If it is a Singleton, set it to InjectSingletonResult. And so on.

Finally, the InjectionConfig parameter is set. At the same time, the InjectionConfig object will exist in the Dictionary of m_mappings of Injector. The entire ing process is complete.

Injection sequence diagram

The Injector's getInstance () method can be used to obtain the injected object. The main program sends a class + injection name request to the Injector to obtain the object.

After the Injector receives the request, it calls its own getConfigurationForRequest () method to obtain the corresponding InjectionConfig injection configuration.

After the injection configuration is obtained, call the getResponse () method of InjectionConfig to obtain the result.

The getResponse () method of InjectionConfig calls the corresponding InjectionResult to return the getResponse () method of the policy to obtain the result. If the value of traverseAncestors is set to true, when the corresponding injection configuration is not found in the current injector, the parent injector is traversed from bottom to top until the location is found.

InjectionResult calls the instantiate () method of Injector to obtain the object.

The instantiate method of Injector calls the getInjectionPoints method of Injector to obtain all injection points of the class to be injected. Then, call the constructor injection point to obtain the instance of the requested class.

After an instance of the Request class is obtained, Injector calls its InjectInto method to perform dependency injection like other injection points of this object.

Finally, the injection results are returned to the program. During the injection process, if the injected object still needs dependency injection, it will execute the same dependency injection on the injected object as in the preceding process, then, inject the injected object into the target object.

Transfer http://sswilliam.blog.163.com/blog/static/189696383201176114713922/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.