Delphi 2010 - Modular application, how to get class properties in the host app?

953 views Asked by At

i did some tests about modular applications mechanism .

the base thing i tried is load packages at run time and playing with classes inside it .

my test was around building form in my package and load form "TCustOrder" from my host application the test succeeded very well .

the problem is :

My TCustOrder has property named "Client:TObject" how can i access this property from the host application .

The Code :

var x : HRESULT ;
    AClass : TPersistentClass ;
    FormOrder : TForm ;

begin

x := LoadPackage('Cutorder.bpl') ;

if x <> 0 then
begin

      AClass := GetClass('TCustOrder');


      if AClass <> nil then
        FormOrder := TComponentClass(AClass).Create(Application) as TForm;



          if Assigned(FormOrder) then
             begin

              FormOrder.Show
             end;

end;
1

There are 1 answers

1
Arioch 'The On BEST ANSWER

The application should know the base of a class to use it. The interface, protocol, specification, you name it. The particular package gives a certain specialized implementatino of it.

So the overall structure, based on BPL and conventional objects, should be the one that you can observe in VCL itself.

Base.BPL - contains TBaseClass and TList<TBaseClass> of implementations provided by plugins. It also contains all virtual methods and properties needed for main program to use it. It is convenient to have

class constructor TBaseClass.CreateClass;
begin
  MyList := TList<TBaseClass>.Create;
end;

class destructor TBaseClass.DestroyClass;
begin
  FreeAndNil(MyList);
end;

class procedure TBaseClass.RegisterClass;
Begin
  MyList.Add(Self);
end;

class procedure TBaseClass.UnRegisterClass;
Begin
  MyList.Remove(Self);
end;

App.Exe would list Base.BPL among its runtime packages (in project options) and use Base.BPL as a "meeting point" with plugins.

Plugin.BPL would list Base.Bpl as required runtime package and use it as rendezvous place too.

TMyClass1 = class (TBaseClass);

...

initialization
   TMyClass1.RegisterClass;
finalization
   TMyClass1.UnRegisterClass;
end.  

My TCustOrder has property named "Client:TObject" how can i access this property from the host application.

type  (** Base.BPL **)
  TBaseClass = class ...
    protected function GetClient: TObject; virtual; abstract;
    public property Client read GetClient;
  ...
  end;

type  (** Plugin.BPL **)
  TMyClass1 = class(TBaseClass) ...
    protected function GetClient: TObject; override;
  ...
  end;

Note, at least Delphi XE2 tends to destroy string array constants when unloading dynamic packages. Dunno if it holds for D2010. In XE4 it was fixed. So "dragons may be here". In modern Delphi there are a lto of bugs in RTL/VCL, be ready to debug them.