Quantcast
Channel: Yet Another Tridion Blog
Viewing all articles
Browse latest Browse all 215

A DD4T.net Implementation - Navigation

$
0
0
Most of the websites I have worked on already existed before upgrading them to DD4T. This means the navigation has already been implemented, usually as means of publishing an XML file from Tridion (most always called navigation.xml ;-) ), and then rendered dynamically at request time by means of XSLT transformation.

This setup makes it quite easy to implement navigation in DD4T. I always try to reuse what I have, and the existing navigation.xml is a perfect candidate. We can reuse the Tridion template and Template Building Block that generates the navigation without any modification.

The generated navigation.xml can be even published to file-system, so other parts of the website can still use it. Alternatively, we can store it in the Content Delivery DB.

The approach for generating navigation in DD4T relies in creating an object model from the navigation XML by deserialization, then caching it for faster performance. Everything is wrapped inside a singleton factory wired through Ninject (or whatever other dependency injection framework you happen to use), and voilà -- you've got navigation.

Object Model

First we need a model to hold the navigation objects. We have the navigation XML, which usually represents a structure of very similar nested nodes.

<root>
<navuri="tcm:1-2-4"title="Root"url="/default.aspx">
<navuri="tcm:1-3-4"title="Products"url="/products/default.aspx">
<navuri="tcm:1-4-64"title="Product 1"url="/products/product-1.aspx"/>
<navuri="tcm:1-5-64"title="Product 2"url="/products/product-2.aspx"/>

We need to create model classes with properties for each attribute in the XML. Luckily, Visual Studio has already a tool that does that automatically -- xsd.exe.

C:\Temp>xsd navigation.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.33440]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\Temp\navigation.xsd'.

C:\Temp>xsd navigation.xsd /classes
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.33440]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\Temp\navigation.cs'.

The execution of the xsd.exe program above generates model class navigation.cs, which after some beautification, looks something like this:

usingSystem.Xml.Serialization;

publicpartialclassnav
{
private nav[] nav1Field;
privatestring uriField;
privatestring titleField;
privatestring urlField;

[XmlElementAttribute("nav")]
public nav[] nav1
{
get { returnthis.nav1Field; }
set { this.nav1Field = value; }
}

[XmlAttributeAttribute()]
publicstring uri
{
get { returnthis.uriField; }
set { this.uriField = value; }
}

[XmlAttributeAttribute()]
publicstring title
{
get { returnthis.titleField; }
set { this.titleField = value; }
}

[XmlAttributeAttribute()]
publicstring url
{
get { returnthis.urlField; }
set { this.urlField = value; }
}
}

And after some more beautification, the code looks like this (note that I added property ParentItem and marked it ignorable while deserialization -- this will hold a reference to the parent navigation item):

usingSystem.Xml.Serialization;

[XmlRoot(Namespace = "", ElementName = "root", IsNullable = false)]
publicpartialclassNavigation
{
[XmlElement("nav")]
public NavigationItem[] Items { get; set; }
}

[XmlRoot(Namespace = "", IsNullable = false)]
publicpartialclassNavigationItem
{
[XmlElement("nav")]
public NavigationItem[] ChildItems { get; set; }

[XmlIgnore]
public NavigationItem ParentItem { get; set; }

[XmlAttribute("uri")]
publicstring Uri { get; set; }

[XmlAttribute("title")]
publicstring Title { get; set; }

[XmlAttribute("url")]
publicstring Url { get; set; }
}

Deserialization

Next, we need to deserialize the XML into our model class. The method below performs the deserialization of an XML navigation file. Similar code could be used to deserialize a String representing navigation XML coming from the database.

private T ParseXml<T>(string filePath) where T : class
{
try
{
using (XmlReader reader = XmlReader.Create(filePath,
newXmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document }))
{
returnnewXmlSerializer(typeof(T)).Deserialize(reader) as T;
}
}
catch (IOException ioe)
{
LOG.Error("Can't read navigation file " + filePath, ioe);
returnnull;
}
}

The ParseXml method is to be called from the NavigationFactory, but more about that in a follow-up post.

    Navigation navigation = ParseXML<Navigation>(navigationFilePath);

Check out the follow-up post Navigation (part 2) for a showcase of the NavigationFactory and a way of creating a fully-linked navigation object model.



Viewing all articles
Browse latest Browse all 215

Trending Articles