Glass and SXA - Part 2

In part 1 of this blog post series we look at the two different rendering mechanism in SXA. In this section we will look at how we can use Glass models with SXA.

Before I continue I should mention the excellent work by David San Filippo who has created a module to help you get started with Glass.Mapper.Sc and SXA. You can check this work out using the links below:

  • Blog Post:
  • Github:

If you look at David’s solution you will see that is uses a composite model solution. This is an excellent solution because it separates the Glass model from the SXA model.

I am going to look at a slightly different solution based on the Glass model inherit the RenderingModelBase.

One of the features of Glass is the ability to mix models with different data sources this feature we can use this to our advantage to create an inherited model.

My basic Glass model looks like this:

    [SitecoreType(AutoMap = false)]
    public class MyModel : RenderingModelBase
        public virtual Guid Id { get; set; }

        public virtual string Title { get; set; }

        public virtual string Description { get; set; }

        public virtual Image Logo { get; set; }

Things to note about this model:

  • It inherits from RenderingModelBase
  • The configuration explicitly disables automapping. You must disable automapping to avoid base properties from being mapped, this needs to be done for both fluent and attribute configuration. By disabling automapping this stops Glass from trying to map the properties on the base RenderingModelBase class.
  • Each property is explicitly configured to tell Glass how the property should be mapped.

With our model created we now need to setup our SXA repository:

    public class DemoRepository : ModelRepository
        private readonly ISitecoreService sitecoreService;

        public DemoRepository(ISitecoreService sitecoreService)
            this.sitecoreService = sitecoreService;

        public override IRenderingModelBase GetModel()
            var model = sitecoreService.GetItem<MyModel>(this.PageContext.Current);
            return model;

The model is first created by Glass and then passed to SXA so that it can set the properties it requires. The model can then be passed to the view for rendering using the Glass and SXA features.

The Summary

The choice of using an inherited model or a composite model is a personal choice. I have demonstrated the inherited model above to show one solution but personally I think that the composite model solution developed by David is probably a better way to go. It gives a cleaner separation between Glass and SXA.

You still need to be clear on why you are mixing Glass with SXA in a single rendering and the trade-offs that will have. This mainly relates to using rendering variants in SXA. One area where Glass maybe very useful in a rendering is in the controller. If your SXA rendering needs more complex controller logic based on Sitecore data then Glass can certainly be helpful here.

欧美人与禽交片mp4Using both Glass and SXA in the same solution can work well, where simpler renderings are built with SXA and more complex renderings that are driven by business rules are built using Glass.

What are your thoughts?

- Mike

Glass needs your support!

Glass.Mappper.Sc is supported by the generous donations of the Glass.Mapper.Sc community! Their donations help fund the time, effort and hosting for the project.

These supporters are AMAZING and a huge thank you to all of you. You can find our list of supporters on the Rockstars page.

If you use Glass.Mapper.Sc and find it useful please consider supporting the project. Not only will you help the project but you could also get a discount on the Glass.Mapper.Sc training and join the Rockstars page.

Become a Rockstar