In this post, I will talk about What’s New and Changed in Glass Mapper 5.
Major Changes at a Glance
- Changes to NuGet packages
- SitecoreContext, GlassView, GlassController are obsolete
- New contexts: IMvcContext, IRequestContext, IWebFormsContext
- SitecoreService methods parameters updates
- Changes to LazyLoading
- A new way of loading Glass configurations when working with Helix.
Obsolete Features
ISitecoreContext, GlassView, GlassContoller are obsolete.
You don’t need them any longer. ISitecoreContext was responsible for a mixture of database access and request context.
It is replaced with three new Contexts now available:: - IMvcContext,
- IRequestContext,
- IWebFormsContext,
as they have a much clearer separation of responsibilities and fit closer with Sitecore Architecture. Any value GlassView and GlassController were providing is available through other means. One of the major functions of GlassController, for example, was providing access to SitecoreContext, which is obsolete in the new version.
Previously you were getting Datasource or rendering parameters of the Glass Controller. Now you can get those from MvcContext.GlassView allowed you to use helper methods in your .cshtml files to work with the Experience Editor without requiring to specify the model as it implicitly knows about the model property.
IMvcContext
This is The first one of the three contexts V5 offers.
You used IMvcContext when you are working with MVC, and it’s mainly for Controlling Renderings.
This context provides access to:
- DataSourceItem,
- PageContextItem,
- RenderingItem, and
- RederingParamteres.
Under the hood, it uses SitecoreService to talk to Sitecore.
IRequestContext
You use Request Context when you are outside of a controller and do not have a DataSource item available.
Common scenarios to use RequestContext include computed fields, pipeline processors, and event handlers. You can get ContextItem, HomeItem the RootItem from this context.
IWebFormContext
DO NOT USE WEB FORMS!
Changes to ISitecoreService
The ISitecoreService interface represents an individual Sitecore database. It provides the ability to perform all basic CRUD functions against the selected Sitecore databases.
Essentially it is a wrapper for a Sitecore database. A common usage scenario is to access the core database.
Here is a list of types of Options classes:
- – `GetItemByIdOptions`
- – `GetItemByItemOptions`
- – `GetItemByPathOptions`
- – `GetItemByQueryOptions`
- – `GetItemOptions`
- – `GetItemOptionsParams`
- – `GetKnownOptions`
Alternatively, you can use two parameters, with the second one being a configuration map for the attributes you want to include.
As an example, what used to be:
sitecoreService.GetItem("/sitecore/Home", inferType:true);
can now be:
service.GetItem("/sitecore/Home",x=>x.InferType());
The attributes can be:
- InferType
- Lazy
- Cache
- EnforceType
- TemplateId
- Version
- Language
Comments
Post a Comment