Saturday 6 March 2010

More Attributes

Just got a comment from Mason Wheeler, that he prefers to use attributes to indicate a property to NOT serialise , rather than those properties to serialise. I quite like this idea, as it’s true that if you have quite a few properties to serialise, your class may get rather messy. My only problem with this is that the property name must then be the same as the column name. I guess not a major problem, but still. In a perfect world, you’d have two types of attributes. Those which came before properties, methods or fields and those which came after. Those which came before would work just like the class visibility sections. i.e. they apply to all properties, methods and fields until the next attribute, or section. Those attributes that came after a property, method or field, apply to that item only. thus I could write something like this.

public
[Persist]
property Id : Int64;[Key][ColName(
'Id')];
property FirstName : string read fname;[ColName(
'Name')];
property Surname : string read fSurname;
property Address : string read fAddress;
public
property FullName : string;
//not persisted


I’m not sure how difficult that would have been to parse.

3 comments:

Jolyon Smith said...

Yep, I pointed out the limitations of the chosen syntax when attributes were first mooted.

I even posited a complete, alternate approach, but too late.

http://www.deltics.co.nz/blog/?p=484

Babnik said...

Yes. Jolyon. Just read your post! Can I have your attribute implementation in Delphi please? Extremely similar to what I have just posted. That's two of us coming to almost the same conclusion independently of each other for almost the same reason. There's got to be something in it.

Torbins said...

In my monitor your code looks like:

public
[Persist]
property Id : Int64;[Key][ColName('Id')];
property FirstName : string read fname;
[ColName('Name')];
property Surname : string read fSurname;
property Address : string read fAddress;
public
property FullName : string; //not persisted

To which property belongs second ColName? That's why I prefer something like "[global Persist]".