Recent

Author Topic: Best way to exchange data between a form and a unit  (Read 14671 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 598
Re: Best way to exchange data between a form and a unit
« Reply #120 on: February 12, 2024, 02:57:25 pm »
Code: Pascal  [Select][+][-]
  1. prCreateDirs = prUser + 1;
Twice..  that was juist stupid of me. :-[

Edit:
I looked at it now and see that i forgot a function and forgot to handle the notification.
« Last Edit: February 12, 2024, 08:21:00 pm by Hansvb »

cdbc

  • Hero Member
  • *****
  • Posts: 970
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #121 on: February 12, 2024, 03:41:23 pm »
Hi
At the moment I'm trying to rearrange your layout to something that's easier to 'navigate' for you, now and later...  8)
Hang on a bit longer...
edit: after rearranging, the code compiles and runs again, on to fase 2...
Regards Benny
« Last Edit: February 12, 2024, 04:34:56 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 970
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #122 on: February 12, 2024, 05:43:21 pm »
Hi
Ok, attached is my suggestion to a new layout of units...
Try it out, it compiles... of course  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

janasoft

  • New Member
  • *
  • Posts: 30
Re: Best way to exchange data between a form and a unit
« Reply #123 on: February 12, 2024, 07:45:15 pm »
Hi
@janasoft: You are very welcome  :)
Please speak up / chime in, if you've got questions or comments...
Regards Benny
So far I'm getting the map of all the processes. For example, when application starts:
TfrmMainView - FormCreate
  CreateObsSubscriber
    TobsSubscriber - Create
  TPresenter - Create
    CreateObsProvider
      TobsProvider - Create
    TModel – Create
      TSettings - Create
        TSettings - ReadFromFile
    TTransactionManager - Create
  TfrmMainView – set_Presenter
    TPresenter - Obj
    TPresenter - Obj
    TobsProvider - Subscribe
    TPresenter - GetStaticTexts
      TModel - FetchViewTextsFull
      TobsProvider - NotifySubscribers
        TobsSubscriber - UpdateSubscriber
          TfrmMainView - HandleObsNotify
            TfrmMainView - DoSetStaticTexts

This way I try to understand the flow of the program. For example, I found easy to understand the observer framework.
But my handicap is that I've never worked with interfaces and, for now, it's very difficult for me to imagine how to work with them, But I'm trying hard and I guess I'll get my head clear at some moment.  ;)
Anyway I have to minor doubts:
  • Why do yo use a function (Str2Pch) to do this "Result:= pchar(aStr)" or the opposite? Your comment is "compiler conversion :o)"
  • In TfrmMainView.set_Presenter yo use 'fPresenter:= TPresenter(aValue.Obj)'. Like fPresenter and aValue are of tipe TPresenter, couldn't it be written fPresenter:= aValue?

I'm still hooked on the masterclass  :)

Thanks in advance

[/list]
Lazarus 2.2.6 (rev lazarus_2_2_6) FPC 3.2.2 x86_64-win64-win32/win64

cdbc

  • Hero Member
  • *****
  • Posts: 970
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #124 on: February 12, 2024, 09:27:55 pm »
Hi
@janasoft: This mapping is so cool, it's exactly how I visualize it in my head  8-) How do you do that?
Quote
This way I try to understand the flow of the program. For example, I found easy to understand the observer framework.
But my handicap is that I've never worked with interfaces and, for now, it's very difficult for me to imagine how to work with them, But I'm trying hard and I guess I'll get my head clear at some moment.
Just take it easy, these are the 'nice' interfaces, where you don't have to wrestle the refcounting and automatic memorymanagement. I like the corba interfaces, because they are exactly what they seem to be, a contract.
Look at the IView, it's never used, but when a form or some object implements this interface, I know that I can work with it. When you work with interfaces, you don't care how they are implemented.
They provide a lot of freedom.
Quote
Why do yo use a function (Str2Pch) to do this "Result:= pchar(aStr)" or the opposite?
Tihi, because I'm too lazy to allocate memory for a pchar to send to 'the other side' and then remember to deallocate mem again... This way I can do xx.Provider.NotifySubscribers(prStatus,nil,str2pch('Hello World'));
the compiler doesn't allow me to do: (prStatus,nil,pchar('Hello World')); because a pchar doesn't allocate memory, you can take the address of.
Quote
In TfrmMainView.set_Presenter yo use 'fPresenter:= TPresenter(aValue.Obj)'. Like fPresenter and aValue are of tipe TPresenter, couldn't it be written fPresenter:= aValue?
Look closer my friend, aValue is of type 'IPresenter' and you can't typecast by assignment backwards with interfaces(they don't implement anything), thus all my interfaces surfaces the underlying object in the function 'Obj' and that you /can/ typecast directly  :D Me Likey ;)
Quote
I'm still hooked on the masterclass
Glad to hear that, makes it all worthwhile  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 970
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #125 on: February 12, 2024, 11:27:16 pm »
Hi
Looking at the code from the lates upload {basic_mvp_layout}, I see some places that need tightening and tuning... So I'll do a code-review/overview starting tomorrow and post an updated cleaner, more stringent 'template' later.
Sorry for the inconvenience  :-[
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Hansvb

  • Hero Member
  • *****
  • Posts: 598
Re: Best way to exchange data between a form and a unit
« Reply #126 on: February 13, 2024, 07:19:17 am »
Don’t worry, i am all ready verry greatfull for what you did so far.

janasoft

  • New Member
  • *
  • Posts: 30
Re: Best way to exchange data between a form and a unit
« Reply #127 on: February 13, 2024, 12:01:52 pm »

Hi
Quote
@janasoft: This mapping is so cool, it's exactly how I visualize it in my head  8-) How do you do that?
I'm so glad to hear that
It is a rudimentary, but very simple system taken from the wiki. It helps me a lot when I do my own programs and, most of all, when I study programs of other people.
I'm sure there will be better systems so, if anyone knows any, please let me know  :)

In the uses clause of one file you must include
Code: Pascal  [Select][+][-]
  1. {$IFDEF debug} LazLogger, {$ELSE}  LazLoggerDummy,{$ENDIF}
In the rest of the units
Code: Pascal  [Select][+][-]
  1. {$IFDEF debug} LazLoggerBase, {$ELSE}  LazLoggerDummy,{$ENDIF}
And in any method you want to observe
Code: Pascal  [Select][+][-]
  1. function TSearcher.CheckRecordCount: integer;
  2. begin
  3.   DebugLnEnter({$I %FILE%}, ' - ', {$I %CURRENTROUTINE%}); or   DebugLnEnter(ClassName, ' - ', {$I %CURRENTROUTINE%});  
  4.    ..//..
  5.   DebugLnExit();
  6. end;
To define automatically the DEBUG variable only in Debug build mode I use
Code: Pascal  [Select][+][-]
  1. {$IFOPT D+} {$DEFINE DEBUG} {$ENDIF}
Normally you have 15 spaces of indentation. If you have a lot of nested methods and need, for example 50 spaces, you can include on the Oncreate method of the main form:
Code: Pascal  [Select][+][-]
  1. DebugLogger.MaxNestPrefixLen:=50;

Quote
Look closer my friend, aValue is of type 'IPresenter' and you can't typecast by assignment backwards with interfaces(they don't implement anything), thus all my interfaces surfaces the underlying object in the function 'Obj' and that you /can/ typecast directly  :D Me Likey ;)
My mistake. I didn´t look closer  :)

Regards. Ricardo
Lazarus 2.2.6 (rev lazarus_2_2_6) FPC 3.2.2 x86_64-win64-win32/win64

cdbc

  • Hero Member
  • *****
  • Posts: 970
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #128 on: February 13, 2024, 01:03:54 pm »
Hi
@janasoft: Thank you, I'll try that sometime  :)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Hansvb

  • Hero Member
  • *****
  • Posts: 598
Re: Best way to exchange data between a form and a unit
« Reply #129 on: February 18, 2024, 08:07:32 pm »
Hi,

I plod along with difficulty.  :-[

I want to create a file. Via start transaction I finally end up in the Model. Is it allowed to call a unit "CreateFile" there without creating an interface.
So just a
Code: Pascal  [Select][+][-]
  1. creFile := TCreateFiles.create;
  2. creFile.<check, prepare, etc functions>;
  3. creFile.CreateFile;
  4. creFile.Free;


Edit:
I guess i have to follow the same path as you showed for the settings. I'll try that first.
« Last Edit: February 18, 2024, 08:15:29 pm by Hansvb »

Hansvb

  • Hero Member
  • *****
  • Posts: 598
Re: Best way to exchange data between a form and a unit
« Reply #130 on: February 18, 2024, 08:22:13 pm »
How should you treat a data module? As a view?

cdbc

  • Hero Member
  • *****
  • Posts: 970
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #131 on: February 18, 2024, 09:02:17 pm »
Hi
Good to have you back  :)
Quote
Is it allowed to call a unit "CreateFile" there without creating an interface.
Yes, in the model everything data & logic is allowed. It's all up to you, how the model generates its output ...and how easy or hard to do, it is...  :D
Quote
How should you treat a data module? As a view?
Now this one is a hard one, because you know the law "NO visual stuff coupled in the model!" And I assume you're talking about /visual/ database-access?!?
The actions of talking to a database to get/give data, belong in the 'Model'...  :(
So, how do we solve this conundrum?!? I'd use an interface defined in vwmain.intf and the corresponding class implemented in the datamodule that the model can talk to. That way, should you change database, you just write a new proxy-class to implement the interface. The Model only knows about the interface!
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

cdbc

  • Hero Member
  • *****
  • Posts: 970
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #132 on: February 18, 2024, 09:11:38 pm »
Hi
Hmmm, does the datamodule have to be /visual/? The reason I ask is, I haven't used a visual datamodule in 25 years... I sure use the components behind the scenes, in units I just call into..., heck, sometimes I have TSqlComponents in my model for convenience & speed.
The fcl-db components don't need a datamodule or a form...
Regards Benny
« Last Edit: February 18, 2024, 09:15:38 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Hansvb

  • Hero Member
  • *****
  • Posts: 598
Re: Best way to exchange data between a form and a unit
« Reply #133 on: February 19, 2024, 05:12:43 pm »
Quote
Good to have you back
:D
I wasn't away but I have less time. And to be honest, I was not sure if this isn't too much of a good thing for my little tools. But for now, I want to see if I can pull it off.

You're right about the data module. I don't think I really need it. It's just an easy component. For my next step, all I need is a query and a transaction. I can also create them in the code.

cdbc

  • Hero Member
  • *****
  • Posts: 970
    • http://www.cdbc.dk
Re: Best way to exchange data between a form and a unit
« Reply #134 on: February 19, 2024, 06:35:54 pm »
Hi
Quote
And to be honest, I was not sure if this isn't too much of a good thing for my little tools.
Weeeellllll, _you_ yourself opened 'Pandora's Box'  :P
Nah, all laughs aside, I showed you the simple stuff first, then you asked about size of units and splitting code into units, and I thought, 'Why the He** Not', and showed you how easy it is to extend mvp.
For small utilities, you can /wing it/ with a Form(view), a TPresenter, a TModel, a decl/types unit & the obs.prosu.pas _or_ a console program + TModel  :D
Quote
For my next step, all I need is a query and a transaction.
Easy Peasy, keep it all in methods of TModel, that you can easily call... -> Job Done  8)
The important thing is: "Keep the visual stuff separated from data & logic"
Regards Benny
« Last Edit: February 19, 2024, 06:57:54 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018