Return a TList<unknown type>

I want to create a service function that returns a list of an entity.
The entity is unknown for development purposes. It is only passed as a parameter.
Is something like that possible?

I already have a general function internally that returns a list. I just need to make the call typeless

See the last/bottom function
function TReportFunc.list( const Param: TJSONObject): TList;

//*************************************************************************************
//*************************************************************************************
type
  [ServiceContract]
  [URIPathSegment('sf')]
  {$I 'sngModellist.inc'}
  IsngFunc = interface(IInvokable)
  ['{9615BB0F-66A5-4B60-B854-E5098443B6BD}']
    function RegisterService                                                              : TJSONObject;
    function RegisterDefaultSettings                                                      : TJSONObject;
    function InstallDB                                                                    : TJSONObject;
    function UpdateDB          ( const Param: TJSONObject)                                : TJSONObject;
    function Uninstall         ( const Param: TJSONObject)                                : TJSONObject;
    function Backup            ( const Param: TJSONObject)                                : TJSONObject;
    function Restore           ( const Param: TJSONObject)                                : TJSONObject;
    [HttpGet] function Version                                                            : TJSONObject;
    [HttpGet] function DBVersion                                                          : TJSONObject;
    function Callback          ( const CallbackFunction: string; const Param: TJSONObject): TJSONObject;
    function CloseModule       ( const Param: TJSONObject)                                : TJSONObject;
    function Signal            ( const Param: TJSONObject)                                : TJSONObject;
    function GetSetting_list   ( const Param: TJSONObject)                                : TList<TsngDBSettings>;
    function GetSetting        ( const Param: TJSONObject)                                : TJSONObject;
    function SetSetting        ( const Param: TJSONObject)                                : TJSONObject;
    function GetLookupDic                                                                 : TStream;
    [HttpGet] function temp    ( const Filename: String)                                  : TStream;

    //#################################################       <--- This is, what I want
    function list              ( const Param: TJSONObject)                                : TList;  // TList<unknown type>
	//#################################################
	
  end;
  
//*************************************************************************************
//*************************************************************************************

type
  [ServiceContract]
  [URIPathSegment('func')]
  [Model('sngReport')]
  IReportFunc = interface(IInvokable)
    ['{DEC46D08-9170-4EE2-B240-6C7C80B160C5}']
    function generate          ( const Param: TJSONObject) : TStream;
    function preparedesign     ( const Param: TJSONObject) : TJSONObject;  
	
    function ExampleList       ( const Param: TJSONObject) : TList<TExampleEntity>;
  end;

//*************************************************************************************
//*************************************************************************************

type
  [ServiceImplementation]
  TReportFunc = class(TInterfacedObject, IsngFunc, IReportFunc)
  strict private  // Interface IsngFunc
    function RegisterService                                                              : TJSONObject;
    function RegisterDefaultSettings                                                      : TJSONObject;
    function InstallDB                                                                    : TJSONObject;
    function UpdateDB          ( const Param: TJSONObject)                                : TJSONObject;
    function Uninstall         ( const Param: TJSONObject)                                : TJSONObject;
    function Backup            ( const Param: TJSONObject)                                : TJSONObject;
    function Restore           ( const Param: TJSONObject)                                : TJSONObject;
    function Version                                                                      : TJSONObject;
    function DBVersion                                                                    : TJSONObject;
    function Callback          ( const CallbackFunction: string; const Param: TJSONObject): TJSONObject;
    function CloseModule       ( const Param: TJSONObject)                                : TJSONObject;
    function Signal            ( const Param: TJSONObject)                                : TJSONObject;
    function GetSetting_list   ( const Param: TJSONObject)                                : TList<TsngDBSettings>;
    function GetSetting        ( const Param: TJSONObject)                                : TJSONObject;
    function SetSetting        ( const Param: TJSONObject)                                : TJSONObject;
    function GetLookupDic                                                                 : TStream;
    function temp              ( const Filename: String)                                  : TStream;

    //#################################################       <--- This is, what I want
    function list              ( const Param: TJSONObject)                                : TList;   // TList<unknown type>
	//#################################################

  strict private    // Interface IReportFunc
    function generate          ( const Param: TJSONObject)                                : TStream;
    function preparedesign     ( const Param: TJSONObject)                                : TJSONObject;
    
	function ExampleList       ( const Param: TJSONObject)                                : TList<TExampleEntity>;
  end;

//*************************************************************************************

class function EntityList_list<T: class>    ( const Param: TJSONObject)      : TList<T>;
var
  LCriteria   : TCriteria<T>;
begin
  ... code ...

      LCriteria := TdmDatabase.JSONFilterToAureliusCriteria<T>( Tenant, Branch, LsngPara.JSONData);
      if LsngPara.PageSize > 0 then
        LCriteria.Skip( LsngPara.PageIndex * LsngPara.PageSize).Take( LsngPara.PageSize);
      Result := LCriteria.List;
	  
  ... code ...	
	  
end;

//*************************************************************************************

function TReportFunc.ExampleList( const Param: TJSONObject): TList<TExampleEntity>;
begin
  Result := TsngFunc.EntityList_list<TExampleEntity>( Param);
end;

//*************************************************************************************

function TReportFunc.list( const Param: TJSONObject): TList;
begin
  Find class from Param string
  call TsngFunc.EntityList_list<class>( Param)
  make result as in TList<MyClass>
  Result := 
end;

You can try to set the function result type a TList<TObject> and cast your list to that type when setting the function result.

Or, you can set the function result type as TStream and serialize your list to a JSON and return it as TStringStream.

OK, I've almost done everything.
Basically the return works as TList;

TObject( Result) := TObject( TsngFunc.EntityList_list<TRepDatasource>( Param));

This, with an example entity, works well.

But finally
I just have to make an assignment. Unfortunately, I can't find the right function for:

class function MyFunction( Param: TJSONObject): TList<TObject>;
var
  [ ... ]
  LCriteria   : TCriteria;
  Clazz: TClass;
begin
  [ ... ]
      Clazz := TsngRtti.GetClassByName( 'TMyTestEntity');

      LCriteria := TdmDatabase.JSONFilterToAureliusCriteria( Clazz, [ ... ]);

      // Here are my problem
      Result := LCriteria.List( Clazz); // this do not exists
      Result := LCriteria.List<Clazz>;  // this don't work

  [ ... ]

end;

Is there an alternative "List" function?

I think I found it.
It's that simple.

Result := LCriteria.List<TObject>;

Everything is running
Thanks for the tip

Best regards
Thomas

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.