Version 5 of Glass.Mapper.Sc was released last year. If you are working with Sitecore 9.1, there isn’t formal support for it with Glass.Mapper.Sc v4. So, I decided it was time to upgrade. I also discovered that tutorials and documentation from the Glass team are now a paid training course, which made the learning curve a little more challenging, although it still wasn’t a big deal. I’m going to outline my steps and the references I found.
Getting Started
Shu Jackson created a pretty kick butt overview of Glass and covers what has changed in version 5. Shu Jackson and Courtney Dean also created an excellent video that covers just about everything.
Circular References & Model Depth
The code base that I am working with has some gnarly model classes that need to be redesigned. Always easier said than done. The Glass.Mapper.Sc API changed enough where some of the tricks involving Lazy Loading that allowed these classes to work in the past, no longer worked. Some classes implemented complex fallback logic and exposed self references via a model property. That code needed to be removed and, in some cases, refactored as a method. Ideally, that code will be moved to a service class and out of the model.
Removing Attributes SitecoreNode and SitecoreQuery
Some of the model glasses also relied on the use of Glass.Mapper.Sc model attributes, particularly SitecoreNode and SitecoreQuery. Some of the queries implemented through these attributes no longer worked as they did previously, most notably, any use of relative and lazy. I removed these model properties and re-implemented them as methods, removing the use of the attributes.
[SitecoreQuery("Primary/*", IsLazy = true, IsRelative = true)]
public virtual IEnumerable PrimarySubMenu { get; set; }
public virtual IEnumerable PrimarySubMenu
{
get
{
GetItemsByQueryOptions builder = new GetItemsByQueryOptions();
builder.Query = new Query("Primary/*");
builder.RelativeItem = this.Item;
builder.Lazy = Glass.Mapper.LazyLoading.Enabled;
return _requestContext.SitecoreService.GetItems(builder);
}
}
Item is the model base class reference to the Sitecore Item.
[SitecoreItem]
public virtual Item Item { get; set; }
The web application started to work after these changes were applied, but the project was still compiling with a dizzying number of warnings.
Removing GlassController
Most of my controller classes inherited from GlassController.
public class MenuController : GlassController
{
The classes had GlassController calls like:
MenuParameters parameters = GetRenderingParameters();
and
var somedata = GetDataSourceItem();
It’s a pretty easy conversion to Glass Mapper 5.
public class MenuController : Controller
{
private readonly IMvcContext _mvcContext;
public MenuController()
{
_mvcContext = new MvcContext();
}
...
MenuParameters parameters = _mvcContext.GetRenderingParameters();
var somedata = _mvcContext.GetDataSourceItem();
Removing GlassView
@using Glass.Mapper.Sc.Web.Mvc
@using Someproject.Models.Data
@inherits GlassView<SomeModelClass>
@if (Model != null)
{
<li class="class-of-business-item">
@if (Model.ArtworkImage != null)
{
@Html.Raw(RenderImage<SomeModelClass>(Model, x => x.ArtworkImage, new { Class = "class-of-business-image" }, isEditable: true))
}
<li class="class-of-business-content">
@Editable(Model, x => x.PageTitle)
@Editable(Model, x => x.Body)
</li>
</li>
}
@using Glass.Mapper.Sc.Web.Mvc
@using Someproject.Models.Data
@model SomeModelClass
@if (Model != null)
{
<li class="class-of-business-item">
@if (Model.ArtworkImage != null)
{
@Html.Raw(Html.Glass().RenderImage<SomeModelClass>(Model, x => x.ArtworkImage, new { Class = "class-of-business-image" }, isEditable: true))
}
<li class="class-of-business-content">
@Html.Glass().Editable(Model, x => x.PageTitle)
@Html.Glass().Editable(Model, x => x.Body)
</li>
</li>
}
Simple Examples
Here are a few simple code snippets:
Create a Reference
SitecoreService _sitecoreService = new SitecoreService(Sitecore.Context.Database);
IRequestContext_requestContext = new RequestContext(_sitecoreService);
Relative Query
Set the RelativeItem property. It does what you expect.
GetItemsByQueryOptions builder = new GetItemsByQueryOptions();
builder.Query = new Query("Primary/*");
builder.RelativeItem = this.Item;
return _requestContext.SitecoreService.GetItems(builder);
Query Item(s) By Path
GetItemByPathOptions builder = new GetItemByPathOptions();
builder.Path = “/sitecore-path/…”;
return _requestContext.SitecoreService.GetItem(builder);
...
GetItemsByQueryOptions builder = new GetItemsByQueryOptions();
builder.Query = new Query("/sitecore-path/…/*");
return _requestContext.SitecoreService.GetItems(builder);
Query Item By Id
GetItemByIdOptions builder = new GetItemByIdOptions();
builder.Id = Guid.Parse(“some-guid-string”);
return _requestContext.SitecoreService.GetItem(builder);
Query Item By Item
Replace any Glass Casting with the following:
return _requestContext.SitecoreService.GetItem(SomeItem);
Other Useful Properties
Request a specific language version of an item:
builder.Language = LanguageManager.DefaultLanguage;
Set Glass caching:
builder.Cache = Glass.Mapper.Configuration.Cache.Enabled;
Set Lazy Loading:
builder.Lazy = Glass.Mapper.LazyLoading.Enabled;