Attributes which can only be written internally

It is sometimes necessary to allow attributes which are read-only to be writable by the object itself

I do this



1) Create a property InternalChangeCount on your base class, in the tagged values make sure that Bold knows it is a Delphi attribute, with a FIELD for read access and no write access



2) Create the following private methods



procedure TBusinessClassesRoot.StartInternalChange;

begin

  Inc(fInternalChangeCount);

end;



procedure TBusinessClassesRoot.EndInternalChange;

begin

  if InternalChangeCount = 0 then

    raise Exception.Create('EndInternalChange without StartInternalChange');

  Dec(fInternalChangeCount);

end;



function TBusinessClassesRoot.IsInternalChange: Boolean;

begin

  Result := fInternalChangeCount > 0;

end;



3) Override the following



procedure TBusinessClassesRoot.AfterConstruction;

begin

  fInternalChangeCount := 0;

end;



{I have never bothered with this one myself}

destructor TBusinessClassesRoot.Destroy;

begin



  if InternalChangeCount > 0 then

    raise Exception.Create('StartInternalChange without EndInternalChange');

  inherited;

end;





Now all of your classes have the ability to do this



StartInternalChange;

try

  SomeReadOnlyAttribute := 32;

finally

  EndInternalChange;

end;



In your descendant classes you would override ReceiveQueryFromOwned and then do this....



begin

  Result := inherited ReceiveQueryFromOwned(etc, etc, etc);

  if Result then

  begin

    if OriginalEvent = bqMayModify then

      if IsInternalChange then

        Result := True

      else

      begin

        if Originator = M_SomeReadOnlyAttribute then

          Result := False;

      end;

  end;

end;


 

Share this article!

Follow us!

Find more helpful articles: