You are not logged in.
Hello,
TObjectDynArrayWrapper/IObjectDynArray is really useful.
But, by how it works by design is not suitable for using in a server to populate DTO object arrays.
Why?
Because it always destroys the instances of objects on the array that it wraps.
However, the method on the server must pass the created objects, not destroy them when the wrapper is destroyed.
Therefore, I suggest adding the OwnObjects parameter to the TObjectDynArrayWrapper constructor.
This does not change the default behavior of the wrapper.
Something like that:
type
TObjectDynArrayWrapper = class(TInterfacedObject, IObjectDynArray)
private
FOwnsObjects : Boolean;
protected
fValue: PPointer;
fCount: integer;
public
/// initialize the wrapper with a one-dimension dynamic array of TObject
/// If AOwnsObjetcs is True, by default, then wraaper destroy all objects on the list on destroy himself.
constructor Create(var aValue; AOwnsObjetcs : Boolean = True);
/// delete all TObject instances, and release the memory
// - is not to be called for most use, thanks to reference-counting memory
// handling, but can be handy for quick release
procedure Clear;
end;
implmentation
constructor TObjectDynArrayWrapper.Create(var aValue; AOwnsObjetcs : Boolean = True);
begin
fValue := @aValue;
FOwnsObjects := AOwnsObjetcs;
end;
procedure TObjectDynArrayWrapper.Clear;
var i: integer;
begin
if fValue^ <> nil then
begin
// free all objects on aray if OwnsObjects is True
if FOwnsObjects then
for i := fCount - 1 downto 0 do
try
TObjectDynArray(fValue^)[i].Free;
except
on Exception do;
end;
TObjectDynArray(fValue^) := nil; // set capacity to 0
fCount := 0;
end
else
if fCount > 0 then
raise ESynException.Create('You MUST define your IObjectDynArray field BEFORE the corresponding dynamic array');
end;
AB, what do you think?
Offline
I am voting for the extension of the TObjectDynArrayWrapper class by the OwnsObjects parameter.
This change don't break the existing interface (default OwnsObjects is True) and extends class functionality significantly.
Who else supports this idea? :-)
Offline
Count me in, the OwnsObjects parameter would allow us to control the lifetime of the contained objects.
Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.
Offline
Please see https://synopse.info/fossil/info/8b3827a511
Also note that a good old TDynArray wrapper is able to work with registered T*ObjArray values, as you expect.
Online
Thank You, AB
Offline