The Power of Glass Mapper and Sitecore 7

Pete Withers-Jones

on 18-03-15

Why should you read this?

Manually mapping your code to your data store is an all-too-common strategy; however, proceed with caution. You are automatically committing to an un-repayable burden of technical debt just for maintaining the relationship, which will grow with your Sitecore structure.

Talk to one of our digital experts

Tom Houdmont

Head of Business Solutions

Do you have an idea or a project you need support with?

Tom leads Box UK’s Business Solutions team and has over 15 years experience in the web industry.  Tom is passionate about creating impactful solutions that solve real problems and deliver the outcomes our clients need.

Or call us on 020 8098 2093

What is Glass Mapper?

Glass and Sitecore

Glass Mapper is a powerful object relation mapping framework that models Sitecore objects to strongly-typed classes. Among other things it removes the necessity of mapping code to your data store, manual code mapping and item querying in your view logic.

We don’t have time to get it working

You do have time to get it working! It took us a few days to get it up, working and functional within our project but as a team we saw immediate improvements in productivity and code quality. We also removed technical debt and made unit testing easier.

How do I use Glass Mapper with the Sitecore page editor?

Although we are continuing to learn some of the more advanced features, we were able to use features such as integration with the page editor straight away. All you need to do is inherit from GlassView in your views/partials and then you can use helper methods such as @RenderLink and @RenderImage like so:

@inherits GlassView
@RenderLink(YourModel, link => link.yourLinkObject, new { @class = "attributes" },bool isThisEditable, theTextForTheLink)
@RenderImage(YourModel, img => image, new {@class = "attributes", width = "width",height = "height"})
@Editable(YourModel)

Team Development for Sitecore

Great! You have told us how to remove the burden of manually mapping our code to our data source but don’t we still have to create and manage your Glass Mapper classes? Enter Team Development for Sitecore which has recently added a feature to automatically generate C# classes for your Sitecore templates and update them. I won’t go into specifics but within the properties for your .TDS project you can select to “Enable code generation” which will automatically generate classes in your desired location.

codegeneration

There must be a downside?

We did come across one or two issues with this and integrating with Solr but nothing we could not overcome. The key, as a wise man once said, is to ‘Think like Glass Mapper’.

Pete Withers-Jones

Head of Development

Pete Withers-Jones leads Box UK’s Development Practice, and has over 20 years of experience in software development, working across various industries and technologies.

Subscribe now and get our expert articles straight to your inbox!

"*" indicates required fields

Privacy*
This field is for validation purposes and should be left unchanged.

Have a project you’d like to discuss?

Give us a call on 020 8098 2093 or fill in the form and we will get back to you.

This field is for validation purposes and should be left unchanged.