09-13-2019, 02:05 PM
(09-09-2019, 09:11 AM)Alexandre Machado Wrote: to create a new data module in any Delphi (including IW) application: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.
- 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.
Is it correct?
Regards, Luiz