Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database application using MVC paradigm

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?

like image 872
Andrej Kirejeŭ Avatar asked Apr 11 '10 08:04

Andrej Kirejeŭ


2 Answers

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.

like image 172
dmajkic Avatar answered Sep 21 '22 13:09

dmajkic


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)

like image 40
André Avatar answered Sep 17 '22 13:09

André