Close
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.

What is Glass Mapper and how does it help us?

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.

Can we use Glass Mapper with the 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’.

About the author

Charlie Afford

Charlie Afford

Charlie is a Sitecore Developer at Box UK. He is an avid supporter of Agile and Test Driven Development and believes that code quality and testability are key.

Related content

Tech round-up: Apr 22nd

By Charlie Afford

Tech round-up: Apr 29th

By Siraaj Ahmed

We're hiring. Let's talk. View available roles