Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Managed Bindings - Build a MVVM Framework


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Sep 29, 2017 4:06 AM Last Post By: Martin .MM.
Martin .MM.

Posts: 3
Registered: 9/8/03
Managed Bindings - Build a MVVM Framework  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 15, 2017 5:36 AM
Hi,

I have searched for good articles/tutorials/videos on using managed bindings with Delphi but other than this video: Introduction to MVVM - The Delphi Way https://www.youtube.com/watch?v=k64Skcn3d6E and this blog post: Using LiveBindings to Connect the UI to Objects https://www.adug.org.au/technical/vcl/using-livebindings-to-connect-the-ui-to-objects/ I haven't found much on using code based bindings.

I would love to study the framework used for the BindingDemo in the video to get inspiration and motivation of creating/extending such a framework.

My idea is instead of using the live bindings designer I would prefer the bindings to be explicitly specified in code:
FViewModel.bind(edtName, 'edtName.Text', 'Name'); // Bind to Properties
FViewModel.bind(edtEmail, 'edtEmail.Text', 'Email');
..
FViewModel.bind(btnSave, 'btnSave.Enabled', 'CanSave'); // Bind to Methods - through property


I am aware there are more robust MVVM and ORM frameworks (mostly using attributes, not explicit binding) so a tutorial or demo of creating a simple MVVM framework would be better for me to deepen my understanding of live bindings, generics and using Delphi's RTTI system.

unit ... 
type
	TContact = class(TBaseModel)
	private
		FName: string;
		...
end;
 
procedure TContact.SetName(const Value: string);
begin
	Update('Name', FName, Value);
end;
 
procedure TContact.SetXYZ(const Value: string);
begin
	Update('XYZ', FXYZ, Value);
end;
 
unit ...
type
	TContactView = class(TForm, IView)
		NameLbl: TLabel;
		...
		NameEdt: TEdit;
	private
		FViewModel: IViewModel; // Not using specific ViewModel class like TContactVM
	public
		constructor Create(AViewModel: IViewModel); reintroduce;
end;
 
constructor TContactView.Create(AViewModel: IViewModel);
begin
	inherited Create(Nil);
	FViewModel := AViewModel;
	// bindings
	FViewModel.Bind(NameEdt, 'NameEdt.Text', 'Name');
	...
	FViewModel.Bind(OKBtn, 'OKBtn.Enabled', 'CanSave');
end;
 
procedure TContactView.NameEdtChange(Sender: TObject);
begin
	FViewModel.NotifyPropertyChange(Sender, );
end;
 
unit ...
type // specifically interested in implementation of TBaseObjectVM and the supporting classes: BaseModel, BaseView, Binder, BindingLink, ...
	TContactVM = class(TBaseObjectVM<TContact>) // another plus is the use of generics for the framework
	private
		function GetCanSave: Boolean;
	protected
		procedure PropertyChanged(const APropertyName: string);
	public
		property CanSave: Boolean read GetCanSave;
	end;
 
	TContactListVM = class(... // How should I implement this?
	end;
 
function TContactVM.GetCanSave: Boolean;
begin
	Result := Model.Name <> '';
end;
 
procedure TContactVM.PropertyChanged(const APropertyName: string);
begin
	if APropertyName = 'Name' then
		NotifyPropertyChange(Self, 'CanSave')
end;


Thanks,
Martin

Edited by: Martin .MM. on Aug 5, 2017 9:14 AM
Bernd Maierhofer

Posts: 161
Registered: 9/27/99
Re: Managed Bindings - Build a MVVM Framework [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 27, 2017 11:00 PM   in response to: Martin .MM. in response to: Martin .MM.
Martin .MM. wrote:

Hi,

I have searched for good articles/tutorials/videos on using *managed
bindings* with Delphi but other than this video: *Introduction to
MVVM - The Delphi Way* https://www.youtube.com/watch?v=k64Skcn3d6E
and this blog post: Using LiveBindings to Connect the UI to Objects
https://www.adug.org.au/technical/vcl/using-livebindings-to-connect-th
e-ui-to-objects/ I haven't found much on using code based bindings.

I would love to study the framework used for the BindingDemo in the
video to get inspiration and motivation of creating/extending such a
framework.

My idea is instead of using the live bindings designer I would prefer
the bindings to be explicitly specified in code:
FViewModel.bind(edtName, 'edtName.Text', 'Name'); // Bind to
Properties FViewModel.bind(edtEmail, 'edtEmail.Text', 'Email');
..
FViewModel.bind(btnSave, 'btnSave.Enabled', 'CanSave'); // Bind to
Methods - through property 


I am aware there are more robust MVVM and ORM frameworks (mostly
using attributes, not explicit binding) so a tutorial or demo of
creating a simple MVVM framework would be better for me to deepen my
understanding of live bindings, generics and using Delphi's RTTI
system.

unit ... 
type
	TContact = class(TBaseModel)
	private
		FName: string;
		...
end;
 
procedure TContact.SetName(const Value: string);
begin
	Update('Name', FName, Value);
end;
 
procedure TContact.SetXYZ(const Value: string);
begin
	Update('XYZ', FXYZ, Value);
end;
 
unit ...
type
	TContactView = class(TForm, IView)
		NameLbl: TLabel;
		...
		NameEdt: TEdit;
	private
		FViewModel: IViewModel; // Not using specific ViewModel class like
TContactVM 	public
		constructor Create(AViewModel: IViewModel); reintroduce;
end;
 
constructor TContactView.Create(AViewModel: IViewModel);
begin
	inherited Create(Nil);
	FViewModel := AViewModel;
	// bindings
	FViewModel.Bind(NameEdt, 'NameEdt.Text', 'Name');
	...
	FViewModel.Bind(OKBtn, 'OKBtn.Enabled', 'CanSave');
end;
 
procedure TContactView.NameEdtChange(Sender: TObject);
begin
	FViewModel.NotifyPropertyChange(Sender, );
end;
 
unit ...
type // specifically interested in implementation of TBaseObjectVM
and the supporting classes: BaseModel, BaseView, Binder, BindingLink,
...  	TContactVM = class(TBaseObjectVM<TContact>) // another plus is
the use of generics for the framework 	private 		function GetCanSave:
Boolean; 	protected
		procedure PropertyChanged(const APropertyName: string);
	public
		property CanSave: Boolean read GetCanSave;
	end;
 
	TContactListVM = class(... // How should I implement this?
	end;
 
function TContactVM.GetCanSave: Boolean;
begin
	Result := Model.Name <> '';
end;
 
procedure TContactVM.PropertyChanged(const APropertyName: string);
begin
	if APropertyName = 'Name' then
		NotifyPropertyChange(Self, 'CanSave')
end;


Thanks,
Martin

Edited by: Martin .MM. on Aug 5, 2017 9:14 AM

MVVM and ORM are quite different things, I suppose, you know that.

Spring4D
https://bitbucket.org/sglienke/spring4d
has the observer apttern as well as multicast events, that maybe what
you are looking for as a starter.

DSharp
https://bitbucket.org/sglienke/dsharp
has some MVVM parts, too.

This book is quite a good starter, too:
https://www.amazon.de/MVVM-Delphi-Architecting-ViewModel-Applications/dp
/148422213X

And last not least:
https://community.embarcadero.com/blogs/entry/mvvm-mvc-rad-architectures
-with-columbusegg4delphi

HTH Bernd
--

Martin .MM.

Posts: 3
Registered: 9/8/03
Re: Managed Bindings - Build a MVVM Framework [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 29, 2017 4:06 AM   in response to: Bernd Maierhofer in response to: Bernd Maierhofer
Bernd Maierhofer wrote:
Martin .MM. wrote:

Hi,

I have searched for good articles/tutorials/videos on using *managed
bindings* with Delphi but other than this video: *Introduction to
MVVM - The Delphi Way* https://www.youtube.com/watch?v=k64Skcn3d6E
and this blog post: Using LiveBindings to Connect the UI to Objects
https://www.adug.org.au/technical/vcl/using-livebindings-to-connect-th
e-ui-to-objects/ I haven't found much on using code based bindings.

I would love to study the framework used for the BindingDemo in the
video to get inspiration and motivation of creating/extending such a
framework.

My idea is instead of using the live bindings designer I would prefer
the bindings to be explicitly specified in code:
FViewModel.bind(edtName, 'edtName.Text', 'Name'); // Bind to
Properties FViewModel.bind(edtEmail, 'edtEmail.Text', 'Email');
..
FViewModel.bind(btnSave, 'btnSave.Enabled', 'CanSave'); // Bind to
Methods - through property 


I am aware there are more robust MVVM and ORM frameworks (mostly
using attributes, not explicit binding) so a tutorial or demo of
creating a simple MVVM framework would be better for me to deepen my
understanding of live bindings, generics and using Delphi's RTTI
system.

unit ... 
type
	TContact = class(TBaseModel)
	private
		FName: string;
		...
end;
 
procedure TContact.SetName(const Value: string);
begin
	Update('Name', FName, Value);
end;
 
procedure TContact.SetXYZ(const Value: string);
begin
	Update('XYZ', FXYZ, Value);
end;
 
unit ...
type
	TContactView = class(TForm, IView)
		NameLbl: TLabel;
		...
		NameEdt: TEdit;
	private
		FViewModel: IViewModel; // Not using specific ViewModel class like
TContactVM 	public
		constructor Create(AViewModel: IViewModel); reintroduce;
end;
 
constructor TContactView.Create(AViewModel: IViewModel);
begin
	inherited Create(Nil);
	FViewModel := AViewModel;
	// bindings
	FViewModel.Bind(NameEdt, 'NameEdt.Text', 'Name');
	...
	FViewModel.Bind(OKBtn, 'OKBtn.Enabled', 'CanSave');
end;
 
procedure TContactView.NameEdtChange(Sender: TObject);
begin
	FViewModel.NotifyPropertyChange(Sender, );
end;
 
unit ...
type // specifically interested in implementation of TBaseObjectVM
and the supporting classes: BaseModel, BaseView, Binder, BindingLink,
...  	TContactVM = class(TBaseObjectVM<TContact>) // another plus is
the use of generics for the framework 	private 		function GetCanSave:
Boolean; 	protected
		procedure PropertyChanged(const APropertyName: string);
	public
		property CanSave: Boolean read GetCanSave;
	end;
 
	TContactListVM = class(... // How should I implement this?
	end;
 
function TContactVM.GetCanSave: Boolean;
begin
	Result := Model.Name <> '';
end;
 
procedure TContactVM.PropertyChanged(const APropertyName: string);
begin
	if APropertyName = 'Name' then
		NotifyPropertyChange(Self, 'CanSave')
end;


Thanks,
Martin

Edited by: Martin .MM. on Aug 5, 2017 9:14 AM

MVVM and ORM are quite different things, I suppose, you know that.

Spring4D
https://bitbucket.org/sglienke/spring4d
has the observer apttern as well as multicast events, that maybe what
you are looking for as a starter.

DSharp
https://bitbucket.org/sglienke/dsharp
has some MVVM parts, too.

This book is quite a good starter, too:
https://www.amazon.de/MVVM-Delphi-Architecting-ViewModel-Applications/dp
/148422213X

And last not least:
https://community.embarcadero.com/blogs/entry/mvvm-mvc-rad-architectures
-with-columbusegg4delphi

HTH Bernd
--


Thanks Bernd for the useful links.

I am more interested in using native Delphi features specifically managed bindings instead of visual bindings i.e. without installing 3rd party components (commercial or open source). This is primarily a learning exercise in addition to limiting myself to using features provided with the product.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02