ABP Source Code Analysis VI: implementation of Dependency Injection

Source: Internet
Author: User

The implementation of ABP's dependency injection has an essential two ways: 1. Essentially, it relies on castle, the old dependency injection framework. 2. One way to achieve this is by implementing the Iconventionaldependencyregistrar class definition register rule and then reading the rule through Iocmanager to complete the dependency injection. 3 Another way to achieve this is to directly iocmanager the Register method to complete the injection.

The first approach :

The following specific analysis: code in the ABP project file under the dependency folder.

First, a diagram of the related interfaces and classes is given and then explained individually.

idictionarybasedconfig/dictionarybasedconfig: Provides the ability to manage configuration through dictionary. After analysis, this dictionary has not been used throughout, it is unclear what the ABP author thinks.

Conventionalregistrationconfig: Encapsulates a bool attribute installinstallers, To tell the ABP whether the underlying framework wants to register the register rule specified by the Iwindsorinstaller interface in the corresponding assembly. (Refer to Castle for information on register through Iwindsorinstaller)

iconventionalregistrationcontext/conventionalregistrationcontext: Works similarly to other context classes. It is mainly used as the method parameter to pass data between methods conveniently. The main package here is Assembly,iocmanager and Conventionalregistrationconfig.

Iconventionaldependencyregistrar: Iocmanager encapsulates a list of Iconventionaldependencyregistrar. Iocmanager traversing the list in the registerassemblybyconvention method, The register is completed according to the rules defined in the Iconventionaldependencyregistrar instance. Note: The registerassemblybyconvention of Iocmanager is called in the Initialize method of Abpkernelmodule.

Here's the code to put it in order:

First, add Basicconventionalregistrar to the list of Iocmanager Iconventionaldependencyregistrar in the Preinitialize method (explained later) Abpkernelmodule

Iocmanager maintains a list called _conventionalregistrars, where the element type is Iconventionaldependencyregistrar.

Then Iocmanager's registerassemblybyconvention is called in Abpkernelmodule's Initialize method.

Iocmanager iterates through the list in the Registerassemblybyconvention method and completes the register according to the rules defined in the Iconventionaldependencyregistrar instance.

The four implementation classes of Iconventionaldependencyregistrar have been discussed in the previous article, and this is not repeated here. Paste the Basicconventionalregistrar code, which registers all classes that inherit from the Itransientdependency,isingletondependency and Iinterceptor interfaces.

Here, it is essential to understand the process of dependency injection through iconventionaldependencyregistrar .

The second way :

Directly on the code interpretation, Abpmodule has a member of the protected Iocmanager, so Abpmodule's derived classes can use this Iocmanager to complete the registration.

Finally, it is recommended to implement a more interesting singleton pattern in the ABP:

singletondependency: A method of generating an Singelton instance for an instance in a container (not explained). This is a one-thread-safe and time-delay-loaded singleton pattern. Take full advantage of the new syntax for. Net 4.0 lazy. (the author thought that the use of lazy implementation of the singleton model is the author's innovation, the original ABP has already done so.) I have also written an article using the lazy implementation of the Singleton model http://www.cnblogs.com/1zhk/p/5257340.html, no use of containers, more reference. )

ABP Source Code Analysis VI: implementation of Dependency Injection

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.