separate dataset instances using datamodules in delphi

前端 未结 4 1924
小蘑菇
小蘑菇 2021-01-14 12:09

I am using Delphi6 and have a data module with an ADO DataSet which is used by two forms, formA and FormB. Each form has a Datas

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 13:05

    As you said you need seperate instances, then my solution would be to have a Datamodule variable in each form declaration:

    TForm1 = class(TForm)
    ...
    private
      fDatamodule : TDatamodule1;
    ...
    end;
    
    procedure TForm1.FormCreate(Sender : TObject)
    begin
      fDatamodule := TDatamodule1.Create(self);
      MyDatasource.Dataset := fDatamodule.MyDataset;
    end;
    

    (repeat for Form2 etc)

    You have the same datamodule, instanciated twice and thus completely seperate from each other, but utilising the same business logic in each form.

    Whilst on the subject, ensure your datamodule code does not make reference to either form. This is bad practice.

提交回复
热议问题