Recent

Author Topic: mvvm experimenting  (Read 1155 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 715
mvvm experimenting
« on: September 23, 2023, 09:15:57 am »
Hi,

I am experimenting with MVVM and have come across the following.

View is the main form. Here I have the Create file button. The actual creation of the file will probably have to be done in the Model. The view model is a pass along? While creating the file, I want to put an informative text in the status bar of the main form. How do I return this text to the view? Or am I separating too much now?

Code: Pascal  [Select][+][-]
  1. Form (view):
  2. procedure TFrmMain.MenuItemProgramNewFileClick(Sender: TObject);
  3. begin
  4.   vmFrmMain.NewDbFile;
  5. end;
  6.  

View model:

Code: Pascal  [Select][+][-]
  1. procedure TVMFormMain.NewDbFile;
  2. var
  3.   tmp : String;
  4. begin
  5.   FAppDbCreate.SelectDbFilePath;
  6. end;
  7.  

Model:
Code: Pascal  [Select][+][-]
  1. function TAppDbCreate.SelectDbFilePath: Boolean;
  2. var
  3.   saveDialog : TSaveDialog;
  4. begin
  5. // Screen.Cursor := crHourGlass;
  6.   how can you send a text back to the main form here? <--------------
  7.  
  8. // Screen.Cursor := crDefault;
  9. end;




cdbc

  • Hero Member
  • *****
  • Posts: 1664
    • http://www.cdbc.dk
Re: mvvm experimenting
« Reply #1 on: September 23, 2023, 10:10:43 am »
Hi
Easy peasy,  8-) use the observer pattern(I think it is part of mvvm-strategy) i.e.:
0) Have a look at IFPObserved/IFPObserver in TPersistent, how it is
    implemented.
1) In your model/view implement the observed part(view don't talk directly
    to the backend, only through controller).
    In your model/view implement a "Notify" method, so that the model /
    backend can call it. This method notifies all registered observers of  the
    change, taking place.
2) In your view implement the observer part e.g.:
    TView = class(TObject,IFPObserver)... etc...
    procedure TView.FPOObservedChange(xx,xx,xx);
3) On create-time you connect / register TView with you model/view...
4) When you tell the controller(model/view) to create a new file, it then tells
    the model/backend to do the necessary operations, it (model) in turn calls
    the "Notify" method of the controller(model/view) along the way to inform
    whomever may be listening/observing, of what it's doing...
5) When you're done, you UNregister the TView with the controller (model /
    view), to avoid dangling references.
(i) It's harder to describe in short, than it is to implement  ;)
EDIT: btw. the following have got no business whatsoever to do in backend:
Code: Pascal  [Select][+][-]
  1. function TAppDbCreate.SelectDbFilePath: Boolean;
  2. var
  3.   saveDialog : TSaveDialog; // BELONGS IN CONTROLLER
  4. begin
  5. // Screen.Cursor := crHourGlass; // BELONGS IN VIEW -> GETS NOTIFIED
  6.   how can you send a text back to the main form here? // CALL CTRL.NOTIFY
  7.  
  8. // Screen.Cursor := crDefault; // BELONGS IN VIEW -> GETS NOTIFIED
  9. end;

HTH
Regards Benny
« Last Edit: September 23, 2023, 10:18:57 am 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: 715
Re: mvvm experimenting
« Reply #2 on: September 25, 2023, 04:18:52 pm »
For me it is not "Easy peasy". I do not know where to begin. I can find verrry little documentation on the subject mvvm. I'm stuck  :-[

cdbc

  • Hero Member
  • *****
  • Posts: 1664
    • http://www.cdbc.dk
Re: mvvm experimenting
« Reply #3 on: September 25, 2023, 05:48:12 pm »
Hi
I think it will be easier for you, to start with MVC, instead of MVVM, as mvc doesn't use a "Binder" component, like e.g.: microsofts version of mvvm does.
Here: https://www.geeksforgeeks.org/mvc-framework-introduction/ is a nice writeup of how it goes and the picture a bit down on the page, is very accurate to visualize what's going on.
Model takes care of business logic and data.(doesn't know about view)
   ^
   v
Controller communicates data to the view and commands/data to the model.
   ^
   v
View takes care of showing data from the controller to the user and sending commands/data to the controller. (doesn't know about model)
One could say. that the controller is a two-faced API, as long as you adhere to the api's, you can swap models(db,txt-f,cloud) and views(cli,gui,web), on the fly...
Tip(*) Make a good testbed, in which to test your ideas...
HTH
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

nummer8

  • Full Member
  • ***
  • Posts: 119
Re: mvvm experimenting
« Reply #4 on: September 25, 2023, 06:04:08 pm »
Hi,

There is a book about the subject
MVVM in Delphi.
https://link.springer.com/book/10.1007/978-1-4842-2214-0

The sourcecode of the book can be found at:
https://github.com/apress/mvvm-in-delphi

It is for Delphi but it can give you some clues.

 

TinyPortal © 2005-2018