Sneak peek to simple integration between DMVCFramework and DORM

Uncategorized 3 Comments »

This is a really simple (not optimized and dirty) integration between the upcoming DMVCFramework (WebBroker based MVC framework) and DORM, “the Delphi ORM”.

This is the DMVCFramework controller with the relative mapping and methods. In the method “GetUsers” dorm is used to execute a select to the database using the sanitized parameter passed on the url.

unit UsersControllerU;
  1.  
  2. interface
  3.  
  4. uses MVCFramework, dorm;
  5.  
  6. type
  7.  
  8.   [MVCPath('/users')]
  9.   TUsersController = class(TMVCController)
  10.   strict private
  11.     dormSession: TSession;
  12.  
  13.   strict protected
  14.     procedure MVCControllerAfterCreate; override;
  15.     procedure MVCControllerBeforeDestroy; override;
  16.  
  17.   public
  18.     [MVCPath('/($id)')]
  19.     [MVCHTTPMethod([httpGET])]
  20.     procedure GetUsers(CTX: TWebContext);
  21.   end;
  22.  
  23. implementation
  24.  
  25. uses
  26.   dorm.loggers, dorm.adapters, dorm.Commons, UsersBO;
  27.  
  28. { TUsersController }
  29.  
  30. procedure TUsersController.GetUsers(CTX: TWebContext);
  31. var
  32.   User: TUser;
  33. begin
  34.   User := dormSession.Load < TUser > (CTX.Request.ParamsAsInteger['id']);
  35.   Render(User);
  36. end;
  37.  
  38. procedure TUsersController.MVCControllerAfterCreate;
  39. begin
  40.   inherited;
  41.   dormSession := TSession.CreateConfigured('dorm.conf', TdormEnvironment.deDevelopment);
  42. end;
  43.  
  44. procedure TUsersController.MVCControllerBeforeDestroy;
  45. begin
  46.   dormSession.Free;
  47.   inherited;
  48. end;
  49.  
  50. end.

Now, if you run the application and go to http://localhost/users/1 (the server is running on port 80), you’ll get the following:

Stay tuned.

Tomorrow the webinar: “Developing Application Services using PHP Servers and Android Clients”

Android, Programming, RADPHP, Video 31 Comments »

Developing Application Services with PHP Servers and Android Phone Clients

Developing Application Services with PHP Servers and Android Phone Clients

In this webinar I’ll show how to develop Android application talking with REST PHP WEB Services. After introducing the basic concepts, attendees of this session will be taken through how to REST-enable the server application, before building the client application targeting an Android phone.

Topics covered in this webinar include:

  • Introduction to REST and JSON support in PHP
  • Introduction to Android client development
  • Creating a REST web service with RadPHP
  • Connecting from an Android application to the REST web service

Companion White Paper, Videos and Source Code Visit the RAD Studio in Action – PHP and Android Resource Center for additional information on this topic, including an in-depth technical white paper, example source code and a video series on building applications with PHP and Android.

More info on http://www.embarcadero.com/rad-in-action/php-android

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

CodeGear, Delphi for Win32, Design Patterns, MVC, MVP, Programming, Uncategorized 11 Comments »

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:

  1. ICalculatorView = interface
  2.   ['{471E3657-C6CE-49A3-BCB4-8FA6AF611DAD}']
  3.   function FirstOperand: String;
  4.   function SecondOperand: String;
  5.   procedure SetFirstOperand(Value :String);
  6.   procedure SetSecondOperand(Value :String);
  7.   function GetOperator: IGUISelectableList;
  8.   procedure SetCalcResult(const Value: String);
  9.   procedure SetCalcResultReadOnly(const Value: Boolean);
  10.   function Error: IGUIEdit;
  11. end;

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

  1.   function FirstOperand: String;
  2.   function SecondOperand: String;
  3.   procedure SetFirstOperand(Value :String);
  4.   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:

  1.   IGUIBaseInterface = interface
  2.     ['{F0B7F031-9302-415E-8545-1FE20A365840}']
  3.   end;
  4.  
  5.   IGUIEdit = interface(IGUIBaseInterface)
  6.     ['{FE2D56FB-0CFB-4B33-9B56-0A523B235D37}']
  7.     procedure SetText(const Value: String);
  8.     function GetText: String;
  9.     function GetAsInteger: Integer;
  10.     function GetAsFloat: Extended;
  11.     procedure SetReadOnly(const AValue: boolean);
  12.     procedure SetVisible(const Value: Boolean);
  13.     function GetTextAsInteger: Integer;
  14.     procedure SetTextAsinteger(const Value: Integer);
  15.     function GetTextAsFloat: Extended;
  16.   end;
  17.  
  18.   IGUISelectableList = interface(IGUIBaseInterface)
  19.     ['{EEFE5C52-94C3-464B-80F2-05E443B0F0F6}']
  20.     procedure SetText(const Value: String);
  21.     function GetText: String;
  22.     procedure SetValue(const Value: String);
  23.     function GetValue: String;
  24.     function GetSelected: ISSKeyValue;
  25.     procedure AddPair(AKey, AValue: String);
  26.     procedure Clear;
  27.   end;

For implementation details see attached sample code.

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

  1. TfrmCalculatorView = class(TForm, ICalculatorView)
  2.   //code
  3. end;
  4.   //interface section
  5. procedure TfrmCalculatorView.FormCreate(Sender: TObject);
  6. begin
  7.   //Link controls with related interface
  8.   IOperators := TGUISelectableList.Create(ComboOperators);
  9.   IError := TGUIEdit.Create(EditError);
  10.  
  11.   //link view and presenter
  12.   //In this version VIEW know PRESENTER
  13.   FPresenter := TCalculatorPresenter.Create(Self);
  14. end;

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.

  1. procedure TfrmCalculatorView.Button1Click(Sender: TObject);
  2. begin
  3.   FPresenter.DoCalc;
  4. end;

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

CodeGear, Delphi for Win32, Design Patterns, MVP, Programming, Uncategorized 3 Comments »

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.

TDataBinder

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

e.g.

  1. 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.

e.g.

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

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

  1. procedure TForm3.FormCreate(Sender: TObject);
  2. var
  3. binder: TDataBinder;
  4. begin
  5. //Create your "BO"
  6. Person := TPerson.Create;
  7. //read data from "database"
  8. Person.Load;
  9.  
  10. //Setup databinding…
  11. binder := TDataBinder.Create(self);
  12. binder.Add(Person, 'FirstName'   ,      Edit1,     'Text');
  13. binder.Add(Person, 'LastName',          Edit2,     'Text');
  14.  
  15. //The same attribute binded to 3 controls
  16. binder.Add(Person, 'Married',     CheckBox1, 'Checked');
  17. binder.Add(Person, 'Married'   , Edit1,     'Ctl3D');
  18. binder.Add(Person, 'Married',     Edit2,     'Ctl3D');
  19.  
  20. //The same attribute binded to 2 controls
  21. binder.Add(Person, 'SomeInteger', ComboBox1, 'ItemIndex');
  22. binder.Add(Person, 'SomeInteger',     TrackBar1, 'Position');
  23.  
  24. //A derived property
  25. binder.Add(Person, 'FullName',     Panel1, 'Caption');
  26.  
  27. //let start…
  28. binder.Bind;
  29. end;

Other info asap so, stay tuned.

Download Code and compiled sample

(Source code require Delphi 2009)

Value Object vs Data Transfer Object

Programming 2 Comments »

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: http://martinfowler.com/bliki/ValueObject.html

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in