Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Share datamodules between Intraweb and VCL projects
#1
Question 
Hello,

this is a more general architectural question than a specific Intraweb one...:

I have a fully functional Intraweb application which manage some classes and data modules.
I would like to leverage on these classes and data modules in a new VCL application.

For the classes is enough to share files but for the datamodules (which leverage on FireDAC) I would like to know which is the best way to manage the connection to DB.

In my Intraweb application all the datamodules refer to a connection component in the Usersession object.
When I try to use them in the VCL application I relized I cannot use the Usersession unit (or at least I think is not the best solution) and I don't know how to manage the connection to the DB in the datamodules.

I thinking about define a parameter to pass the connection in the datamodules constructor but perhaps there is a better solution.

Which is the best way to share datamodules and address the FDConnection between IW and VCL projects?

Thank you,
Davide
Reply
#2
Hi Davide,

Been there, done that, a long time ago.

IMO, the best way to do it is using code to link things instead of properties via Object Inspector.


Something like this:

Code:
unit Unit1;

interface

uses
  {$IFDEF IW}
  UserSessionUnit,
  {$ENDIF}
  System.SysUtils, System.Classes, FireDAC.Comp.Client;


type
  TDataModule1 = class(TDataModule)
    procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
    FDBConnection: TFDConnection;
  public
    { Public declarations }
  end;

{$IFNDEF IW}
var
  DataModule1: TDataModule1;    // NEVER USE THIS IN AN IW application
{$ENDIF}

implementation

{$R *.dfm}

procedure TDataModule1.DataModuleCreate(Sender: TObject);
var
  i: Integer;
  DS: TFDRdbmsDataSet;
begin
  {$IFDEF IW}
  FDBConnection := UserSession.MainConnection;
  {$ELSE}
  FDBConnection := SomeDataModule.MainConnection;
  {$ENDIF}
  for I := 0 to ComponentCount - 1 do
  begin
    if Components[i] is TFDRdbmsDataSet then
    begin
      TFDRdbmsDataSet(Components[i]).Connection := FDBConnection;
    end;
  end;
end;

end.

Add a conditional define to your IW project (something like IW) and use it to isolate IntraWeb-specific code.

Connect the DBConnection to the DataSets via code, using a code like this (that you can write once in a TDataModuleBase that would be the ancestor of all your DataModules). 
This simple arrangement works well for this kind of scenario.

Have in mind that global variables like that one that Delphi creates for you (var DataModule1: TDataModule1) is a no-no in IntraWeb applications.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)