Forum > General
Just to confirm my approach
egsuh:
Hi, I just want your ideas on my approach. I wrote an application which uses several forms, and one datamodule. Most of the forms have to access different datasets in the datamodule.
I did several trial and errors, and finally settled on following approach.
1) in each TForm definition module, I add a TDataSet variable under public section.
2) And assign TDataSet object in the datamodule to the variable within "program" unit, before Application.Run, like following.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit fselectproject_5; interfaceuses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrlsl type TfProjectChoice = class(TForm) bbrnLogin: TBitBtn; bbtnCancelLogin: TBitBtn; private procedure RefreshProjectList; public uid: string; DataSet: TDataSet; // <== this is TDataSet variable. end; var fProjectChoice: TfProjectChoice;.... //--------------- program aq01editor; {$mode delphi}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Interfaces, // this includes the LCL widgetset Forms, fselectproject_5, daq_fb_5, ... ; {$R *.res} begin RequireDerivedFormResource:=True; Application.Initialize; Application.CreateForm(TAQFB4, AQFB4); Application.CreateForm(TfProjectChoice, fProjectChoice); Application.CreateForm(TfQEdit, fQEdit); Application.CreateForm(TfoNewQDef, foNewQDef); fProjectChoice.DataSet := AQFB4.dsProjects; // <== Assign the dataset here. Application.Run;end.
In this way, I can use the forms and datamodule almost independently. Up to now this works fine, without any problem. But not sure there could be any problem in this approach.
Want your advice.
sash:
To follow a "decoupling approach" I'd rather move Dataset assignement code from main (program) unit to each corresponding form's Constructor/OnCreate.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TSomeForm.FormCreate(Sender : TObject);begin Dataset := ADataModule.SomeDataSet; // <== Assign the dataset here. end;
Soner:
I would not put datasets in datamodule and use same datesets in other forms.
I did late 90's, because I read it in Delphi because and get problems after my program was bigger and I need to change something in database.
Today I put in datamodule only shared objects like connections (i.e. TIBConnecion, TZConnection), TImageList for all forms.
It is so easy to change database or tables.
egsuh:
Hmmmm..
I see. My approach does not seem to be a good practice.
Problem is that I'd like to use a same unit in multiple applications with different database connections.
I'll think it over.
Thank you for your comments.
mangakissa:
--- Quote from: Soner on November 09, 2018, 05:56:05 pm ---I would not put datasets in datamodule and use same datesets in other forms.
I did late 90's, because I read it in Delphi because and get problems after my program was bigger and I need to change something in database.
Today I put in datamodule only shared objects like connections (i.e. TIBConnecion, TZConnection), TImageList for all forms.
It is so easy to change database or tables.
--- End quote ---
Datamodule is built to separate data form the GUI. That's why datasets en connections will be placed on that form.
When your form must be updated or somehing, it's easier to connect the datamodule again.
I've never heard such thing on Delphi sites
The sugestion of sash is a right thing to do
Navigation
[0] Message Index
[#] Next page