Check out the previous posts about the NavigationFactory: Navigation and Navigation (part 2). I presented a way to deserialize the navigation XML into an object model and then serve it through a factory class that uses caching.
In this post, I will present a simple way of locating a certain node in the navigation object model. Locating a NavigationItem is done by comparing it in some way to a given identifier. The most natural way is to retrieve an item by its TcmUri.
In order to do so, I defined the following method on the INavigationFactory interface:
The implementation of this method traverses the Navigation object passed as parameter and applies a Comparator to it, hoping to identify the given tcmUri.
The method calls helper method FindItem that performs the recursive traversal of the Navigation object model.
The UriComparator class is an implementation of the custom interface IComparator that compares the given Uri property to the TcmUri property of the NavigationItem we are currently testing.
Finally, the FindItem method calls the actual comparator's Compare method and performs the depth-first recursive traversal of the NavigationItem passed to it.
As an example, the calling code uses the NavigationFactory.GetItemById() in the following way:
In this post, I will present a simple way of locating a certain node in the navigation object model. Locating a NavigationItem is done by comparing it in some way to a given identifier. The most natural way is to retrieve an item by its TcmUri.
In order to do so, I defined the following method on the INavigationFactory interface:
NavigationItem GetItemById(Navigation navigation, string tcmUri);
The implementation of this method traverses the Navigation object passed as parameter and applies a Comparator to it, hoping to identify the given tcmUri.
The method calls helper method FindItem that performs the recursive traversal of the Navigation object model.
public NavigationItem GetItemById(Navigation navigation, string pageUri) { if (navigation == null) { return null; } IComparator comparator = new UriComparator() { Uri = pageUri }; foreach (NavigationItem item in navigation.Items) { NavigationItem result = FindItem(item, comparator); if (result != null) { return result; } } return null; }
The UriComparator class is an implementation of the custom interface IComparator that compares the given Uri property to the TcmUri property of the NavigationItem we are currently testing.
private interface IComparator { bool Compare(NavigationItem item); } private class UriComparator : IComparator { public string Uri { get; set; } public bool Compare(NavigationItem item) { return Uri != null && item != null && Uri.Equals(item.TcmUri); } }
Finally, the FindItem method calls the actual comparator's Compare method and performs the depth-first recursive traversal of the NavigationItem passed to it.
private NavigationItem FindItem(NavigationItem item, IComparator comparator) { if (item == null) { return null; } if (comparator.Compare(item)) { return item; } if (item.ChildItems != null) { foreach (NavigationItem child in item.ChildItems) { NavigationItem result = FindItem(child, comparator); if (result != null) { return result; } } } return null; }
As an example, the calling code uses the NavigationFactory.GetItemById() in the following way:
INavigationFactory navigationFactory =
DependencyResolver.Current.GetService<INavigationFactory>();
NavigationItem item = navigationFactory.GetItemById(Navigation, "tcm:1-2-64");
Comments