Wednesday, December 4, 2013

A hint how to use collect in OCL

Sometimes I want to merge several list into one big list. Consider this OCL expression

WorkPool.allinstances.buffInProcess->union(WorkPool.allinstances.buffopenplans)->union(Workpool.allinstances.buffforwarded)

So in this case Workpool contain some buffers or multilinks. I want to merge 3 multilinks into one list.
It can be done with collect.

WorkPool.allinstances->collect(buffInProcess->union(buffopenplans)->union(buffforwarded))

The result is exact the same but you do not need to use WorkPool.allinstances on many places.

Another case

Instead of

if myLooongexpression.condition then
  myLooongexpression.attributeone
else
  myLooongexpression.attributetwo
endif
 
do this

myLooongexpression->collect(if condition then attributeone else attributetwo endif)

Sunday, November 10, 2013

Bold need your attention

Bold is propably the best persistant framework for Delphi. Only Embarcadero can make it available again. Tell them that community need Bold and give yor votes on QC. Currently it have 216 votes an place 10, but more is needed. Thanks!

Saturday, July 20, 2013

Future for Bold ?

Many may wonder what happened after my petition ? Well, the discussion with Embarcadero is not dead. There are now 176 supporters on the petition and I am still hopeful that we reach a solution for this issue.

Because it is an issue for: 
  1. For those developers that still use Bold in old but important applications. They are forced to stay with an old Delphi version. Many have chosen to leave Delphi for more active development platforms.
  2. For the whole Delphi community that are not aware what a great ORM framework Bold is. For the right type of application ORM and Bold is a really timesaver.
  3. For Embarcadero that miss those Delphi licenses if those customers above would update.
  4. For my employer Attracs as we may need developers with Bold experience. It is not easy to find these days.
I have promised Embarcadero to not reveal any details, but there have been discussions with Embarcadero about how we should continue with Bold. In the meantime I collected some links of Bold resources. Mostly where people mention Bold and what they want it to be. Yes I know I am involved in many discussions... :)

Blogs and forums
Other resources

Friday, November 9, 2012

Use Custom Events to remove dependencies

I have used Delphi a lot but until now not realized how powerful events are. During the last week I got an Aha experience when I realize what can be done. And how clean code can be written with events.

An event is the same a a function pointer for those that use for example C or C++. Delphi have a lot of predefined events. For example a button can have OnClick, OnExit etc. It's easy to use them as Delphi IDE helps you with that.

If you use custom events it can help you break dependencies  between classes. This make it easier to write tests for the code and refactor it.

An Example
This illustrate a form that create and show a dialog. This dialog may also be called from 2 other forms. In the dialog there are special cases to handle this.
First look at the old code before the change:


frmBook.pas
procedure TBookForm.OrderSearch(Sender: TObject);
var
  vForm: TFindParcelForm;
begin
  vForm := TFindParcelForm.Create(self);
  vForm.ShowModal;
end;

procedure TBookForm.OrderNew(Sender: TObject);
begin
  // Code to make new order
end;


frmFindParcel.pas
type  TFindParcelForm = class(TForm)     // Som declarations
end;

// Dependencies to owner
uses
  frmBook,
  frmPlan,
  frmOrg; 

procedure TFindParcelForm.MakeOrderClick(Sender: TObject);
begin
  // Block1 of code here

  if owner is TBookForm) then
    (Owner as TBookForm).OrderNew(self)
  else if Owner is TPlanForm then
    (Owner as TPlanForm).OrderNew(self)
  else if Owner is TOrgForm then
    (Owner as TOrgForm).OrderNew(self)    

  // Block2 of code here
end;

Let's now use the predefined event TNotifyEvent. It is defined in VCL Classes.pas:


TNotifyEvent = procedure(Sender: TObject) of object;

frmBook.pas
procedure TBookForm.OrderSearch(Sender: TObject);
var
  vForm: TFindParcelForm;
begin
  vForm := TFindParcelForm.Create(self);
  vForm.OnNewOrder := OrderNew;
  vForm.ShowModal;
end;

// Have the same signature as TNotifyEvent

procedure TBookForm.OrderNew(Sender: TObject);
begin
  // Code to make new order
end;

frmFindParcel.pas

type  TFindParcelForm = class(TForm)     // Some declarations  private    fNewOrder: TNotifyEvent;  public    property OnNewOrder: TNotifyEvent read fNewOrder write fNewOrder; 
end;

procedure TFindParcelForm.MakeOrderClick(Sender: TObject);
begin
  // Block1 of code here

  if Assigned(OnNewOrder) then
    OnNewOrder(Self);

  // Block2 of code here
end;

So what happened was that we have broken the dependency from FrmFindParcel.pas to the owners TBookForm, TPlanForm and TOrgForm. This means that it is now easier to test frmFindParcel as a separate unit. Method MakeOrderClick is now simplified and is not aware of the owner. If else is also gone.

But we don't need to stop here. With a small addition we can make own custom events as the blog title suggests.

type
  TSetOrder = procedure(aOrder: TOrder; aPrice: Double) of objects;
  TGetOrder = function(): TOrder of objects;

  TFindParcelForm = class(TForm)     // Some declarations  private    fNewOrder: TNotifyEvent;

    fOnSetOrder: TSetOrder;
    fOnGetOrder: TGetOrder;
public    
    property OnNewOrder: TNotifyEvent read fNewOrder write fNewOrder; 
    property OnSetOrder: TSetOrder read fOnSetOrder write fOnSetOrder;
    property OnGetOrder: TGetOrder read fOnGetOrder write fOnGetOrder;
end;

We added 2 new events OnSetOrder that is a procedure with 2 parameters. And OnGetOrder that just return an order.

Usage:

frmBook.pas
procedure TBookForm.OrderSearch(Sender: TObject);
var
  vForm: TFindParcelForm;
begin
  vForm := TFindParcelForm.Create(self);
  vForm.OnNewOrder := OrderNew;
  vForm.OnSetOrder := SetActiveOrder;
  vForm.OnGetOrder := GetActiveOrder;
  vForm.ShowModal;
end;

procedure TBookForm. SetActiveOrder(aOrder: TOrder; aPrice: Double);
begin
  // Code to set Active Order
end;

function TBookForm. GetActiveOrder: TOrder;
begin
  // Code return active Order
end;

// Have the same signature as TNotifyEvent
procedure TBookForm.OrderNew(Sender: TObject);
begin
  // Code to make new order
end;

frmFindParcel.pas


procedure TFindParcelForm.TestOrderClick(Sender: TObject);
var
  vOrder, vOrder2: TOrder;
begin
  // Block1 of code here

  vOrder := MakeOrder;

  if Assigned(OnSetOrder) then
    OnSetOrder(vOrder);

  if Assigned(OnGetOrder) then
    vOrder2 := OnGetOrder;


  // Block2 of code here
end;

So to break dependencies and make code more testable, own custom events are a powerful tool to accomplish that.

Thursday, June 14, 2012

Vote on Bold for Delphi

Hi!

I have voted on this report on Delphi quality central http://qc.embarcadero.com/wc/qcmain.aspx?d=97382. Please do the same to support a change in this issue!

1. Login, if you don't have account create one.
2. Add 5 votes. That is maximum.

PS
Please join then new group on FaceBook
http://www.facebook.com/groups/279755425455148/

My twitter:
https://twitter.com/#!/d98rolb 

Friday, June 8, 2012

Petition update

To this date 131 signs is reached. I think this is remarkable for a closed product that is unsupported for so many years. I did a grouping for countries and Australia was the leader with 22 signs.
Thanks for all signs about the petition!

The whole list of countries can be downloaded here

Tuesday, May 22, 2012

Petition is published

Yes, this evening a petition is published with the goal to release Bold for Delphi from Embarcadero. Of course I want as many as possible to sign on. Even better if you write why this is important. How would it make your development easier etc.

Get Bold for Delphi back!