| View previous topic :: View next topic |
| Author |
Message |
Roland Guest
|
Posted: Mon Nov 20, 2006 5:33 am Post subject: how to make mainform and datamodule not know each other |
|
|
Hi:
We have an application using interbase and SQL server. On main form, every
menu item is added during design time and assigned unique number on their
tag. There is one table holding these tags and captions.
Now the menu items can be enabled or disabled depanding on users rights. My
question is how to do this process while the main form and datamodule should
not know each other? Thanks.
Roland |
|
| Back to top |
|
 |
Craig Guest
|
Posted: Mon Nov 20, 2006 5:43 am Post subject: Re: how to make mainform and datamodule not know each other |
|
|
| Quote: |
We have an application using interbase and SQL server. On main form, every
menu item is added during design time and assigned unique number on their
tag. There is one table holding these tags and captions.
Now the menu items can be enabled or disabled depanding on users rights. My
question is how to do this process while the main form and datamodule should
not know each other? Thanks.
|
Create a decoupler class that goes in between. This is how I do similar
things (code not tested)
type
TGetMenuItemsEvent = procedure(Sender: TObject; Items: TList) of object;
TDecoupler = class(TObject)
public
function GetMenuItems(const User: string): TList;
property OnGetMenuItems: TGetMenuItemsEvent read FOnGetMenuItems
write FOnGetMenuItems;
end
function GetMenuItems(const User: string): TList;
begin
Result := TList.Create;
if Assigned(FOnGetMenuItems) then
FOnGetMenuItems(Self, Result);
end
In the data module have something like
Decoupler.OnGetMenuItems := GetMenuItems; // This is a method that
populates the list
In the main form just call the Decoupler.GetMenuItems method.
I hope that makes sense.
Craig. |
|
| Back to top |
|
 |
Marc Rohloff [TeamB] Guest
|
Posted: Mon Nov 20, 2006 6:34 am Post subject: Re: how to make mainform and datamodule not know each other |
|
|
On Mon, 20 Nov 2006 10:43:55 +1100, Craig wrote:
| Quote: | Create a decoupler class that goes in between. This is how I do similar
things (code not tested)
|
Another alternative would be to have an interface to define what the
form needs and have the datamodule implement the interface.
--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com |
|
| Back to top |
|
 |
Roland Guest
|
Posted: Mon Nov 20, 2006 9:14 am Post subject: Re: how to make mainform and datamodule not know each other |
|
|
One more issue:
We need udpate database(insert new tag id and caption into table) if we
added new menuitem at design time. Would be better to pass menu items to
TDecoupler or use other ways? Thanks.
Roland |
|
| Back to top |
|
 |
Roland Guest
|
Posted: Mon Nov 20, 2006 9:14 am Post subject: Re: how to make mainform and datamodule not know each other |
|
|
Thanks Craig:
It works perfect.
Roland
"Craig" <craigvn (AT) gmail (DOT) com> wrote in message
news:4560ec36$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
We have an application using interbase and SQL server. On main form,
every menu item is added during design time and assigned unique number on
their tag. There is one table holding these tags and captions.
Now the menu items can be enabled or disabled depanding on users rights.
My question is how to do this process while the main form and datamodule
should not know each other? Thanks.
Create a decoupler class that goes in between. This is how I do similar
things (code not tested)
type
TGetMenuItemsEvent = procedure(Sender: TObject; Items: TList) of object;
TDecoupler = class(TObject)
public
function GetMenuItems(const User: string): TList;
property OnGetMenuItems: TGetMenuItemsEvent read FOnGetMenuItems write
FOnGetMenuItems;
end
function GetMenuItems(const User: string): TList;
begin
Result := TList.Create;
if Assigned(FOnGetMenuItems) then
FOnGetMenuItems(Self, Result);
end
In the data module have something like
Decoupler.OnGetMenuItems := GetMenuItems; // This is a method that
populates the list
In the main form just call the Decoupler.GetMenuItems method.
I hope that makes sense.
Craig.
|
|
|
| Back to top |
|
 |
Roland Guest
|
Posted: Mon Nov 20, 2006 9:14 am Post subject: Re: how to make mainform and datamodule not know each other |
|
|
Thanks Marc:
| Quote: | Another alternative would be to have an interface to define what the
form needs and have the datamodule implement the interface.
|
I am not good at interface. Should the mainform inherited from interface? Do
you have any simple sample? Thanks.
Roland |
|
| Back to top |
|
 |
Wayne Niddery [TeamB] Guest
|
Posted: Mon Nov 20, 2006 9:36 pm Post subject: Re: how to make mainform and datamodule not know each other |
|
|
Roland wrote:
| Quote: |
Now the menu items can be enabled or disabled depanding on users
rights. My question is how to do this process while the main form and
datamodule should not know each other? Thanks.
|
My rule has always been: Datamodules must not know about any form (no form
unit should ever appear in the uses clause of a datamodule). However there
is no reason for a form to not know about one or more datamodules. Thus the
datamodule can provide public methods that can be called by a form to find
out such such information.
If you really want the form to not have direct links to the datamodule, then
create a class that handles specific things, in this case, user rights. The
class uses a datamodule to get the needed info and the form talks to the
class.
--
Wayne Niddery - Winwright, Inc (www.winwright.ca)
"A man is likely to mind his own business when it is worth minding,
when it is not, he takes his mind off his own meaningless affairs by
minding other people's business." - Eric Hoffer |
|
| Back to top |
|
 |
Charles McAllister Guest
|
Posted: Mon Nov 20, 2006 9:59 pm Post subject: Re: how to make mainform and datamodule not know each other |
|
|
Roland wrote:
| Quote: | Thanks Marc:
Another alternative would be to have an interface to define what the
form needs and have the datamodule implement the interface.
I am not good at interface. Should the mainform inherited from interface? Do
you have any simple sample? Thanks.
Roland
here's some sample code: |
http://www.marcocantu.com/code/md6htm/FormIntf.htm
in this code, see how the main form can invoke Load on all forms that conform to the interface:
procedure TFormMain.btnLoadClick(Sender: TObject);
var
i: Integer;
begin
for i := 0 to Screen.FormCount - 1 do
if Supports (Screen.Forms [i], IFormOperations) then
(Screen.Forms [i] as IFormOperations).Load;
end; |
|
| Back to top |
|
 |
Marc Rohloff [TeamB] Guest
|
Posted: Tue Nov 21, 2006 8:14 am Post subject: Re: how to make mainform and datamodule not know each other |
|
|
On Mon, 20 Nov 2006 09:59:54 -0600, Charles McAllister wrote:
It would be better to get the interface during the supports call
instead of doing the interface check twice.
procedure TFormMain.btnLoadClick(Sender: TObject);
var
i: Integer;
fo:IFormOperations;
begin
for i := 0 to Screen.FormCount - 1 do
if Supports (Screen.Forms [i], IFormOperations, fo) then
fo.Load;
end;
--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com |
|
| Back to top |
|
 |
Roland Guest
|
Posted: Wed Nov 22, 2006 5:42 am Post subject: Re: how to make mainform and datamodule not know each other |
|
|
Thanks Wayne:
| Quote: | However there is no reason for a form to not know about one or more
datamodules. Thus the datamodule can provide public methods that can be
called by a form to find out such such information.
|
We have duplicate applications linked to two databases(Interbase & SQL
server). It causes maintenance headech with duplicate codes. To minimise the
mainenance, I like to make all forms without DBAware components can be
shared for two applications which I used function calls.
I have read an article "Simple Application Frameword for VCL" (forgot who
written from Developer Express, by Craig Stuntz?). I am quite impressed with
that technique.
So I guess it wil make me more confortable to let Form unknowing DataModule
rather then calling DataModule1.DoSomthing().
Roland |
|
| Back to top |
|
 |
Roland Guest
|
Posted: Wed Nov 22, 2006 5:43 am Post subject: Re: how to make mainform and datamodule not know each other |
|
|
Thanks Marc and Charles:
I will have a try.
Roland |
|
| Back to top |
|
 |
Nando Dessena Guest
|
Posted: Wed Nov 22, 2006 3:37 pm Post subject: Re: how to make mainform and datamodule not know each other |
|
|
Roland,
| Quote: | I have read an article "Simple Application Frameword for VCL" (forgot who
written from Developer Express, by Craig Stuntz?). I am quite impressed with
that technique.
So I guess it wil make me more confortable to let Form unknowing DataModule
rather then calling DataModule1.DoSomthing().
|
Whether you use a data module or anything else, the key here is
encapsulation. If you want to support several database kinds with a
single code base you've got a longer way to go than simply put specific
component sets in separate data modules and switch among them. You need
to encapsulate your data access layer completely, including data type
mapping and SQL query building.
Ciao
--
Nando Dessena
Ethea |
|
| Back to top |
|
 |
|