Friday, 7 November 2008

MVC Update

In the real world, which I obviously can't post here, I have a problem where the result of the validation request does three things;

1) Validates the input values
2) Returns other information based on the inputs (i.e. Other client, stock details)
3) Set whether a specific field input is valid or not.

Sunday, 2 November 2008

More MVC!

A client search from would look like this ..


type
TForm3 = class(TForm, IObserver)
Button1: TButton;
Edit1: TEdit;
ListBox1: TListBox;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure UpdateView (obj : TObject);
end;

...

procedure TForm3.Button1Click(Sender: TObject);
var
c : TCommand;
o : TCommandParam;

begin
c := TCommand.Create ('SEARCH');

o := TCommandParam.Create;
o.Name := edit1.Text;

c.AddParam(o);

Controller.DoCommand(c);
end;

procedure TForm3.Button2Click(Sender: TObject);
begin
ModalResult := mrOK;
end;

procedure TForm3.Button3Click(Sender: TObject);
begin
ModalResult := mrCancel;
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
Controller.AddView (self);
end;

procedure TForm3.UpdateView (obj : TObject);
var
val : String;

begin
// Update the view here
if ((obj as TDomainModel).ClientSearchResult <> nil) then
begin
for val in (obj as TDomainModel).ClientSearchResult do
begin
ListBox1.AddItem(val, nil);
end;
end;
end;



I have merge the View classes with the actual form, in order to make the code relationships a little clearer.

I'm really happy with all of the, and I think it will be applicable for the requirements I have at work.