Jump to content

How to give correct link to created instance?


elGringo

Recommended Posts

Having in MainForm

procedure TMainForm.UniFormCreate(Sender: TObject);
begin

FGenerateUniqueCode:=TGenerateUniqueCode.Create(Self);

end;

in other module

procedure TGenerateUniqueCode.DataModuleCreate(Sender: TObject);
begin
//
 if (Sender is TMainForm) then

 begin

 MainForm_UnitVar:=(Sender as TMainForm);
 qSelectAllCodes.Connection:=MainForm_UnitVar.DBConnection.FDConnection;
 qInsertNewCode.Connection:=MainForm_UnitVar.DBConnection.FDConnection;
 end;




end;

 

How to give correct link to TMainForm? On Debug it acts like it doesn' see it, but in TMainForm.UniFormCreate i give link to Self...

Link to comment
Share on other sites

Something like this

 

DataModule:

function TGenerateUniGuiCode.GenereteSomeCode : string;
var
  SomeCode: string;
begin
  SomeCode:= 'SomeCode';
  Result:= SomeCode;
end;

MainModule:

procedure TMainForm.UniFormCreate(Sender: TObject);
var
  CodeGenerator: TGenerateUniqueCode;
begin
  CodeGenerator: TGenerateUniqueCode.Create(Self);
  FGenerateUniqueCode:= CodeGenerator.GenereteSomeCode; //function returns string and saves it to property  
end;
Link to comment
Share on other sites

Ok, maybe I understood. I usually hold FDConnection inside DataModule and not MainForm. In your case I would create setup function right after creating DataModule. I give to this setup method pointers of components as parameters from main form that i'll be using in datamodule.

procedure TGenerateUniGuiCode.SetUp(ASomeConnection: TFDConnection; AAnotherConnection: TFDConnect);
begin
  //Connection1 and Connection2 are TFDConnetion
  Connection1:= ASomeConnection;
  Connection2:= AAnotherConnection;
end;
procedure TMainForm.UniFormCreate(Sender: TObject);
var
  CodeGenerator: TGenerateUniqueCode;
begin
  FGenerateUniqueCode: TGenerateUniqueCode.Create(Self);
  FGenerateUniqueCode.SetUp(ASomeConnection, AAnotherConnection);
end;
Link to comment
Share on other sites

Yes! 

 You created instance of your data module and use it in MainModule - that's normal situation.

 

 

In my case i need to give link to MainForm to instance created in uMainForm. My temporary decision is

 

uGenerateUniqueCode

...
type
  TGenerateUniqueCode = class(TDataModule)
    qSelectAllCodes: TFDQuery;
    qInsertNewCode: TFDQuery;
    procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }

    FSessionID:integer;

    FMainForm:TObject; /// <<< Look here, this field to give link to instance of MainForm
...
  public
    { Public declarations }

    function GenerateUniqueCode:integer;

    property SessionID:integer read FSessionID write FSessionID;
    property MainForm:TObject read FMainForm write FMainForm; // Look here, this property to give link to instance of MainForm


 

uMainForm

...
FGenerateUniqueCode:=TGenerateUniqueCode.Create(Self);
FGenerateUniqueCode.MainForm:=Self; // <<< Giving link here
...

So, it works, but it is strange that it doesn't work through Sender param like I wrote before !

Link to comment
Share on other sites

I think that You should re-organize architecture.

If MainForm contains something important - maybe You can make simple factory in MainModule which hold this data together with session object. So during MainForm start You make something like this: MainModule.Factory.Register(ThisSession, MyData). Next other objects (forms) can call MainModule.Factory.GetData(ThisSession). At the end Unregister should be of course.

This is theory - I don't know Your needs.

 

Regards :)

Link to comment
Share on other sites

ok, little program for SMS confirmation, on MainForm i have tuniedits and tunibuttons like this (on the picture attached, all in Russian but nevermind for the question)

 

I need info from tuniedits that I need in other module, so I need access to tuniedits on MainForm. I usually just give link to instance of main form and use proper info.

 

So what would be decision here? Maybe to put all controls on Separate frame?

 

post-2378-0-07740800-1480417559_thumb.jpg

Link to comment
Share on other sites

It is not so happy when You take action results from edit fields. You should have this data in memory structure and singleton factory for holding this datas in MainModule.

After action execute data is pushed to factory and from then it is available for everyone (for edit fields and other system elements).

Link to comment
Share on other sites

So u mean send info to Special Structure on MainModule and then use it?

Just there is no books or manual about such things and I used usual coding style as in Delphi.

 

As I can understand you that's needed cause all forms in UniGUI have shorter life time than MainModule ? So info will be more safe?

Link to comment
Share on other sites

Hmm I'm just beginner in UniGui but maybe I will have luck :)

You have two DataModules in project - MainModule and ServerModule. MainModule is one for session and ServerModule is one for whole application - simple.

So when Your data is important cross sessions that You should put it in ServerModule. When You need data access only for current session You should go to MainModule.

Link to comment
Share on other sites

That's clear for the moment.

 

So, forms are just interface elements that collect data and send it to MainModule which is 1 per session or to Server Module which is 1 per App.

 

Data from forms should be collected in special Structures in Memory which are like mirror to form elements.

 

The same is with connection to DB - should be 1 per App, so it should be in ServerModule for example.

 

So, thats clear now.

 

Thank you, dear all !!!

Link to comment
Share on other sites

That is very intersting moment here ! 

 

Initially I put FDConnection to MainForm or special unit for FDConnection that I called in Main form and connected to DB.

 

I use FireDAC and MySQL so I just put libmysql.dll to EXE folder and it is OK if I use Stand Alone Mode.

 

Yesterday I tried to convert my EXE to DLL  and started UniAPP on IIS - so UniAPP could not find libmysql.dll

I tried to put it to the folder where IIS start itself, but no effect, then to system32, also no effect,

 

Then I tried to use FDManager - who knows that is special component in FireDAC to set up persistent connections, but nevermind here - it should be used 1 per APP, so if to put FDManager and other FD components to MainForm - than it errors that 1 FD Manager should be used...

 

Than I put all FD components to Server Module - and it works - and now you say Never put it to Server Module ))) 

 

It is kind of quest for me.

Link to comment
Share on other sites

 

Yesterday I tried to convert my EXE to DLL  and started UniAPP on IIS - so UniAPP could not find libmysql.dll

I tried to put it to the folder where IIS start itself, but no effect, then to system32, also no effect,

 

Do you try c:\windows\SysWow64\ if your dll is 32bits.

Have you got your FDGUIxWaitCursor.screencursor set to  grcnone.

I'm using unigui with firedac and isapi on iis without problem

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...