c# - How to lazy load a property with custom attribute -
c# - How to lazy load a property with custom attribute -
i want create custom attribute using glass mapper getting sitecore url, because not possible lazy load property sitecoreinfo(sitecoreinfotype.url)
, have performance issues loading url of mapped items, url never used.
here i've got far:
the configurationpublic class sitecoreurlconfiguration : abstractpropertyconfiguration { public sitecoreinfourloptions urloptions { get; set; } public bool islazy { get; set; } }
the attribute public class sitecoreurlattribute : abstractpropertyattribute { public sitecoreurlattribute() { this.islazy = true; this.urloptions = sitecoreinfourloptions.default; } /// <summary> /// gets or sets value indicating whether lazy. /// </summary> public bool islazy { get; set; } public sitecoreinfourloptions urloptions { get; set; } public override abstractpropertyconfiguration configure(propertyinfo propertyinfo) { var config = new sitecoreurlconfiguration(); this.configure(propertyinfo, config); homecoming config; } public void configure(propertyinfo propertyinfo, sitecoreurlconfiguration config) { config.urloptions = this.urloptions; config.islazy = this.islazy; base.configure(propertyinfo, config); } }
the mapper public class sitecoreurlmapper : abstractdatamapper { public override object maptoproperty(abstractdatamappingcontext mappingcontext) { var context = mappingcontext sitecoredatamappingcontext; if (context == null) { throw new mapperexception("mapping context null"); } var item = context.item; var scconfig = this.configuration sitecoreurlconfiguration; if (scconfig == null) { throw new mapperexception("sitecoreurlconfiguration null"); } var urloptions = utilities.createurloptions(scconfig.urloptions); urloptions.language = null; // now, what? } }
so far, good. how can lazy load url in mapper? have idea?
the way see map lazy<t>
, add together new property class returns value of when accessing it. in mapper, set // what?
homecoming lazy string:
return new lazy<string>(() => linkmanager.getitemurl(item, urloptions));
then in model, set these 2 properties:
[sitecoreurl] public lazy<string> lazyurl { private get; set; } [sitecoreignore] public virtual string url { { homecoming this.lazyurl.value; } }
c# sitecore glass-mapper
Comments
Post a Comment