I'm using Glass Mapper V4 with Sitecore 7.2 that is tied in with TDS. TDS is used to auto generate all of my Glass Mapper models. I've also created a number of partial classes to extend the Glass Mapper classes TDS has generated. So far so good.
I have a web form on the website that I want to take advantage of Glass Mapper caching. Here we can see what TDS has generated:
[SitecoreType(TemplateId=ISenior_Surf_Day_Survey_PageConstants.TemplateIdString)] //, Cachable = true
public partial class Senior_Surf_Day_Survey_Page : GlassBase, ISenior_Surf_Day_Survey_Page
{
You can see that //, Cachable = true is commented out by code generation.
Since I can't manually edit my TDS code generated file with all the Glass Mapper classes, I tried to add the cachable attribute to my manual extended partial class.
[SitecoreType(Cachable = true)]
public partial class Senior_Surf_Day_Survey_Page
{
However, this produces a compile error of:
Error CS0579 Duplicate 'SitecoreType' attribute
My question is, how can I go about adding the cachable attribute with code generated models?
You can use the "Custom Data' field on a TDS item's properties tab to specify that a particular class be generated with caching enabled.
The Custom Data field is formatted like a query string, so you could add something like 'cacheable=true' to it (delimited by an ampersand, if you have multiple key/value pairs).
In order to get that to work with code generation, you'll need to specifically look for that custom data key during generation, and then apply the appropriate attribute overload [SitecoreType(Cachable = true)].
If you're using the provided sample .tt files from TDS, then you should have a helper function already defined, called 'GetCustomProperty'. You can use this function like so in your item.tt file to generate the appropriate SitecoreType attribute:
<# if (GetCustomProperty(template.Data, "cacheable") == "true") { #>
[SitecoreType(Cacheable = true)]
<# } else { #>
[SitecoreType]
<# } #>