Tag Archives: glassmapper

GlassMapper update

Sitecore Upgrade to 10 – Glass.Mapper 5 and Windsor

If you use WindsorContainer and upgrading Glass Mapper to 5, you need to change the registration to the container

 public class ContextInstaller : IWindsorInstaller
 {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
             container.Register(
                Component.For<IRequestContext>().ImplementedBy<RequestContext>().LifestylePerWebRequest(),
                Component.For<IMvcContext>().ImplementedBy<MvcContext>().LifestylePerWebRequest(),
                Component.For<IWebFormsContext>().ImplementedBy<WebFormsContext>().LifestylePerWebRequest(),
                );
        }
}

The GlassMapperScCustom need to be extended to install the ContextInstaller. This should be done in CreateResolver() method

var container = new Castle.Windsor.WindsorContainer();
container.Install(new ContextInstaller());
GlobalConfiguration.Configuration.Services.Replace(typeof(System.Web.Http.Dispatcher.IHttpControllerActivator),
               new WindowsControllerActivator(container));
           System.Web.Mvc.ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container.Kernel));

For more information upgrading Sitecore, see this post.

How to upgrade GlassMapper is described in this post. However, here is the official page.

GlassMapper update

Sitecore Upgrade to 10.2 – GlassMapper upgrade from 4 to 5

SitecoreContext

SitecoreContext is no longer available in GlassMapper 5+ and need to be replaced with MvcContext (used mainly in Controllers) or RequestContext (used outside Controllers).

So for every piece of code

private readonly ISitecoreContext _sitecoreContext

should be changed to

private readonly IRequestContext _sitecoreContext;

or

private readonly IMvcContext _sitecoreContext;

Note that GetItem method and etc need to be called from SitecoreService.

_sitecoreContext.GetItem<Item>("{8A1AC22A-EADC-46CE-9954-88BB2680AB83}")

should be changed to

_sitecoreContext.SitecoreService.GetItem<Item>("{8A1AC22A-EADC-46CE-9954-88BB2680AB83}")

Another example is where in the class method the SitecoreContext is initialized

For example

var context = new SitecoreContext();

need to be changed to

var context = new RequestContext(new SitecoreService(Sitecore.Context.Database)).SitecoreService;

GetHomeItem

_sitecoreContext.GetHomeItem<ISiteRoot>();

will be changed to

 context.SitecoreService.GetItem<ISiteRoot>(Sitecore.Context.Site.StartPath)

GlassView is obsolete

All views need to be updated as follow:

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<MyNamespace.MyViewModel>

 @Editable(Model, x => x.Item.Title)

to

@model MyNamespace.MyViewModel
@Html.Glass().Editable(Model, x => x.Item.Title)

GetCurrentItem

The new version GetCurrentItem is marked obsolete and replaced by GetContextItem method

 Item = _sitecoreContext.GetCurrentItem<MyModel>()

should be changed as follow, note that _sitecoreContext is now IMvcContext

 Item = _sitecoreContext.GetContextItem<MyModel>()