Home » Delphi » Delphi 2010 » Delphi Dependency Injection » Design Patterns » MVC » MVP » A Simple start with MVP in Delphi for Win32, Part 2

A Simple start with MVP in Delphi for Win32, Part 2

Some month ago I wrote a simple article about an MVP variant called PassiveView.
That example was very simple. Now I’ll present a more “advanced” version of that example.

The main problem with first example was the following method:

procedure TfrmCalculatorView.FormCreate(Sender: TObject);
begin
  //Link controls with related interface
  IFirstOperand := TGUIEdit.Create(EditFirstOp);
  ISecondOperand := TGUIEdit.Create(EditSecondOp);
  ICalcResult := TGUIEdit.Create(EditResult);
  IOperators := TGUISelectableList.Create(ComboOperators);
  IError := TGUIEdit.Create(EditError);

  //link view and presenter
  FPresenter := TCalculatorPresenter.Create(Self); //<<-- THIS IS THE BAD LINE
end;

The “BAD” line links the View with the Presenter but it’s in the view code, so this is meaning that View KNOWS the presenter… and this is not a good thing becouse the view is not so “passive”.

In a more advanced (and complex) version the View should be completely ignorant about the class that implement the presenter and the service.

In the main dpr file now the code now looks like the following.

var
  MainPresenter: ICalculatorPresenter;
  CalculatorView: TForm;
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  //SETUP THE MAIN APPLICATION FORM FOR VCL PURPOSE
  Application.CreateForm(TfrmCalculatorView, CalculatorView);  
  //SETUP ALL THE LINKS BETWEEN THE MVP TRIAD
  MainPresenter := TCalculatorPresenter.Create(CalculatorView as ICalculatorView, TCalculatorService.Create);
  //LETS START!
  Application.Run;
end.

Now the presenter take care about all links between the MVP triad.

constructor TCalculatorPresenter.Create(CalculatorView: ICalculatorView;
  CalculatorService: ICalculatorService);
begin
  inherited Create;
  FCalculatorService := CalculatorService;
  FCalculatorView := CalculatorView;
  FCalculatorView.SetPresenter(self);
  InitView;  //does the links
end;

There is another addition to the previous example. Now there is only one constructor in the presenter, and using dependency injection take 2 interface for the view and the service.

    constructor Create(CalculatorView: ICalculatorView; CalculatorService: ICalculatorService);

Another plus is the possibility to open the same form a number of times without change the code for create it.

This is the GUI for this simple application.

3 instance of the same view with different presenter and service

3 instance of the same view with different presenter and service

As bonus, unit tests and mock object arent changed.

As usual the source code is here.

4 commenti a “A Simple start with MVP in Delphi for Win32, Part 2

  1. Hi Teti,

    Thanks for a great blog on Passive View. Do you have an opinion on using Supervising Controller? Passive View is great for simple forms and dialog boxes but if you need to do something more interactive, Supervising Controller is more useful. The only problem is that it is more complex because it requires a publish/subscriber-type bindings to the View.

  2. Hi,

    Great post and blog! But and about a CRUD Application?

    I know this post is old, but a CRUD Aplication has some particularities. There are no examples about it in Delphi.

    If I make a simple example (maybe a User CRUD) and send to you, you could take a look? Maybe using it to make a post about it?

    I appreciate your attention,

    Best regards,

    Rafael Draghetti Dipold

Lascia una risposta

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *

È possibile utilizzare questi tag ed attributi XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>