The core components of the module assembly source code parsing loaded abp vnext2.0

abp vnext micro-services framework abp abp official on the basis of the above construction, to be honest, after reading the core components of the source code, it is very excited, the whole framework will reflect on the use of the components of a good, really super decoupling. the old version of the entire framework relies Castle problems, vnext be decoupled, support AutoFac or use the default container .vnext Core .Net is still in use EF core-based, and the rest ORM auxiliary thinking, of course, EF core DDD indeed be implemented there are advantages, EventBus provides distributed version, and provides an implementation version of RabbitMQ, Aop interceptor is still using Castle.Core.AsyncInterceptor. Dora.Interception it looks like you can solve, if the estimated height of the component, then this is an extension point the entire module loading system more perfect, provided with more optional features, the unit of work has also been reconstructed a small margin, the code more user-friendly (there is reflected in the work unit nested asynchronous design), etc. etc. there are many, of course, is not the focus of this article, vnext2.0 is worth using the framework. let's start back to the topic.

1, the module loading system

Module loading system be vnext entire framework of the inlet away from him, the framework can scrap it do specifically, see the following code analysis module inlet loading system as follows:

 Each application module startup frame must have a type that may be passed by generic or Type example, and parameters given promoter.

Start Module Type: Although the above given constraints must be implemented IAbpModule, but most cases are achieved

Temporarily do not speak AbpModule source code analysis back to the specific process do introduction.

ok, look AbpApplicationFactory factories do anything, by the name of the analysis it is clear that .AbpApplication factory.

This analysis method can be obtained as long as the incoming startup module type and parameters of the DI ServiceCollection and launch applications, and will be able to build a IAbpApplicationWithExternalServiceProvider, then take a look at what has IAbpApplicationWithExternalServiceProvider

 After the completion of construction of basic entities, calls the Initialize method to initialize the framework. Look at IAbpApplication Interface

 Comprising startup module type, the DI injection set, the DI service provider class, and a close ShutDown application must perform a method in a look IModuleContainer

 Comprising a collection of modules, in Abp, the module represents an assembly here is to start all the module types startup module type abp vnext frame depends, i.e., set the set of all programs that you can understand, because a Module Type (Inherited AbpModule type or implement IAbpModule type interface) on behalf of an assembly., and an assembly is only one Module type (or types inherited AbpModule achieve IAbpModule interface type).

ok, then returns to the above code

 Here omit some core processes independent code, as follows:

 Some simple check is not empty, there is a very interesting design, as follows:

Continue to view as follows

 ObjectAccessor source code as follows:

 Similar decorator pattern, inside a receiving type. Finally,

 ok, here is roughly the entire process, to create a ObjectAccessor IServiceProvider, Value and ObjectAccessor no value, while the write ObjectAccessor DI, and do a simple search optimization. ObjectAccessor on the role of the IServiceProvider, not yet introduced, would say follow-up .

Then look at the following code:

 Initialization of external setting parameters, injection Take singleton object IAbpApplication and IModuleContainer to the DI.

Then look at the following code:

Injection configuration files, logs, and other international services. Then look AddCoeAbpServices method

Injecting the ModuleLoader (dependencies between a set of handlers, the processing module loading lifecycle core types), the assembly was found classes (all assemblies can get through this type, as long as the assembly was added to the frame), found that the type of class (program set contains the set of all types)

 Initialization file system configuration, operation, etc., then see the following code, written to the above-described type DI

 The next line of code they're very interesting, as follows:

Look what it did, as follows:

 See what services.GetConventionalRegistrars done as follows:

 Obviously, reading the assembly registration list from the DI rule class, if not, the default assembly registration rule class is written. So, here if you want to customize the Assembly Registration rules, only the valid application loading register class lifecycle stage injection assembly to custom, the type must implement the interface shown below

 ok, finished after this extension point, to see what the default rule class DefaultConventionalRegistrar Assembly Registration dry, as follows:

 Very simple, self-reading, and then look to achieve AddType, as follows:

 Skip the type of support, if the type played DisableConventionalRegistrationAttribute characteristics, then this type will not be written DI.

 If the current type is not playing DependencyAttribute, or hit DependencyAttribute feature is not set Lifetime, the current type is also not written to DI.

Note here that, according to the code can be found, abp to the type of life cycle in two ways, the old version only one, as follows:

The first:

 By implementing ISingletonDependency (singleton injection), ITransientDependency (general reference type), (within the scope of the only) IScopedDependency three types of interfaces to indicate the current life cycle, the old version abp also use this approach, but there is no IScopedDependency

The second:

 By DependencyAttribute characteristics, the following structure

Next, the following code

 Type the following methods if the current hit ExposeServicesAttribute feature that will then call

 The use of this method is to find out if we need to release from the DI types, which can be used in several ways (the common interface itself, etc.), the following sample code:

 TestClass used if needed to achieve in the frame, can be dependent on injection ITestClass interface because

 Of course, where you can write more, because

 ExposeServicesAttribute characteristics of IncludeDefaults and IncludeSelf property is the default strategy,

IncludeDefaults is set to true interface to identify the type of its implementation, and the interface must begin with the letter I, and followed by the name and type of the current interface must be equal. If a match then the interface is active may be performed dependency injection.

IncludeSelf set to true, then the current can be injected through the dependency type.

Then the following code See

 Very simple, just load the effective application lifecycle stage injection specified Action, injection method as follows:

Examples of use, type mapping, as follows:

Finally, look at the following code

This code is very simple, is not explained .DependencyAttribute characteristics to the corresponding value can be specified action, ok, to summarize here the usefulness of this design is very nice, the old version of the original core of the system abp registered a single case of type is dependent on the castle, if changed to this design, more flexible, if we need to add a class to the underlying core, just create a class, and then with Dependency properties and characteristics can ExposeServices and DI perfect collection, while also providing the Action extension, so you can do a lot of things, on this point, much better than the old version abp. DefaultConventionalRegistrar introductions here

ok, returning AddCoreAbpServices method, as follows:

 Here is also very simple to write AbpModuleLifecycleOptions DI previously, the controlling parameter for the life cycle of loading modules, which correspond to four interface modules loaded Contributor life cycle,

 Look at the abstract core Module

 Here is certainly a lot of people very confused, so skip some of the processes here, look how ModuleManager process, as follows

 Contributor released collection

Contributor effect is obvious, module loading some of the ways you can perform the life cycle of these methods will get a ServiceProvider, that is, you can operate the DI, to complete the operation of some key services.

About the life-cycle approach of what modules are loaded, as follows

 Each interface corresponds to a life cycle, and the old version Abp this design is completely different. Advantages and disadvantages of being not found.

Subsequently, as follows:

 Singleton instance of the ModuleLoader call, method of performing load modules. Abp core algorithm and the old version as explained here slightly lower,

 The core points are as follows:

(1), all dependent load modules start module, and setting the dependencies, a set of last generation IAbpModuleDescriptor

 (2), topological sorting module, for detecting cyclic dependencies

 ok, let's start parsing core point source

. To handle dependencies between modules DependsOnAttribute characteristic kernel code is as follows:

 Get DependsOnAttribute characteristics of the current type, parses its internal type, added to the set of dependency types so dependencies represent the dependencies between modules two modes can be obtained according to the above code as follows:

 Commonly used is the second.

After all get a set of dependency types by the above method, the following recursive method

 This can traverse all modules since all modules start while to finally generate a traverse all instances of modules repeated except that the following types of modules

 Lifecycle module instance is a single embodiment, as shown below:

Then start the plug-in module begins processing configuration parameters

 Three kinds of ways to add plug-in modules are as follows

public static class PlugInSourceListExtensions
    {
        public static void AddFolder(
            [NotNull] this PlugInSourceList list, 
            [NotNull] string folder, 
            SearchOption searchOption = SearchOption.TopDirectoryOnly)
        {
            Check.NotNull(list, nameof(list));

            list.Add(new FolderPlugInSource(folder, searchOption)); } public static void AddTypes( [NotNull] this PlugInSourceList list, params Type[] moduleTypes) { Check.NotNull(list, nameof(list)); list.Add(new TypePlugInSource(moduleTypes)); } public static void AddFiles( [NotNull] this PlugInSourceList list, params string[] filePaths) { Check.NotNull(list, nameof(list)); list.Add(new FilePlugInSource(filePaths)); } }

Introduced here only one of the remaining core processes are the same, as follows:

FolderPlugInSource add plug-in type, the core parameters are as follows:

+ Directly to the folder path name, the following scanning plug-in assembly, and the assembly was filtered, the filter core as follows:

 Finally, to achieve a return to the core module of the type AbpModule

 ok, then back to the loading method of module plug-in loading system, as follows:

 ok, here you can find highlights

1, you can add plug-ins at the same time forms the host, can be a folder all the plug-in assembly, the assembly may be a solution, it can be a specified assembly file .Abp temporarily provide these three, of course, If you have the strength, you can write plug assembly remote calls.

2, and the system has completed loading the module integration, the above processes as the type of load modules all start dependent, and writes DI

 ok, plug-in modules described here is completed. Finally, ordinary generation module as set IAbpModuleDescriptor

Subsequently, after getting all of the set of modules (including plug) to begin setting up all the dependencies between modules, as follows, you will find the above attentive

 There are dependent on the set. The following code is the consolidation of the relationship.

 Here, the logic is simple, not introduced, skip, the main characteristic is achieved through DependsOnAttribute.

Next introduce two core points topological sorting module, for detecting cyclic dependencies

At this point, we got a complete set of modules, internal dependencies also has begun to perform well.

 The core code is as follows, with regard to topological sorting (core-logic algorithms access to the code itself, the main content is in accordance with the dependencies were successively added to the collection, the latter can be executed once, so you can integrate the life cycle), to prevent the cycle of dependence will not say, then, to start with the module life-cycle approach into the final execution module.

ok, here two core points introductions.

Next. The following code

 Generate the following context, and single cases written DI

The Item property drunk, personal feeling useless, because the following for loop

 Then execute the following code

 So these two time node interface performs life cycle must keep in mind while DI container will give you the context, allowing you to perform any type of operation required.

Adhesive

 The current corresponding to the type of module assembly write all types DI, injection default rules have been described above, the default type is registered DefaultConventionalRegistrar. Lifecycle interfaces simultaneously performed IZcfModule.

Three modules lifecycle interface has been performed here as follows:

 Remember node of its execution.

 System initialization module then begins, note that here I skipped content handover DI container (DI container handover source on subsequent analysis blog will introduce), as follows:

 ModuleManager singleton class released from the DI, the following initialization method performed

 This code module is loaded simple logging, core code behind said above, the module performs a predefined lifecycle approach correspond to the following interfaces:

Implementation of this method must implement four interfaces, of course, to virtual tags, so you can do some type of operation in the order once in AbpModule in. But these life-cycle function, context only ServiceProvider,

Limited functionality. The remaining three life-cycle interface provided is IServiceCollection instance, so there are still differences between them, in addition to the execution order.

ok, here abp vnext2.0 core module records system kernel source code analysis process is over, purely personal understanding, limited capacity, there is a problem, please correct me!

Next will explain how to complete the DI vnext switched into autofac or other container and how system loading and binding module.

Published 162 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/cjscf/article/details/104184545