4

I wrote some basic TAction classes (like TDataSetAdd or TDataSetReOpen) and it works like a charm. Now I tried to write an action to handle commit of a transaction but for some reason it behaves somewhat unexpectedly. "HandlesTarget" function never shows my transaction component as an object at all although it is on the form? What am I missing? Thanks in advance.

  TFDTransactionAction = class(TAction)
  private
    FTransaction: TFDTransaction;
    procedure SetTransaction(Value: TFDTransaction);
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  public
    function HandlesTarget(Target: TObject): Boolean; override;
    property Transaction: TFDTransaction read FTransaction write SetTransaction;
  end;

  TFDCommitAction = class(TFDTransactionAction)
  public
    procedure ExecuteTarget(Target: TObject); override;
    procedure UpdateTarget(Target: TObject); override;
  published
    property Transaction;
  end;

{ TFDTransactionAction }

function TFDTransactionAction.HandlesTarget(Target: TObject): Boolean;
begin
  Result := ((Transaction <> nil) or
    (Transaction = nil) and (Target is TFDTransaction));
end;

procedure TFDTransactionAction.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (AComponent = Transaction) then
    Transaction := nil;
end;

procedure TFDTransactionAction.SetTransaction(Value: TFDTransaction);
begin
  if FTransaction <> Value then
  begin
    FTransaction := Value;
    if Value <> nil then
      Value.FreeNotification(Self);
  end;
end;

{ TFDCommitAction }

procedure TFDCommitAction.ExecuteTarget(Target: TObject);
begin
  Transaction.Commit
end;

procedure TFDCommitAction.UpdateTarget(Target: TObject);
begin
  Enabled := TFDTransaction(Target).Active
end;

end.
Ken White
  • 123,280
  • 14
  • 225
  • 444
kidbabic
  • 109
  • 2
  • 11
  • 1
    I find this an interesting question but it has a barrier to entry. There's no test app. We'd have to build it ourselves and hope we got it right. That takes time. Maybe somebody will solve it with running code. Or maybe somebody will solve it by building a test app. But if the barrier to entry was not there you already would have an answer. – David Heffernan Feb 03 '15 at 23:06
  • 1
    HandlesTarget acts on `Target` and on `FTransaction`. ExecuteTarget and UpdateTarget act on only one. Target <> FTransaction. Also, your FreeNotification code is incomplete. See [this example](http://stackoverflow.com/a/6806550/757830). – NGLN Feb 12 '15 at 20:52

0 Answers0