views:

447

answers:

2

I found 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?

+13  A: 

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

Runner
Let me add this too...it will be mostly used by framework designers or component writers. For example you can take a look at DeHL which uses this feature for providing XML serialization to programmers.
vcldeveloper
+1  A: 

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.

mjustin
+1 for components in the protected (not private) section.
Daniele Teti
@Daniele: edited my answer, thanks for the hint
mjustin