My speech at “PHPCon Italia 2009”

I will talk at the italian PHPCon in Rome on March 18-20 2009.

I will talk about “Data Access Design Patterns” showing many PHP examples.

PHPCon Italia 2009
Holiday Inn Eur Parco dei Medici
Viale Castello Della Magliana 65
ROME, 00148
Web Site

Click here for discount on ticket (italian web site)

And this is the “Speaker Button”

My Speaker Button
My Speaker Button

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

As GUI framework such as VCL become more and more powerful, it’s common practice to let the UI layer do more than it should. Without a clear separation of responsibilities, the UI layer can often become an integral part of application and businness logic, but… this kind of responsabilities belongs to other layers of the application.
A design pattern (and his numberless variants), is especially well suited to solving this problem.

In this article I want to build a simple application using MVP pattern. Actually, pattern used is not “clear” MVP but his variation called Passive View.

Using Fowler words:

A perennial problem with building rich client systems is the complication of testing them. Most rich client frameworks were not built with automated testing in mind. Controlling these frameworks programaticly is often very difficult.

A Passive View handles this by reducing the behavior of the UI components to the absolute minimum by using a controller that not just handles responses to user events, but also does all the updating of the view. This allows testing to be focused on the controller with little risk of problems in the view.

Passive View ensures no dependecies between Model and View.

Passive View has no dependencies between view and model (Unlike most MVC-style triad)
Passive View has no dependencies between view and model (Unlike most MVC-style triad)

In this sample, “model” is a simple layer for application logic. In real world, “service layer” should incapsulate “application service” and “domain model“.

Application looks like following:

The Calculator
The Calculator
Div operator with result
Div operator with result
Div operator with a EDivByZero Exception
Div operator with a EDivByZero Exception

Connect View and Presenter
The view (the Form in VCL application) must implement an interface.

This interface should provide all method to interact with GUI:

ICalculatorView = interface
  function FirstOperand: String;
  function SecondOperand: String;
  procedure SetFirstOperand(Value :String);
  procedure SetSecondOperand(Value :String);
  function GetOperator: IGUISelectableList;
  procedure SetCalcResult(const Value: String);
  procedure SetCalcResultReadOnly(const Value: Boolean);
  function Error: IGUIEdit;

For simple interacting with GUI widget (in our example are EditFirstOperand, EditSecondoperand and EditCalcResult) we use a simple methods like following

  function FirstOperand: String;
  function SecondOperand: String;
  procedure SetFirstOperand(Value :String);
  procedure SetSecondOperand(Value :String);

But, if we need more by our widget (like populating combo box or change font color in the EditError or set ReadOnly to true) we should use another interface for a family of component.
In this sample I wrote 3 general interface:

  IGUIBaseInterface = interface

  IGUIEdit = interface(IGUIBaseInterface)
    procedure SetText(const Value: String);
    function GetText: String;
    function GetAsInteger: Integer;
    function GetAsFloat: Extended;
    procedure SetReadOnly(const AValue: boolean);
    procedure SetVisible(const Value: Boolean);
    function GetTextAsInteger: Integer;
    procedure SetTextAsinteger(const Value: Integer);
    function GetTextAsFloat: Extended;

  IGUISelectableList = interface(IGUIBaseInterface)
    procedure SetText(const Value: String);
    function GetText: String;
    procedure SetValue(const Value: String);
    function GetValue: String;
    function GetSelected: ISSKeyValue;
    procedure AddPair(AKey, AValue: String);
    procedure Clear;

For implementation details see attached sample code.

Finally in FormCreate of our form we can wire Presenter and View:

TfrmCalculatorView = class(TForm, ICalculatorView)
  //interface section
procedure TfrmCalculatorView.FormCreate(Sender: TObject);
  //Link controls with related interface
  IOperators := TGUISelectableList.Create(ComboOperators);
  IError := TGUIEdit.Create(EditError);

  //link view and presenter
  //In this version VIEW know PRESENTER
  FPresenter := TCalculatorPresenter.Create(Self);

This is a very simple example, so not all looks like real world. In a real world application, for example, view should not known the presenter class. With dependency injection you can do that (Next article in this serie will talk about this).

Every event generated by View (our Form) must be redirected to Presenter.

procedure TfrmCalculatorView.Button1Click(Sender: TObject);

Another approach is to publish some events in view interface and let presenter to bind them via standard event handler or anonimous methods (but this is for another post).

In attached sample code there is a sample application and unit test for Service Layer and View.
Required Mock Library is included in the zip file.

Simple Passive View, Sample Code

In 2nd part I’ll talk about unit test and mock object in Passive View.

.NET databinding in Delphi for Win32

Databinding is defined as: “General technique that binds two data/information sources together and maintains them in sync. This is usually done with two data/information sources with different types as in XML data binding. However in UI data binding, we bind data and information objects of the same type together (e.g. Java objects to Java UI elements).”

Databinding is common technique in VCL. Since Delphi 1 we have TDataset class for bind data and UI controls (DB Aware) in a GUI application.

In .NET world, instead, databinding is very different.

So, I’m starting to write (actually for fun) a DataBinder component to use .NET “like” databinding (or something similar to) in Delphi for Win32 too.

All the code has been written in about 2 hours.


With this component you can “bind” an object property to another object property in a declarative mode.


DataBinder.Add(Person, 'FirstName', Edit1, 'Text');

and then, every update to Person.FirstName property, will be reflected in the Edit1.Text property.

You can bind different control properties to different BO properties.


//Text = FirstName
DataBinder.Add(Person, 'FirstName', Edit1, 'Text');
//If Person is not married, TEdit become flat
DataBinder.Add(Person, 'IsMarried', Edit1, 'Ctl3D');

So in your initialization code (e.g. FormCreate) you can write somethig similat to following:

procedure TForm3.FormCreate(Sender: TObject);
binder: TDataBinder;
//Create your "BO"
Person := TPerson.Create;
//read data from "database"

//Setup databinding...
binder := TDataBinder.Create(self);
binder.Add(Person, 'FirstName'   ,      Edit1,     'Text');
binder.Add(Person, 'LastName',          Edit2,     'Text');

//The same attribute binded to 3 controls
binder.Add(Person, 'Married',     CheckBox1, 'Checked');
binder.Add(Person, 'Married'   , Edit1,     'Ctl3D');
binder.Add(Person, 'Married',     Edit2,     'Ctl3D');

//The same attribute binded to 2 controls
binder.Add(Person, 'SomeInteger', ComboBox1, 'ItemIndex');
binder.Add(Person, 'SomeInteger',     TrackBar1, 'Position');

//A derived property
binder.Add(Person, 'FullName',     Panel1, 'Caption');

//let start...

Other info asap so, stay tuned.

Download Code and compiled sample

(Source code require Delphi 2009)

Value Object vs Data Transfer Object

One of the main goals of design patterns is to have a names dictionary shared among professionals.
This dictionary, sometimes, has some trouble.

It’s the case of two well known design patterns: Value Object and Data Transfer Object.

Value Object, according to Martin Fowler, is defined as:
“A small simple object, like money or a date range, whose equality isn’t based on identity.”

But Value Object, according to SUN, was for some time defined as follows: “An object that carries data between processes in order to reduce the number of method calls.”

In the words of Fowler: “Many people in the Sun community use the term “Value Object” for this pattern. I use it to mean something else.” (Talking about Data Transfer Object)

Some time ago, Value Object pattern (SUN version), has been renamed to “Transfer Object” according to Fowler.

Question is exposed in a few lines by Fowler at following address:

New home for my blog

Hi all,

now there is another home for my blog, my software, my photos and so on.

I will transfer all of my contents from the old site to this one in an reasonably short time.

English is not my natural language, so I’m apologize for any mistake you will find in these pages.


–Daniele Teti