Solved

Database programming strategy

Posted on 2004-10-11
8
123 Views
Last Modified: 2010-04-05
What is the best practice to create delphi database application, in terms of where to put the DB component ? Maybe its quite difficult to understand my question :)

Here is the situation :

I would like to use TinyDB component (http://www.sunisoft.com/tinydb/). It is a nice very small database package. It consists of 2 components, TinyDatabase and TinyTable.

Usually, I have a main form, called "MainForm". I put all the components there.

But, if I created other forms, for example "AddDataForm", I will loose the component reference there.

What is the best way for this ? So I can still refer the components from all forms in my application.

I guess, creating a somekind of global unit, and "create" the component during initialization will do the trick. But is there any way so I can still be able to access the component "visually" in the Delphi IDE ?

I hope you understand my question :)

Thanks
0
Comment
Question by:dudup
8 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
It all depends on the size of your project, the database you're using and the target systems that will run your final application.
In general, don't use a form to store non-visible components but use a Datamodule instead. Then the visual components and non-visual components are nicely separated. Add the datamodule to the uses-clause of every form in your project that needs to access them. This way, the datamodule becomes a shared repository for everything.

The datamodule would be your global unit in this case.

Unfortunately, if you're using the Personal Edition of Delphi you might not have any data modules. In that case, either upgrade to a better Delphi version or drop those components on a form that you're not going to show. (Visible set to false, but still create it!) This non-visible form would then act like a datamodule would.
0
 

Author Comment

by:dudup
Comment Utility
Thanks for the explanation. I think I have DataModule in my Delphi. But honestly, I dont know how to use it. It is like a normal form.
0
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 100 total points
Comment Utility
It is like a normal form, except it won't be visible and you can't drop visible controls like panels or labels on it. Only non-visual components. Which makes it perfect for storing global non-visible components.

If you want to share visible components between multiple forms then you have to consider using frames instead. But that's not what you asked for. Both frames and datamodules are important things to learn though. Especially the datamodule is pretty important. Simply because it's the global unit you were thinking about but it allows you to set up the non-visual components in design-time.
0
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
you could also, of course, add the main form's unit to your secondary form's uses clause ;-)
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 5

Expert Comment

by:Voodooman
Comment Utility
Comment only

Hi

Both ideas are good (and work).

If you only have one database and a few tables - just stick em on the main form.......

Voodooman

0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
True... :-)
Personally, I really hate it when two forms are linked to each other, though. I prefer to have them all linked to a single datamodule. This just makes things a lot easier to oversee. I've seen projects with 50+ forms where every form was linked to at least a dozen other forms. When I was asked to solve some bug and add some new features in that code I just showed my boss the finger and told him that rewriting the whole thing was a lot less expensive. I didn't create the garbage and I considered the code unmaintainable.
And I was right. About two months later the new version was ready, with the new features, a new and improved layout, better stability and lots of new bugs that the users didn't know about yet. ;-) But because I'd set it up to have a lot less dependencies between the forms, it also became a lot easier to maintain, and this showed itself after the bug reports arrived. Before, every bug would take about two days to solve. Now, quite a few bugs were solved in a matter of hours and I was left with a lot of time just doing nothing. (Well, nothing... Browsing EE...)

It's better to have all your forms linked to a single datamodule than to have them all cross-linked to each other. It's easier to maintain.
0
 
LVL 5

Expert Comment

by:Voodooman
Comment Utility

Hi Workshop Alex,

I agree with what you are saying in many ways.

When I said <<If you only have one database and a few tables - just stick em on the main form.......>>

I was quite clearly referring to smaller applications.

Many small database applications revolve around the main form, so referring to it is no big deal - if the app is centered around it.

In the case of an App using TinyDB I would not expect to have 50+forms.

In bigger apps I agree that Data modules are the way to go - for the more experienced programmer.

I use data modules myself and also create in all my applications some other standard modules that I always use.

I have a usefulFunctions module (which does exactly what it says on the tin), a declares module (where I rather obviously put all object declarations, global flags etc), and add others e.g. a Registration module - where I check the Registration status of the software.

I use these and others in all my projects.

Its horses for courses.

I was suggesting to dudup that he has options, depending on the size and scope of his project.

Voodooman

0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
Well, even with small projects I tend to stay away from having forms refer to the main form. It makes it harder to re-use forms inside other projects. In the past I noticed some problems with projects made by collegues because they had too much dependencies on the main form. Parts of these projects were to be used in another project but we could not do this because of the dependancy to the main form and the enormous overkill the mainform added. Even in small applications the use of a datamodule is very sensible. In general, if a project will have 3 or more forms that are linked to the database, then I'll add a datamodule for the database stuff.

Oh, and I easily expect an application to have 20+ forms, even if it's created with TinyDB. In general, an application might have a grid form to display an overview of a table, and a second edit-dialog to edit one record from what the grid is displaying. So if, e.g. you create a simple application that is keeping scores of your favorite soccer club, you would have one grid and dialog for the clubs, one grid and dialog for the players, one grid and dialog to enter game data and scores, one grid with dialog that links players to clubs and handles transfers of players. These are already 8 forms. If you also use forms to create reports in (like with QuickReports) then you would get even more forms. And then you might also need forms to modify generic configuration data. It might grow to a database with over 15 tables, with three forms per table. That is already 45 forms, without the main form...

Thus, any small application could grow to become huge...

Also, consider frames too. A frame is also some kind of form but it's included as part of some other forms.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now