How can I get a dataset of in-memory objects?

2019-03-26 12:35发布

问题:

Does anyone know of a TDataset descendant that works with Generics and RTTI, so that I can write code like this, and make use of data-aware components in the GUI? :

...
ds:TDataset<TPerson>;
...

procedure DoStuff;
begin    
  ds:=TDataset<TPerson>.create;
  ds.add(TPerson.Create('A.','Hitler',77));
  ds.add(TPerson.Create('O.','Bin Laden',88));
 end; 

This should be possible. The fielddefs can be created via RTTI because the exact type of the data is known. Values can also be automatically marshalled back and forth, so you can both view and edit data that's in a class or a record.

I hate having to write a lot of useless marshalling code, while the required information for that is available via RTTI already.

Or maybe somebody once wrote some sort of TEnumerable <-> TDataset adapter?

Does something like that exist, or should I start writing one?

...

The closest thing that I could find is an (excellent!) example by Marco Cantu, from Mastering Delphi 7, but the code itself doesn't make use of new language features like generics, the new RTTI system, or attributes, and it doesn't work with Unicode delphi. TDataset has changed since D7 too.

回答1:

The TAureliusDataSet included in TMS Aurelius comes very close to that.



回答2:

Take a look at EverClassy Dataset from Inovativa at www.inovativa.com.br/public.



回答3:

another one is Snap Object Dataset http://digilander.libero.it/snapobject/



回答4:

DotNet4Delphi by A-Dato Scheduling Technology from the Netherlands is good for you.

Quotes:

From Torry's Delphi

Hook up any collection to your data aware controls.

DotNet4Delphi implements many .Net collection classes, including generic types like List<> and Dictionary<>. Different from their Delphi counterpart is that our generic collections also implement the non-generic interfaces (IList, IDictionary) allowing you to access your collections in multiple ways. This opens the door to use any collection as a data source for data aware controls which is exactly what the (also included) TListDataset component provides.

It targets Delphi XE and XE2.

It's an open source initiative, Delphi rocks !!!



回答5:

I have found a more relevant resource and can't help sharing it! So relevant that I think it deserves a separate post rather than a mere update in my first answer.


The Dduce library for Delphi XE2-XE6 makes use of TListDataSet<...> a generic dataset component that can be used to expose a generic list as a TDataSet.

The most relevant units pertaining to the implementation of the generic dataset are:

  • DDuce.Components.VirtualDataSet.pas (The original SO post is itself cited by the author within the source code as a reference among others!!!)
  • DDuce.Components.ListDataSet.pas

Class hierarchy:

TDataSet <= TCustomVirtualDataset <= TListDataset <= TListDataset<T>

Yes, it inherits lots of features... my only wish is to have at my disposal a version working with a lessen requirement (Delphi XE without most of the other bells and whistles).

Look and feel: