GlassMapper upgrade from 4 to 5

Sitecore Upgrade Journey – Part 2


Hello! I’ve started the Sitecore upgrade journey from my upgrade experiences in my previous article. Part 2 has arrived :D. In this article I will describe the necessary steps related to GlassMapper upgrade.

With GlassMapper upgrade there were a lot of changes introduced, and a lot of deprecated, obsolete classes, methods, contexts etc. I will cover all that I know from personal experience.

1. SitecoreContext

GlassMapper was removed from 5.x SitecoreContext.
This means in the solution, you will need to replace all of the SitecoreContext references with:
1. MvcContext mainly for Controlling Renderings. This context provides access to DataSourceItem, PageContextItem, RenderingItem, and RederingParameters.
2. RequestContext when you are outside of a controller and you don’t have data source item. This is used i.e. computing index fields, pipeline processors, event handlers etc.

When replacing MvcContext, you will see that every method call, such as .GetItem, .Database etc. will return compile errors. This is because you will need to call these from SitecoreService.
In the following example:
sitecoreContext.GetItem will be replaced with mvcContext.SitecoreService.GetItem, sitecoreContext.Database will be replaced with mvcContext.SitecoreService.Database etc.
At chapter 4 there will be a detailed description about these methods.

2. GlassController

GlassController is obsolete, so you will need to replace with other types of controller. In most cases I replaced it with SitecoreController.
GlassController has SitecoreContext property. In case if the controller has used the SitecoreContext property, you have two options:
1. Inherit SitecoreController, add mvc context property on the controller and inject with the mvc context.
2. Create a new controller with mvc context property injected and inherit that new controller for all of the controllers which are using mvc context property.

Example:

public class ArticleController : GlassController
{
private readonly ISitecoreContext _sitecoreContext;
public ArticleController(ISitecoreContext sitecoreContext)
{
_sitecoreContext = sitecoreContext;
}
}

will be replaced with the following example:

public class ArticleController : SitecoreController //or Controller or OtherController
{
private readonly IMvcContext _mvcContext;
public ArticleController(IMvcContext mvcContext)
{
_mvcContext = mvcContext;
}
}

if you need the custom controller, which you can inherit, and then you don’t need to inject MVCContext, the solution is:

using Glass.Mapper.Sc.Web.Mvc;
using Sitecore.Mvc.Controllers;
namespace Test.Foundation.SitecoreExtensions.Controllers
{
public class MvcController : SitecoreController
{
public IMvcContext MvcContext { get; set; }
public MvcController()
: this(new MvcContext(new SitecoreService(Sitecore.Context.Database))) { }
public MvcController(IMvcContext mvcContext)
{
MvcContext = mvcContext;
}
}
}

Example of using the controller:

using System.Web.Mvc;
using Test.Foundation.SitecoreExtensions.Controllers;
namespace Test.Feature.Articles.Controllers
{
public class ArticleController : MvcController
{
public ActionResult Article()
{
var model = MvcContext.GetContextItem<Article>();
return View(model);
}
}
}

3. GlassView

To solve obsolete GlassView you just need to remove this reference from Views and instead of inherit you need to use that model.
Example:

@inherits  Glass.Mapper.Sc.Web.Mvc.GlassView<Test.Feature.Article>

will be replaced with:

@using Test.Feature.Article

On views there will be problems after these changes with the glass helper calls like @Editable, @BeginRenderLink, @Renderlink, @RenderImage etc.
These actions need to be called with Html.Glass() helper.
Example:

@Editable(x => x.Quotation)

will be replaced with:

@Html.Glass().Editable(x => x.Quotation)

Important note! Check all of the viewrenderings cshtml references which used GlassView.
If they have model specified in Sitecore, it’s fine.
If not, you will need to check if the cshtml is using the model properties:
If it’s not using them then it’s also fine, you don’t need to do anything.
If it’s using properties from model, then you will need to assign the model from the cshtml for viewrendering:
So in case of the following ViewRendering:

You will need to change the ViewRendering to have the Model from cshtml.
In my case the cshtml has the following model:

@model Test.Feature.Events.Models.EventViewModel

I will create the model item in sitecore:

And I will assign the created model to ViewRendering:

4. Obsolete methods

In this chapter, I will list all of the obsolete methods which it was changed, renamed or deleted, and workaround to them.

4.1 GetCurrentItem

sitecoreContext.GetCurrentItem<Test>()

will be replaced with:

mvcContext.GetContextItem<Test>()

4.2 Cast

sitecoreContext.Cast<Test>(item)

will be replaced with:

mvcContext.SitecoreService.GetItem<Test>(item)

4.3 DatasourceItem

sitecoreContext.GetItem<Test>(DatasourceItem)

will be replaced with

mvcContext.GetDatasourceItem<Test>()

in case of datasource there is a method call also:

GetDataSourceItem()

will be replaced with

mvcContext.GetDataSourceItem()

4.4 RenderingItem

GetRenderingItem()

will be replaced with

mvcContext.GetRenderingItem()

4.5 Map

sitecoreContext.Map<Test>(item)

will be replaced with

mvcContext.SitecoreService.Populate<Test>(item)

4.6 CreateType

sitecoreContext.CreateType<Test>(item)

will be replaced with

mvcContext.SitecoreService.GetItem<Test>(item)

5. GetItem options

5.1 InferType:

sitecoreContext.GetItem(path, inferType:true)

will be replaced with:

mvcContext.SitecoreService.GetItem(path, x=>x.InferType());

Other example in case of annotation can be found on 6.2.

5.2 LazyLoading:

Note: from default LazyLoading it’s true, if you change to false, even if with 4.x or older glass it was working fine, it can happen that it won’t work with 5.x because of performance improvements.

sitecoreContext.GetItem(path, isLazy:true)
sitecoreContext.GetItem(path, isLazy:false)

will be replaced with:

mvcContext.SitecoreService.GetItem(path, x=>x.LazyEnabled())
mvcContext.SitecoreService.GetItem(path, x=>x.LazyDisabled())

Other example in case of annotation it can be found in section 6.1.

5.3 Language:

sitecoreContext.GetItem(path, language:lang)

will be replaced with:

mvcContext.SitecoreService.GetItem(path, x=>x.Language(lang))

Other examples can be found in sections 6.4 and 6.5.

5.4 TemplateId:

mvcContext.SitecoreService.GetItem(path, x=>x.TemplateId(id))

Another example in case of template annotation can be found in 6.2 section

5.5 Version:

sitecoreContext.GetItem(path, version:ver)

will be replaced with:

mvcContext.SitecoreService.GetItem(path, x=>x.Version(ver))

Another example can be found in section 6.3

5.6 Cache:

[SitecoreType(Cachable = true)]
public virtual Company Company { get; set; }

[SitecoreType(Cachable = false)]
public virtual Company Company { get; set; }

will be replaced with:

[SitecoreType(Cache = Glass.Mapper.Configuration.Cache.Enabled)]
public virtual Company Company { get; set; }

[SitecoreType(Cache = Glass.Mapper.Configuration.Cache.Disabled)]
public virtual Company Company { get; set; }

In case of GetItem options, the following solutions will be used:

mvcContext.SitecoreService.GetItem(path, x=>x.CacheEnabled())
mvcContext.SitecoreService.GetItem(path, x=>x.CacheDisabled())


6. GetItemBy option calls

6.1 GetItemByQuery:

[SitecoreQuery("/Family", IsRelative = true, IsLazy = true)]
public virtual Family Family { get; set; }

will be replaced with

private Family _family;
public virtual Family Family
{
get
{
if (_family != null)
{
return _family;
}
GetItemByQueryOptions builder = new GetItemByQueryOptions();
builder.Query = new Query("/Family");
builder.Lazy = Glass.Mapper.LazyLoading.
builder.RelativeItem = Sitecore.Context.Database.GetItem(new ID(this.Id));
return new RequestContext(new SitecoreService(Sitecore.Context.Database)).SitecoreService.GetItem<Family>(builder);
}
set
{
this._family = value;
}
}
view raw Family.cs hosted with ❤ by GitHub

6.2 GetItemsByQuery:

[SitecoreQuery("/Families", InferType = true, TemplateId = "{E7A480B9-F931-4D37-AA56-58264A26F49F}")]
public virtual IEnumerable<Family> Families { get; set; }

will be replaced with:

private IEnumerable<Family> _families;
public virtual IEnumerable<Family> Families
{
get
{
if (_families != null)
{
return _families;
}
GetItemsByQueryOptions builder = new GetItemsByQueryOptions();
builder.Query = new Query("/Families");
builder.InferType = true;
builder.TemplateId = "{E7A480B9-F931-4D37-AA56-58264A26F49F}";
builder.RelativeItem = Sitecore.Context.Database.GetItem(new ID(this.Id));
return new RequestContext(new SitecoreService(Sitecore.Context.Database)).SitecoreService.GetItems<Family>(builder);
}
set
{
this._families = value;
}
}
view raw Families.cs hosted with ❤ by GitHub

6.3 GetItemByIdOptions:

Document doc = sitecoreContext.GetItem<Document>(document.ItemId.ToGuid(), version: ver);

will be replaced with:

var options = new GetItemByIdOptions(document.ItemId.ToGuid()) { Version = ver};
Document doc = mvcContext.SitecoreService.GetItem<Document>(options);

6.4 GetItemByPathOptions:

var event = sitecoreContext.GetItem<Event>(eventPath, english);

will be replaced with:

var options = new GetItemByPathOptions(eventPath) { Language = english };
var event = mvcContext.SitecoreService.GetItem<Event>(options);

6.5 GetItemByItemOptions:

var news = sitecoreContext.GetItem<News>(item, english);

will be replaced with:

var options = new GetItemByItemOptions(item) {Language = english};
var news = mvcContext.SitecoreService.GetItem<News>(options);

These were my changes for GlassMapper upgrade. I hope that here you have found the code needed for your upgrade:D

I wish a lot of patience for GlassMapper upgrade 😀

Journey to be continued.. 🙂

Published by Gerhart Magdas

I'm a Sitecore developer since 2017 and Sitecore Technology MVP 2022

One thought on “GlassMapper upgrade from 4 to 5

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: