Dusty Eves

dusty.eves@techno-babble.net

Sitecore Architect with Paragon Consulting

Advanced Glass Mapper Tricks

Introduction

There are two kinds of Sitecore developers, those who love the Glass mapper and those who have never used it.  I don’t think I’ve met a single Sitecore developer who was familiar with Glass who didn’t love it.  And with good reason.  It provides a very low effort way to interact with the solutions Sitecore templates as objects POCO objects without loosing any of the advantages of using the Sitecore objects directly.  It’s not without it’s quirks and eccentricities but it is without a doubt with the price of admission.

In a typical MVC application the controller is responsible for hydrating the model.  In Sitecore one of the most common patterns that I see is developers using the default Sitecore controller where the model needed is a 1-1 representation of a single template instance and using a controller to hydrate any model more complex than that.  While the latter method is certainly effective, by adding some additional intelligence with a couple of Glass Mapper tricks we can relieve the controller of the need to hydrate our model.

Sitecore Children

One of the simplest examples is a content component with a header/child relationship.  A good example of this is a multi-tab:

Multi-tab-1

This can be solved with a controller renderings in which the controller collects the children and hydrates the model, or we can create a slightly more clever model:

namespace TechnoBabble.Core.Models
{
	[SitecoreType(TemplateId=Contants.Templates.MULTITAB )] 
	public partial class MultiTab
	{
		[SitecoreField]
		public string Title { get; set; }

		[SitecoreChildren]
		public IEnumerable<ContentTab> Tabs { get; set; }
	}
}

Quick, simple, and easy.  One thing to note, the example above assumes that ContentTab is a Glass object also decorated with a SitecoreType attribute.

Sitecore Query Relative

Continuing our scenario above, sometimes our data is more complicated than just parent/child.  If for example in our site we have explicitly defined navigation, our navigation elements might look something like the following:

Navigation-Tree

Again, with some clever use of the Glass Mapper, we don’t need the controller to hydrate the model.

namespace TechnoBabble.Core.Models
{
	[SitecoreType(TemplateId=Constants.Templates.NavigationRoot)]
	public class MainNavigation
	{
		[SitecoreQuery("./Products/*", IsRelative=true)]
		public IEnumerable<NavigationItem> ProductsMenu { get; set; }

		[SitecoreQuery("./Services/*", IsRelative=true)]
		public IEnumerable<NavigationItem> ServicesMenu { get; set; }
	}
}

Sitecore Query Direct

On occasion we have need to retrieve items from the content tree that are consistent regardless of context.  A good example of this would be Sitecore items that define settings.  Taking our navigation example from above, how would we go about limiting the number of elements that can occur at each level.

namespace TechnoBabble.Core.Models
{
	[SitecoreType(TemplateId=Constants.Templates.NavigationRoot)]
	public class MainNavigation
	{
		[SitecoreQuery("./Products/*", IsRelative=true)]
		private IEnumerable<NavigationItem> ProductsMenuItems { get; set; }

		[SitecoreQuery("./Services/*", IsRelative=true)]
		private IEnumerable<NavigationItem> ServicesMenuItems { get; set; }

		[SitecoreQuery("/sitecore/content/Settings/NavigationSettings/*", IsRelative=true)]
		private IEnumerable<ISettings> Settings { get; set; }

		private int MaxItemsPerLevel 
		{
	 	 	 get 
	 	 	 {
	 	 	 	 return int.Parse(Settings.Single(s => s.Name == "MaxItemsPerLevel"));
	 	 	 }
		}

		public IEnumerable<NavigationItem> ProductsMenu
		{ get { return ServicesMenuItems.Take(MaxItemsPerLevel); } }

		public IEnumerable<NavigationItem> ServicesMenu
		{ get { return ProductMenuItems.Take(MaxItemsPerLevel); } }


	 }
}

For more on these I highly recommend the tutorials found here. You’ll find all of the above there and more.

Like what you see? Something I missed? Have an even cooler way to do the same thing?!?! Let me know in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *