I know by TMS Aurelius that we can use the "new" 2010 attributes feature to serialize database table fields into object properties at run-time, for example, and I am not an expert on this deep object oriented schema, so I look into the TMS source code and could not understand how to implement it myself, not for DB, not for XML.
So I've looked for all Google's results on Delphi Attributes
and all that people post are declaration examples and then stops before even showing their examples in action.
Then where are the real world examples of how can we project, declare, code and USE those juiced classes inside a form/executing code?
Does anyone have an example to share here or know a good article that is complete?
Edit1:
The answer should have a TForm
with a TButton
where, when clicked, execute some use of the attribute classes created, do not answer showing just the attribute and classes interfaces, because there are many of those declaration examples as I told before
I must say its not much clear to me what kind of example do you need. IMHO in http://docwiki.embarcadero.com/RADStudio/XE4/en/Overview_of_Attributes is everything you should need, perhaps providing that you have some basic knowledge of annotation and/or aspect programming resp.
An example depends on the way/purpose an author of particular SW used attributes for. You mentioned ORM system: the typical usage here is to annotate member of class representing DB entity with additional information neccessary for DB operation in the backend of such framework. Let assume you have a DB entity having field COMPANY CHAR(32) NOT NULL and you want to represent it in Delphi class:
then you will define attribute TCharColumn with constructor
And usage of such annotation could look something like this:
This piece of program demonstrates, how to define fields in dataset in run-time based on annotation in Delphi. We are limited little bit due lack of named parameters, hence working with parameter list is not flexible as should be e.g. like in Java (compare TMS Aurelius annotation set http://www.tmssoftware.com/site/manuals/aurelius_manual.pdf and http://www.techferry.com/articles/hibernate-jpa-annotations.html
Not sure if the question is asking for real world examples of attribute use or how to serialize db tables into objects using attributes. The example below is a contrived simple one (but an example none the less) showing how to use attributes to log changes to object properties.
Define your custom attribute
The "hello world" of classes TProduct using the attribute
Any class that has a "loggable attribute" can be passed to this method to iterate through the properties and log them.
end;
Example of use:
If you want to declare you own attribute, you can do it like this:
An attribute is a regular class, that has the TCustomAttribute as its ancestor. You implement it as usual:
Now the attribute is declared and implemented, you can just use it:
So now you have an attribute declared and implemented and you have used it to add a display label to some class. The final phase is to use that attribute, since you have a class decorated with it. The code that uses the attribute does not resides in the attribute nor the decorated class, it is implemented in the service layer that will use the decoration.
Let's say we have a class that returns a possible display label for a class:
That method will inspect a class and return its display label, given it exists. Otherwise it returns an empty string: