Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical usage for Delphi's new RTTI - Attributes,Values

I found a great explanation about the new RTTI in Delphi,but I don't understand one important thing about all I have read - Where can I use that?

What is it supposed to replace?

like image 332
Ivan Prodanov Avatar asked Apr 17 '10 06:04

Ivan Prodanov


2 Answers

The extended RTTI works like Reflection in .NET. It gives you access to your internal application structure information. You can access class properties, methods etc.. at runtime, at extent you could not do it before.

Some ways of using it:

  1. Serialization / Deserialization of classes to XML or other media
  2. Mapping of objects to databases. ORM.
  3. Cloning of objects
  4. Dynamic invocation of methods
  5. "Scanning" of object at runtime and acting according to that.
  6. Easier development of "plugin" type systems

There is probably a lot of scenarios where it would be beneficial to use it. In short it adds dynamic aspect to your application. Your product is able to do some things at runtime, and more efficiently, than designing everything at designtime. It is not a silver bullet and a lot of people may never use it. But some design patterns or some problems just cannot be solved (at least not in efficient way) without the use of the new RTTI

like image 154
Runner Avatar answered Nov 01 '22 21:11

Runner


One example where extended RTTI could be useful:

Until today, form properties had to be published, because this visibility was necessary to serialize / deserialize Delphi Form Files (dfm).

With extended RTTI, Delphi could use a new streaming layer which would not require this high level of visibility. Of course this is only an idea, but in practice have the advantage that all components on a form could be private or protected. This would be more OOP (encapsulation) and eliminate the 'noise' caused by dozens of irrelevant entries in the list of form fields / properties visible from the outside.


Another typical use case for extended RTTI are Inversion of Control and Dependency Injection frameworks.

For example the Delphi Spring Framework Delphi requires Delphi 2010 (or higher) because of its heavy usage of RTTI, which can be used for constructor injection.

like image 35
mjn Avatar answered Nov 01 '22 19:11

mjn