Fog Creek Software
Discussion Board

Using DataModule

Using datamodule is simple way to reduce the number of datasources in application. When I need to open and close the datasource? After openning the datamodule or after opening the forms that use the datasource? And if I opened the datasource in form, should I close it then close that form?

Andrew Yashchuk
Sunday, October 27, 2002

You should open datasources when they are needed and close them when you're done with them. It depends on your application design, but in your case, when a form opens and closes

Paul Sjoerdsma
Tuesday, October 29, 2002

Thank you for reply!
Is it true for MDI application?
If I've opened two forms using the same datasource from datamodule, can I close it from any of the form?

Andrew Yashchuk
Tuesday, October 29, 2002

Using Data Modules is always a good idea. I have two remarks though:

1. You should use data modules dynamically if you're using dynamically created forms. It is quite ok for two instances of a form to use the same instances of a dataset, but you'll see that the cursor moves in all forms at the same time. This may or may not be what you want.

2. The Delphi documentation states that you put Data Sources on the Data Module, and that you connect all your data aware controls to that datasource. This is a misconception that has survived 5 iterations of Delphi. (I don't know if they still try to persuade you to do this in Delphi 7 since I don't have that version yet) In case you're wondering, a Data Source is used to control the user interface elements. You want to write an OnStateChange if you need to enable or disable buttons or something like that. Leave the operations on the data (business logic, domain logic) on the Data Module.

Also, if you're creating your forms dynamically, you'll WANT to have the Data Source there, since it takes less effort to connect one Data Source to a (dynamically created) Data Set, than it does to connect every little edit box to the correct Data Source.

Comments are welcome.


Dave Van den Eynde
Wednesday, October 30, 2002

Thank you for reply Dave!
Using the dynamically created datamodule is the way to keep all business logic in the one place. 

Andrew Yashchuk
Wednesday, October 30, 2002

*  Recent Topics

*  Fog Creek Home