Learned from the demo program,
In the framework main program engineering file, forms are replaced with Utangramframework,
Each module has a tmodule subclass and a getmoduleclass export function
So how does the framework manage the modules?
Let's start with the module.
UnitDlloneplugin;Interfaceusessysutils,classes,utangrammodule,sysmodule,regintf; Type Tusermodule=Class (tmodule)Private PublicConstructor Create;Override; destructor Destroy; Override; procedureInit;Override; procedureFinalOverride; procedureNotify (Flags:integer; Intf:iinterface);Override; class procedureRegistermodule (Reg:iregistry);Override; class procedureUnregistermodule (Reg:iregistry);Override; End;ImplementationinitializationRegistermoduleclass (tusermodule);FinalizationEnd.
This is the Tmodule subclass of the module, thecode below initialization is executed first , so let's take a look at the registermoduleclass function on the Utangrammodule.pas
UnitUtangrammodule;{$weakpackageunit on}InterfaceusesSysutils,regintf,sysmodule;procedureInstallmodule (reg:iregistry);procedureUninstallmodule (reg:iregistry);functionGetmoduleclass:tmoduleclass; Exports Installmodule, Uninstallmodule, Getmoduleclass;ImplementationvarFmoduleclass:tmoduleclass;procedureRegistermoduleclass (moduleclass:tmoduleclass);beginFmoduleclass:=Moduleclass;End;functionGetmoduleclass:tmoduleclass;beginResult:=Fmoduleclass;End;End.
Registermoduleclass (tusermodule); in fact, the Tmodule subclass in the module is saved to a class reference variable fmoduleclass;
The exported function Getmoduleclass returns the fmoduleclass, so the Tmodule subclass of the module can be obtained by exporting the function Getmoduleclass
Next we look at the main program
Forms is replaced with Utangramframework, and a Application object is defined in the Utangramframework file, which means
Program HOST; uses utangramframework, in'mainform.pas' { Frmmain}; {$R *.res} begin application.initialize; Application.mainformontaskbar:= True; Application.createform (Tfrmmain, frmmain); Application.Run; End.
The Application object here is actually defined by Utangramframework.
Unit Utangramframeworkttangramapp=Classvar Application:ttangramapp; initialization application:=ttangramapp.create; Finalization Application.free;
In this way, the application object defined by the framework is created at the beginning of the program, and then we start with this line of code and debug to see how the module is managed
First enter the Tangramapp constructor
The floadmodulefromregistry here is Ttangramapp's Loadmodulefromregistry property variable, and the Set framework defaults to module loading from XML
The Fmodulemgr is a Tmodulemgr object, which is responsible for the management of the module, and the constructor of the module management class.
This creates a TObjectList object that is used to save the list of modules.
Tsingletonfactory.create (Iregistry, @CreateRegObj); Several lines are the creation of a class factory, which is described in detail in the interface management.
Continue, when all the units that contain the initialization section are executed, the program entry
Enter these functions to see
As can be seen, in fact, Ttangramapp just a few of the same name function, the actual call or to the Application object of forms, so that the winner program before starting to have the opportunity to do some initialization work
Next run to the Run function
Floadmodulefromregistry is set to true in the constructor, and we have not modified this property, so here we begin to load the module automatically.
This calls the module management class Tmodulemgr object Fmodumgr for module loading, into Loadmodules
The Getmodulelist function is responsible for reading the module file path that needs to be loaded automatically from the XML file and saving it to alist
This first determines if the file exists, and if it exists, it is ready to load
This creates a Ttangrammodule object based on the module's file, which is responsible for loading the module
This determines the type of the module and then loads it, after the load is completed
@GetModuleClassPro: = GetProcAddress (Fmodulehandle, getmoduleclass " ); if Assigned (getmoduleclasspro) then begin fmodulecls: =getmoduleclasspro; Fvalidmodule: =fmodulecls<>nil ; if (Fmodulecls<>nil ) " Span style= "color: #0000ff;" >and (createmoduleobjinstance) then Fmoduleobj: =fmodulecls.create; end ;
Get the module's export function Getmoduleclass, if this function is saved and called, save the return value to Fmodulecls, this fmodulecls save is the Tmodule subclass in the module, and create the object
procedureTmodulemgr.loadmodulefromfile (ConstModulefile:string);varModule:ttangrammodule;begin TryModule:=ttangrammodule.create (modulefile,self. Floadbatch); if module.isvalidmodule then fmodulelist.add (Module) ElseModule.free; Except on E:exception Do beginwriteerrfmt (Err_loadmodule, [Extractfilename (Modulefile), e.message]); End; End;End;
If it is a valid module, it is added to the object list Fmodulelist
procedure Ttangramapp.run; begin if then begin fmodulemgr.loadmodules; fmodulemgr.init; End ; Forms.Application.Run; fmodulemgr.final; End;
When all modules are loaded, start module initialization
This takes out the Module object list fmodulelist each object in the initialization
Notice here that the fmoduleobj.init fmoduleobj is a Tmodule object, which is the object created by the Tmodule subclass in the Getmoduleclass return module when the module is loaded.
While Init is a virtual function in Tmodule, the actual call here is actually the INIT function implemented by the Tmodule subclass in the module.
When all modules are initialized, enter the message loop until the main form exits, calling the module's terminating function
procedure Ttangramapp.run; begin if then begin fmodulemgr.loadmodules; Fmodulemgr.init; End ; Forms.Application.Run; fmodulemgr.final; End;
Tmodulemgr provides two interface Imoduleloader,imoduleinstall for developers to customize module loading and installation
Video Tutorial Address:
erpsystem Development tutorial Http://pan.baidu.com/s/1kT7Rb3D
Erpsystem Development Tutorial Http://pan.baidu.com/s/11xN5s
Framework Source Code Http://pan.baidu.com/s/1jGIc2Su
Demo Source Code Http://pan.baidu.com/s/1bnyEafH
Contact qq:1330009208 (Verification information please fill in Erpsystem)
Erpsystem Development Tutorial 04 Framework Module Management