Board index » delphi » To datamodule or to not datamodule

To datamodule or to not datamodule

Hello,

After many years of not using datamodules have started to use them in my
last few app.  Afterall it makes sense to put your data model into one unit,
and handle all the consistency checks and errors there.

But i keep coming up against the problems of reflecting dataset state in my
forms.  For instance if the dataset is empty maybe i disable a few
buttons... or when inserting i have to change a few menu items...

So i use to link all this code to the events of the dataset i.e.
OnNewRecord, OnChangeState etc...  BUT now i find that i must put this code
in my Data Module... and here i only want to have put data related code and
nothing to do with my forms.

If i move the datasources to my forms unit then I end up having trouble
using master datail relationships because then i have to "use" the form unit
in the data module so i can link the datasets.

Is there a better way of doing all this?

/Richard

 

Re:To datamodule or to not datamodule


Quote
> If i move the datasources to my forms unit then I end up having trouble
> using master datail relationships because then i have to "use" the form
unit
> in the data module so i can link the datasets.

Use a different datasource for linking and master/ detail relationships.

Datasource to pipe data to user interface goes on the form.

Datasource to provide master/detail link goes on the datamodule.

Dean Bisseker
Leeds, England

Re:To datamodule or to not datamodule


There is another way you can control your OnNewRecord and those kind of
events by rerouting them to procedures in the form.  When you load the form
you can say

DataModule.ADOTable1.OnNewRecord := MyProcedure;

and in the form you must have a procedure called MyProcedure with the same
parameters as the OnNewRecord procedure in the datamodule.

ect
MyProcedure must look like this
MyProcedure(DataSet: TDataSet)
as would ADOTable1 in the datamodule looks like
ADOTable1NewRecord(DataSet: TDataSet)

Hope it helps
JB

Quote
"Richard Bibby" <richard.bi...@wilnor.se> wrote in message

news:3ee20e67$1@newsgroups.borland.com...
Quote
> Hello,

> After many years of not using datamodules have started to use them in my
> last few app.  Afterall it makes sense to put your data model into one
unit,
> and handle all the consistency checks and errors there.

> But i keep coming up against the problems of reflecting dataset state in
my
> forms.  For instance if the dataset is empty maybe i disable a few
> buttons... or when inserting i have to change a few menu items...

> So i use to link all this code to the events of the dataset i.e.
> OnNewRecord, OnChangeState etc...  BUT now i find that i must put this
code
> in my Data Module... and here i only want to have put data related code
and
> nothing to do with my forms.

> If i move the datasources to my forms unit then I end up having trouble
> using master datail relationships because then i have to "use" the form
unit
> in the data module so i can link the datasets.

> Is there a better way of doing all this?

> /Richard

Re:To datamodule or to not datamodule


Richard,

Some tips and tricks...
http://community.borland.com/article/0,1410,20563,00.html

Good luck,
krf

Quote
"Richard Bibby" <richard.bi...@wilnor.se> wrote in message

news:3ee20e67$1@newsgroups.borland.com...
Quote
> Hello,

> After many years of not using datamodules have started to use them in my
> last few app.  Afterall it makes sense to put your data model into one
unit,
> and handle all the consistency checks and errors there.

> But i keep coming up against the problems of reflecting dataset state in
my
> forms.  For instance if the dataset is empty maybe i disable a few
> buttons... or when inserting i have to change a few menu items...

> So i use to link all this code to the events of the dataset i.e.
> OnNewRecord, OnChangeState etc...  BUT now i find that i must put this
code
> in my Data Module... and here i only want to have put data related code
and
> nothing to do with my forms.

> If i move the datasources to my forms unit then I end up having trouble
> using master datail relationships because then i have to "use" the form
unit
> in the data module so i can link the datasets.

> Is there a better way of doing all this?

> /Richard

Re:To datamodule or to not datamodule


Another solution is to use the Observer pattern.  Using a TList isntance in
the datamodule, you can attach and detach observers to the datamodule.  When
a particular state changes on any component that needs to notify the
observers of the change, run through the list and call a custom notification
event implemented in the observer class.

For more information, refer to the book "Design Patterns" by Erich Gamma,
Richard Helm, Ralph Johnson, and John Vlissides (published by
Addison-Wesley).  Here is a site where you can find the information in
electronic format...

http://sern.ucalgary.ca/courses/SENG/609.04/W98/lamsh/observerLib.html

Scott

Quote
"Richard Bibby" <richard.bi...@wilnor.se> wrote in message

news:3ee20e67$1@newsgroups.borland.com...
Quote
> Hello,

> After many years of not using datamodules have started to use them in my
> last few app.  Afterall it makes sense to put your data model into one
unit,
> and handle all the consistency checks and errors there.

> But i keep coming up against the problems of reflecting dataset state in
my
> forms.  For instance if the dataset is empty maybe i disable a few
> buttons... or when inserting i have to change a few menu items...

> So i use to link all this code to the events of the dataset i.e.
> OnNewRecord, OnChangeState etc...  BUT now i find that i must put this
code
> in my Data Module... and here i only want to have put data related code
and
> nothing to do with my forms.

> If i move the datasources to my forms unit then I end up having trouble
> using master datail relationships because then i have to "use" the form
unit
> in the data module so i can link the datasets.

> Is there a better way of doing all this?

> /Richard

Other Threads