Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I create an additional data module for a Session?
#1
I'm tasked with maintaining a very old Delphi XE7, IntraWeb 14 (I think.. how do I determine the IW version number installed?), OS: Windows Server 2008

The IntraWeb app is working well.  It connects to a Firebird database and all the DB connection components (DAC by DevArt version 5.5.16) are in the UserSessionUnit.

My task is to allow this old app to connect to an additional Firebird database.  The company is upgrading all their apps, and they all point to a new Firebird database that has a different internal table and field structure (both "old" and "new" DBs are Firebird 3x, just different tables and fields).  The Intraweb app is a "helper" app, not the main one, so it must be made to bridge the gap between old and new.

I've done this before, many years ago, but forgot the best way to code this.  Here is my plan:

- Leave everything that is working, alone
- With your generous help, code the few lines in the few places necessary, to create an additional datamodule for the user session.
- Copy everything from the existing working UserSessionUnit to the newly created-on-the-fly datamodule (All component names can remain the same this way, making other code adjustments easier.  Only the datamodule the components and code point to, need be changed.)
- In the .ini file, put a flag that tells the IntraWeb app to use the old DB or the new one. (I'll flip this on "switch over" day from old DB to new DB)
- Make adjustments in the code to point to the appropriate datamodule and thereby the appropriate DB as indicated by the parameter in the above step

Question:

- I need step by step instructions on how to add an additional data module to the IW / Delphi XE7 app, starting right from the menus in XE7, to the final coding snippets and tell me the units to put them in.

Thank you for any help you can provide.
Reply
#2
to create a new data module in any Delphi (including IW) application:

- File -> New -> Other -> Delphi Files -> Data Module

It will create an empty DataModule with a default name. Change the name of the DataModule for something more meaningful and save the project. You are mostly done

Now, to start using it in your application... there are several ways to initialize and use it... the most simple way of using it is like this:

Code:
unit Unit1;

interface

uses
  System.SysUtils, System.Classes;

type
  TDataModule1 = class(TDataModule)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function DataModule1: TDataModule1;

implementation

{$R *.dfm}

uses
  IWInit;

threadvar
  _DataModule1: TDataModule1;

function DataModule1: TDataModule1;
begin
  if not Assigned(_DataModule1) then begin
    _DataModule1 := TDataModule1.Create(WebApplication);
  end;
  Result := _DataModule1;
end;

end.

I kept the default DataModule1 name, but you get the idea.

First thing you need to do is moving the var DataModule1 created by Delphi IDE to the implementation section and change it into a threadvar, so each session gets its own copy. You also change the name (I just added an underscore to it)

Then you declare a function named DataModule1 which will create an instance on demand. The owner of the new data module instance becomes the WebApplication instance which means that you don't need to worry about its destruction (WebApplication will do it for you).

And that's it. You can start using it as you would in any other Delphi application just using its name "DataModule1" to reference it and everything will just work.

PS: There are several ways of achieving the same result, but I find this way particularly handy and easy to understand.
Reply
#3
(09-09-2019, 09:11 AM)Alexandre Machado Wrote: to create a new data module in any Delphi (including IW) application:

- File -> New -> Other -> Delphi Files -> Data Module

It will create an empty DataModule with a default name. Change the name of the DataModule for something more meaningful and save the project. You are mostly done

Now, to start using it in your application... there are several ways to initialize and use it... the most simple way of using it is like this:

Code:
unit Unit1;

interface

uses
  System.SysUtils, System.Classes;

type
  TDataModule1 = class(TDataModule)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function DataModule1: TDataModule1;

implementation

{$R *.dfm}

uses
  IWInit;

threadvar
  _DataModule1: TDataModule1;

function DataModule1: TDataModule1;
begin
  if not Assigned(_DataModule1) then begin
    _DataModule1 := TDataModule1.Create(WebApplication);
  end;
  Result := _DataModule1;
end;

end.

I kept the default DataModule1 name, but you get the idea.

First thing you need to do is moving the var DataModule1 created by Delphi IDE to the implementation section and change it into a threadvar, so each session gets its own copy. You also change the name (I just added an underscore to it)

Then you declare a function named DataModule1 which will create an instance on demand. The owner of the new data module instance becomes the WebApplication instance which means that you don't need to worry about its destruction (WebApplication will do it for you).

And that's it. You can start using it as you would in any other Delphi application just using its name "DataModule1" to reference it and everything will just work.

PS: There are several ways of achieving the same result, but I find this way particularly handy and easy to understand.
Alexandre, I understood  from original question He needs to create a datamodule attached to User Session. In your code the datamodule owner is WebApplication not UserSession.
Is  it correct?

Regards, Luiz
Reply
#4
WebApplication owns UserSession. UserSession is a custom DM to allow you to add stuff easily visually, but WebApplication is the actual "user" session to IW.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)