DataModule created before main form [closed]
Asked Answered
E

5

6

Some database application developers prefer to create a data module before main form by editing the project source file like this

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TDM, DM);
  Application.CreateForm(TMainForm, MainForm);
{...}
  Application.Run;
end.

The question is - why? What are pros and contras?

Erhart answered 14/1, 2010 at 15:34 Comment(3)
There is no OnCreate handler or else. MainForm may reference datasources declared in datamodule, but afaik they are successfully resolved without changing the creation order.Erhart
This doesn't mean you have to edit the project source. The creation order of datamodules and forms can be changed in the project options dialog. The first form created will be the main form, but there can be datamodules created before that. You may also have a look at my answer to this question: #383027Hydrangea
to Uwe Raabe - I know there are a lot of options. I myself sometimes compile a GUI unit test project without any form.Erhart
G
17

The obvious reason would be if the main form needs the Data Module for its setup. For example, if there's something in there that the main form references in its OnCreate, then of course the data module would have to be ready first.

Otherwise, it doesn't really matter.

Greedy answered 14/1, 2010 at 15:44 Comment(1)
I agree, also seen to generate menus dynamically.Methodism
V
6

I agree with Mason's answer because it explains why people may do this. However, I believe this is a bad approach because it hides the dependency in code that is maintained by the IDE. In my opinion, the data module should be removed from the auto create list and it should be created in the main form's OnCreate method.

Vivisectionist answered 14/1, 2010 at 16:8 Comment(1)
I disagree that the data module should be created by the MainForm's OnCreate method and think that is bad style. Instead, I would create main form first ALWAYS, and data module second, and I would not place ANY code inside the main form that depends on ANY other form, during it's creation, or destruction. Instead, I would write separate initialization functions, and invoke them by code that I write, to happen at some time. I don't like FormCreate events that do much other than create local objects that are private/protected fields inside the form. Anything else should not be in FormCreate.Gothard
C
5

There really are two camps on this one, and both are correct.

The first lets the application manage the life of each form/data module. In this scenario, if the main form uses the data module, then it must be created before it can be used. This works fine for small applications, but there is a loading overhead when you get to larger applications with multiple forms...however once the application is loaded then displaying a form is almost instant since its already created in memory. Because each form/resource is already created there also is a large memory hit upon running the application. This method is the default one that Delphi "leads" you too as you add new forms/data modules to the application. If you don't use the datamodule in the OnCreate of the mainform, then it can be lower in the create order as it won't be invoked until after the Application.Run is launched.

The second camp wants to handle the creation AND destruction of each form/data module itself (generally for all forms other than the MainForm). The advantage to this method is that the application will load faster, and consume less memory immediately upon startup. Generally in this scenario, it is the main form (or other forms) which completely manage the life-cycle of each form/data module they use. This method works best for larger applications with many forms.

Checkerbloom answered 14/1, 2010 at 17:10 Comment(0)
M
0

Just because it's the laziest way to assure the DataModule content are available to MainForm. If you have just one DataModule, there's no trouble.

Mentholated answered 14/1, 2010 at 16:16 Comment(0)
P
0

MainForm is not displayed before all other components are created. So basically you will wait in both cases, weather data-module is created first or not. If your grid is on the main form then you might get in trouble when trying to use something like this from the data-modules table (or query's) event (after-open):

cxGrid1DBTableView1.Controller.TopRowIndex :=0;
cxGrid1DBTableView1.DataController.FocusedRowIndex := 0;

Reason is obvious: The grid isn't created yet .. I must admit, I also create datamodule first. But there are reasons,like the one I just described, when it's not practical to do that.

Philippic answered 24/2, 2014 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.