Database application using MVC paradigm

1.5k views Asked by At

In classic Delphi database application we have a form, a set of data-aware visual controls connected to TDataSets via TDataSources. If one wants to design database application using MVC model, where to place TDataSet components? Should they stay on form and therefore be a part of View? Or should they be encapsulated inside some Model object? If the latter how they could be bound to visual components?

2

There are 2 answers

1
dmajkic On

You could see delphi project via MVC eyes like this:

TDataModule -> Model
TYourForm.pas -> Controller
TYourForm.dfm -> View

If you accept it, than you should put DataSets in Datamodule.

In project I am working on, I also put TClientDataSets on forms and clone data on creation. That way I have isolated data in TForm, with logic in datamodule.

Pity there is no ActiveRecord like framework for delphi.

0
André On

I have made a MVC-like framework for my current customer, for you it would make something like this:

  • TDatamodule = Model (for data, so TDataset stuff)
  • TForm = View (contains datasources, connected to TDatasets of model)
  • Controller.pas = Controller (execution/business logic)

Try to keep your form as clean as possible: may only contain stuff for GUI. Implement special (business rules, data updates etc) logic in controller. Keep in mind: your view must be easily be replaced with another view (like webpage).

In my MVC-like framework, the view calls methods (like search, refresh, etc) with params of the controller, the controller knows what and how to search etc and fills/updates the model. View is connected with the model (property of controller).
It is not exactly MVC, but this way the GUI is separated from all kind of logic and it is still easy to debug and maintain (you could use dependency injection/loose coupling with the controller etc but this makes it more difficult, only use when needed)