Recent

Author Topic: Dublicate of document – how to implement?  (Read 1904 times)

hakatovich

  • Newbie
  • Posts: 2
Dublicate of document – how to implement?
« on: April 01, 2018, 08:24:36 pm »
Hello friends :) I recently started to study FreePascal and the Lazarus environment.

I need your help in implementing my scheme through the OOP.

So, I'm working with the document in the form. The document is on the disk. Through tiOPF, it is loaded into the program as the original.

In the form, I create a duplicate of this document and work with it. If the user saves the document (presses the Save button), the original is assigned from the duplicate and is saved. All actions take place in the view-model.

I made this code:

Code: Pascal  [Select][+][-]
  1. type
  2.  TBusinessObject = class(TPersistent)
  3.  // fields...
  4.  end;
  5.  
  6.  TBaseViewModel = class
  7.   private
  8.     function GetOriginal: TBusinessObject; virtual; overload;
  9.     function GetWorkspace: TBusinessObject; virtual; overload;
  10.     procedure SetOriginal(const AValue: TBusinessObject); virtual; overload;
  11.     procedure SetWorkspace(const AValue: TBusinessObject); virtual; overload;
  12.   protected
  13.     {- Original document, can be stored in other place im memory -}
  14.     property Original: TBusinessObject read GetOriginal write SetOriginal;
  15.  
  16.     {- Dublicate document, stored only in this class -}
  17.     property Workspace: TBusinessObject read GetWorkspace write SetWorkspace;
  18.   public
  19.     constructor Create;
  20.     destructor Destroy; override;
  21.  
  22.     {- Create dublicate of document in memory -}
  23.     procedure MakeWorkspace(AOriginal: TBusinessObject); overload; virtual;
  24.  
  25.     {- Assign original from dublicate -}
  26.     procedure Revert(const ASaveChanges: boolean);
  27.   end;

and implement it

Code: Pascal  [Select][+][-]
  1. procedure TBaseViewModel.MakeWorkspace(AOriginal: TBusinessObject);
  2. begin
  3.   Workspace.Assign(AOriginal);
  4.   Original := AOriginal;
  5. end;
  6.  
  7. procedure TBaseViewModel.Revert(const ASaveChanges: boolean);
  8. begin
  9.   Original.Assign(Workspace);
  10.  
  11.   if ASaveChanges then
  12.     Original.MarkDirtyAndSave; // <-- indicates that there were changes and saved the document.
  13.  
  14.   // like a FreeAndNil(Workspace)
  15.   Workspace := nil;
  16.   Workspace.Free;
  17. end;

So, my question.

How to make it so that this system can work freely with the derived class TBusinessObject?

For example, for a class
Code: Pascal  [Select][+][-]
  1. TDocumentRTF = class(TBusinessObject).
I want to use custom View-Model.
Code: Pascal  [Select][+][-]
  1. TEditRTFFormViewModel = class (TBaseViewModel)
  2. //...
  3. public
  4.     property Original: TDocumentRTF read GetOriginal write SetOriginal;
  5.     property Workspace: TDocumentRTF read GetWorkspace write SetWorkspace;
  6. end;

How to make it so that it can elegantly and easily work with the derived class TBusinessObject?

How to make sure that the Original and Workspace properties always have TBusinessObject's successors?

For example, for TEditRTFFormViewModel this will be TDocumentRTF, for TEditDOCFormViewModel will be TDocumentDOC etc.
« Last Edit: April 01, 2018, 08:33:08 pm by hakatovich »

hakatovich

  • Newbie
  • Posts: 2
Re: Dublicate of document – how to implement?
« Reply #1 on: April 02, 2018, 06:45:13 am »
Any ideas?

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: Dublicate of document – how to implement?
« Reply #2 on: April 03, 2018, 12:50:51 am »
You ask a lot in one post and it is a little confusing..

But I There are  "AS". "IS" to cast and test for the existence of a class given in a class pointer.

So lets go to the next step. because I am still not sure  here..

In your Class define have fields that hold the Original and current TDocument classes.

It looks like you have provided Getters and Setters..

Those need to be implemented as Methods in your code and defined in the implementation section.

come back when you got that solved ;)

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018