Tuesday 6 January 2009

MVC updated

I have had a small rest over Christmas and have decided that the code below needs to be refactored. I broke down all of the interfaces and implementations of them into different units, and begun to separate out the stuff that is MVC and stuff which used by the model.

Specifically I broke down the command stuff and implemented descendants of TCommand that know what to do to the model - i.e. the model doesn't do anything other than respond to the data changes (or not as is the current state).

This means I have ended up with code like this ..

TCalculateCommand = class (TCommand)
procedure Run (model : tObject); override;
end;
..

{ TCalculateCommand }

procedure TCalculateCommand.Run (model : tObject);
begin
(model as TDomainModel).Data.Price := self.Params[0].Value;
(model as TDomainModel).Data.Consideration := ((model as TDomainModel).Data.Size *
(model as TDomainModel).Data.Price) + (model as TDomainModel).Data.Charges;
end;


This is a lot simpler in my view.

However it doesn't solve the problem with how to indicate that a field is invalid?

I might have to register each control with the model, so it knows which fields map to each control. That sounds like a lot to faff, but I don't know how else to do it.

2 comments:

Unknown said...

Hi, i found this tutorial of the Delphi implemention of MVC pattern design very useful. Can you tell me, where can i download the source code of the example described in the the tutorial?

inpw said...

Hi,
I was just using this as a memort aide. I can't really post my code on online, as (a) it isn't finished, and (b) you will need an iSeries to make it work!

So, I would point you at the Joanne Carter pages that I largely cribbed / copied / changed.

Hope this helps