tags:

views:

275

answers:

4

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?

+10  A: 

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.

Mason Wheeler
I agree, also seen to generate menus dynamically.
Eduardo
+6  A: 

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.

Lawrence Barsanti
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.
Warren P
A: 

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.

Fabricio Araujo
+5  A: 

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.

skamradt