Mohamad
Mohamad

Reputation: 1117

How can I get List of objects with Generic type in Delphi

I have several classes as follows:

Type
  TSystemBaseEntity = class(TPersistent)
  private 
    FID: integer;
  public
    property ID: integer read FID write FID;
  end;

  TProcessHeaderEntity = class(TSystemBaseEntity)
  private
    FHeaderDate: TDateTime;
  public
     property HeaderDate: TDateTime read FHeaderDate write FHeaderDate;
  end;

  TInvoiceHeaderEntity = class(TProcessHeaderEntity)
  private 
    FCustomerId: integer;
  public
    property CustomerId: integer read FCustomerId write FCustomerId;
  end;


  TRequestHeaderEntity = class(TProcessHeaderEntity)
  private 
    FWarehouseId: integer;
  public
    property WarehouseId: integer read FWarehouseId write FWarehouseId;
  end;


  TDataList = class(TPersistent)
  private
    FValues: TObjectList<TSystemBaseEntity>;
  protected
    function SetCaption: string; virtual;
  public
    procedure Execute; virtual; abstract;
    property Values: TObjectList<TSystemBaseEntity> read FValues;
  end;

  TInvoice = class(TDataList)
  public
    procedure Execute; override;
  end;

How can I get list of objects with generic types that inherited from TSystemBaseEntity by Values property?

For example, a list of Invoices(TInvoiceHeaderEntity) or a list of Requests(TRequestHeaderEntity) and access to it's properties.

Upvotes: 0

Views: 3244

Answers (1)

Caleth
Caleth

Reputation: 62686

If you want a TDataList that holds one specific subclass of TSystemBaseEntity, define TDataList to be a generic class, with a type constraint

This is defined as

  TDataList<T:TSystemBaseEntity> = class(TPersistent)
  private
    FValues: TObjectList<T>;
  protected
    function SetCaption: string; virtual;
  public
    procedure Execute; virtual; abstract;
    property Values: TObjectList<T> read FValues;
  end;

And used as

  TInvoice = class(TDataList<TInvoiceHeaderEntity>)
  public
    procedure Execute; override;
  end;

procedure TInvoice.Execute
var
  InvoiceHeader: TInvoiceHeaderEntity;
begin
  for InvoiceHeader in Values do
    ...
end;

If you instead want one type TDataList, with a member that is TObjectList<TSystemBaseEntity>, you cannot use that as a TObjectList<TInvoiceHeaderEntity> etc. Have a look at Wikipedia for an introduction, but also consider the following would be allowed under such a scheme:

procedure DoBadThingsWithGenerics(aDataList: TDataList);
var
  myInvoices: TObjectList<TInvoiceHeaderEntity>
  myRequests: TObjectList<TRequestHeaderEntity>
  Request: TRequestHeaderEntity;
begin
  myInvoices := aDataList.Values<TInvoiceHeaderEntity>;
  myRequests := aDataList.Values<TRequestHeaderEntity>;

  // Some code ...

  myInvoices.Add( TInvoiceHeaderEntity.Create );

  // Some more code...

  for Request in myRequests do
    // Oops, we have a TInvoiceHeaderEntity pretending to be a TRequestHeaderEntity
end;

What you can do here is have a procedure that takes a new list of the decendant type, filtering elements of the Values list that are of the appropriate type and adding them to the new list.

procedure TDataList.FilterByType<S:TSystemBaseEntity> ( intoList: TObjectList<S> );
var
  Value: TSystemBaseEntity;
begin
  for Value in Values do
    if Value is S then
      intoList.Add( Value as S );
end;

Upvotes: 4

Related Questions