Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

XPathNavigator Processor #214

Merged
merged 1 commit into from
May 30, 2017
Merged

Conversation

leekelleher
Copy link
Collaborator

This can be used if you need a more raw/direct query to get data from the content-cache.

Also refactored the current XPath processor, extracted out the reusable parts into an UmbracoXPathProcessor base class.


In terms of a use-case, for an events listing page, I needed a dropdown list of all the "in-use" years from the events. I'd previously got the event listing node, looped over all the event-page child nodes, got the event date, built up a list, etc. It worked fine, but not very efficient.

Since the Umbraco content-cache can give you direct access to GetXPathNavigator(), we can use a direct XPath query to get the aggregated data.

Here's my usage...

[InUseYears("$current/ancestor-or-self::homepage/eventListing[1]/eventPage/eventDate[normalize-space(.)]")]
public IEnumerable<int> Years { get; set; }
public class InUseYearsAttribute : XPathNavigatorAttribute
{
	public InUseYearsAttribute(string xpath)
	{
		XPath = xpath;
	}

	public override object ProcessValue()
	{
		var items = base.ProcessValue() as IEnumerable<XPathNavigator>;

		return items
			.Select(x => x.ValueAsDateTime.Year)
			.Distinct()
			.OrderByDescending(x => x);
	}
}

With limit miniprofiler benchmarks, my original processor ran at around 100ms, then using XPathNavigator came down to 0.5ms.

This can be used if you need a more raw/direct query to get data from the content-cache.

Refactored the current XPath processor, extracted out the reusable parts into an "UmbracoXPathProcessor" base class.
@leekelleher leekelleher self-assigned this May 18, 2017
@leekelleher leekelleher added this to the 0.11.0 milestone May 30, 2017
@leekelleher leekelleher merged commit f84f20f into develop May 30, 2017
@leekelleher leekelleher deleted the feature/xpathnavigator-processor branch May 30, 2017 20:34
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

None yet

1 participant