The DeRailed Challenge
Nicolai Waniek
no.spam at thank.you
Fri Feb 9 15:35:46 PST 2007
I don't know RoR, but
Frits van Bommel wrote:
> And reflection support?
You may use reflection to write a persistence layer for a database
application. For example, in Delphi you got access to the Runtime Type
Information (RTTI) with which it is possible to develop some sort of
reflection. Going a step further, it is possible to develop database
components such like a ClientDataSet that contains objects (usually they
just contain a copy of the database-query structure. For example if you
make a "select name, familyname from customer", you may access the
fields in the clientdataset by .fieldByName("name").AsString and so on)
and access its properties the same way as with regular datasets:
objectdataset.fieldByName("MyObjectProperty").AsInteger = 10;
something like that. Then you may concentrate on the objects and let the
software do the persistence on its own and you may directly map objects
into relational databases.
I didn't have a look at D's runtime type information capabilities, but
if there are none, it would be a _really_ nice feature to have - beside
Delphi's "class of " feature to do something like that:
[delphi code]
type
TBaseItem = class(TObject, ...)
property ...
end;
TBaseItemClass = class of TBaseItem;
TSpecializedItem = class(TBaseItem)
// something special
end;
// constructor for a list that gets a "class of" type to create it's own
items:
constructor TMyList.create(AItemClass: TBaseItemClass);
begin
...
end;
// create a list:
myList = TMyList.create(TSpezializedItem);
// the following code then creates an item of type TSpecializedItem
myList.addItem();
[/delphi code]
Well this is possible with D, but a few other ways: use a delegate, use
a template, use an object factory. for the last one:
[D code]
class BaseItem
{
// ...
}
class SpecializedItem
{
// ...
}
class List
{
this (ItemFactory factory)
{
// get an item
factory.getItem();
}
}
class ItemFactory
{
BaseItem getItem() { ... }
}
class SpecializedItemFactory : ItemFactory
{
override BaseItem getItem() { return new SpecializedItem(); }
}
[/D code]
But this seems to be code redundancy as you either have to write a
template for each item or derive a new "ItemFactory" for each item type.
More information about the Digitalmars-d
mailing list