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:
type
TBusinessObject = class(TPersistent)
// fields...
end;
TBaseViewModel = class
private
function GetOriginal: TBusinessObject; virtual; overload;
function GetWorkspace: TBusinessObject; virtual; overload;
procedure SetOriginal(const AValue: TBusinessObject); virtual; overload;
procedure SetWorkspace(const AValue: TBusinessObject); virtual; overload;
protected
{- Original document, can be stored in other place im memory -}
property Original: TBusinessObject read GetOriginal write SetOriginal;
{- Dublicate document, stored only in this class -}
property Workspace: TBusinessObject read GetWorkspace write SetWorkspace;
public
constructor Create;
destructor Destroy; override;
{- Create dublicate of document in memory -}
procedure MakeWorkspace(AOriginal: TBusinessObject); overload; virtual;
{- Assign original from dublicate -}
procedure Revert(const ASaveChanges: boolean);
end;
and implement it
procedure TBaseViewModel.MakeWorkspace(AOriginal: TBusinessObject);
begin
Workspace.Assign(AOriginal);
Original := AOriginal;
end;
procedure TBaseViewModel.Revert(const ASaveChanges: boolean);
begin
Original.Assign(Workspace);
if ASaveChanges then
Original.MarkDirtyAndSave; // <-- indicates that there were changes and saved the document.
// like a FreeAndNil(Workspace)
Workspace := nil;
Workspace.Free;
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
TDocumentRTF = class(TBusinessObject).
I want to use custom View-Model.
TEditRTFFormViewModel = class (TBaseViewModel)
//...
public
property Original: TDocumentRTF read GetOriginal write SetOriginal;
property Workspace: TDocumentRTF read GetWorkspace write SetWorkspace;
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.