tags:

views:

89

answers:

2

SubSonic newbe question;

I want to put custom attributes on properties in the generated classes, is that possible?

+4  A: 

It's definitely possible. Is this SubSonic 2 or 3? It's doable for either one, but the question of where to find the templates is different depending on which one you're using.

Edit: For SubSonic 2, follow this link and look at the entry on "templateDirectory". For SubSonic 3, you probably have some .tt files in your VS Project you can mess with.

Skinniest Man
I'm using v.3. The .tt files do not give me access to the properties, do they?What I want to do is mark _some_ properties, not all of them (this is for auditing purposes, I want to audit properties that have values, not props that are to define the structure; IQueriables woth other entities)
iammaz
If you want to add them manually you'll need a buddy class.
Adam
@iammaz: The .tt files absolutely _do_ give you access to the properties. In your .tt file, look for: foreach (Column col in tbl.Columns)You can put all sorts of logic in that foreach loop to determine whether to spit out an attribute on your property. I'm doing exactly this to selectively apply DataMember attributes to my generated properties.
Skinniest Man
Hm, that's very intresting. I'll look into that. Thanks Skinniest Man
iammaz
+1  A: 

There's two ways to do this:

  • If you want to add them at generation time you can do what Skinniest Man suggests and modify the templates to add them.

  • If you want to add them manually things are a little more tricky and you'll need to create a 'buddy class' and apply your custom attributes to that. You can see an example for data annotations here which should illustrate the concept.

Adam
Well, I concur with Ayende on the buddy class concept: "This make me feel ill." If this is the only way to do this then I'd say that we have some shortfalls that need to be addressed by the 'higher uppers'http://ayende.com/Blog/archive/2009/05/04/the-buddy-classes-are-drowning-dry.aspx
iammaz
There's a whole world of issues with buddy classes but they're the only way to do what you want to.
Adam
Ok, I'll look into that. Thank you both for your help, appreciate it.
iammaz