Life Through a Lens – Lesser known Glass Mapper features

Mapping types

[SitecoreNode] / Node() – This mapping type allows you to map to another item in Sitecore directly by the string representation of id as a guid or by its path. You would typically map this to another Glass mapped type.
[SitecoreLinked] / Linked() – This mapping type allows you to map to an IEnumerable for items that are linked to the mapped item (think navigate > links in the standard Sitecore ribbon). This (using the configuration options) can be referrers / referred to items.
[Ignore] / Ignore() – this can definitely be handy for example when injecting content from external sources into your models from controller code.

Supported types
Whilst not mapping types, Glass can indeed map to types that are not quite so commonly used.
RuleList – if you map to a rulelist (technically a [SitecoreField] mapping) then you can actually get out a usable set of conditions for use in the Sitecore rules api.
Nullable Mapping – Glass Mapper has long supported mapping to nullable .net types (e.g. int? or Nullable), this includes the majority of regular mappings with HasValue generally referring to whether the Sitecore field was empty or not.

Typed Edit Frames
“Because I am lazy” is the best and most useful introduction to a feature I have seen in a while – brought to to from this V4 Demo at around 26min 15secs. The whole video is well worth a watch, but it shows them in use. Typed edit frames though are a feature to allow the creation of edit frames based upon Glass models.

Mapping to / from external data

One of the great advantages of Glass models being detached from Sitecore (unlike most wrapping based solutions) is the ability to map data from external third party solutions. In the Glass Mapper tutorials, there is a great example of mapping Glass Mapper objects using external data.

Data transfer between environments

Another great advantage of Glass models being detached from the Sitecore item is that, using a Glass model, you can breach the master / web boundary or even environment boundary. So, for example, it would be perfectly easy to write a web service (maybe for transferring data from web to master for the purpose of saving user submitted FAQ’s for example), that would construct a Glass model on the web node, post it to the master node and save it using regular Glass code.

Model Caching
Model caching was primarily designed to allow the caching of models that are used in locations that cannot use regular Sitecore caching. This could be for example, a rendering that contains a form and would thus need to respond to postbacks etc.

Configuration Maps
Configuratioin maps are a wrapper for regular Glass fluent configuration that allows better separation from the models, it was covered in further detail in V4 – Configuration Maps

The version count disabler is a simple structure that allows Glass to behave a little more like the standard Sitecore API, in use it is really this simple

using(new VersionCountDisabler)
   var whatever = sitecoreContext.GetItem<IWhatever>(); // this code will get the item even if the version count is 0

More information on version count disabling can be found at – VersionCountDisabler

Typed Glass Controllers
A relatively recent addition to Glass Mapper has been the addition of a typed GlassController, this was described further in Unit Testing with GlassController

Further Reading / Watching

Glass Mapper V4 release post
Glass Mapper V4 release video

Using RequireJs to manage script dependencies based on attributes in your Sitecore Solution

A common architectural issue I hear amongst developers is how to prevent Sitecore loading all and sundry when it comes to javascript dependencies. Indeed it is often the case that front end developers don’t inform us or assume that we will use a single page type model and therefore don’t really need to worry about the number of script dependencies. The reality is that in actual fact if we included every script that a front end developer used throughout all of the scripts within the site – we would have 20 odd script tags in our markup, something you may well be keen to avoid.

As a solution to this, myself and many other developers including former colleague and Sitecore Solution Architect Richard Seal in his post Using Require to Organize your Sitecore Javascript opt to use an advanced module loader such as RequireJs. If you haven’t used it before, I recommend first reading their documentation as there are definite ways to write your javascript (and in particular jQuery) code to maximise your success with it.

Continue reading