What is Glass.Mapper and how to Configure it


What is Glass Mapper?

Glass Mapper is a Sitecore ORM (Object Relation Mapping) created by Sitecore MVP Mike Edwards. An ORM maps data to the objects in your code.

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. and its allows you to map data from Sitecore Database directly into strongly typed C# models.

Glass mapper very famous ORM in Sitecore Community.

WHY?

In Simple way we can say its automatically map the data Sitecore items to C# models. Developers don’t need to write too many code to get data.

Here some advantage about that.

a. Map all basic attributes of a Sitecore Items
b. Supports experience editor
c. Make your code unit testable
d. Large community to support
e. Strongly typed classes



For Comatability With Sitecore version You can go through below URL.

Install Glass Mapper

So let’s start installing and using glass mapper.

Just go on your solution and open manage nuget window and search glass mapper according to your Sitecore version. I am using Sitecore 8.2 update 7.

Before installation you have to make sure that you already added two dlls.

Sitecore.Mvc
Sitecore.Kernel



Once installation complete you will be find glass classes under App_Start folder and some dlls under reference.




So let’s move to create template in Sitecore. After created this one just go on home node and create one item using this template and put layout on particular item.






Configuration

So in glass mapper have Two types of configuration.

1. Attribute Configuration
2. Fluent Configuration

1. Attribute Configuration

As shown below images the models you have to define template id and the filed id. The main purpose to do that in future you want to changed property then it will not break your code. Because it will map field by field id.





2. Fluent Configuration

As shown below images for Fluent Configuration you will need to create mapping configuration class and add the mapping to the configuration factory in GlassMapperScCustom.cs




So its up to your interest which one you want to choose.

But I am using Auto Mapping with virtual keyword. You don’t need to do any other configuration for that.

Now we need to create models to cast this template so I have created one model. Kindly note we are using auto mapping so property should be match with your template.


So let’s move on controller to take this template and bind the result on view page. So we need to write little bit code and pass to view.



we need to bind all property on cshtml


Once you done all the configuration just build your solution and publish the files to see the result on screen.



hopefully it will help during setup glass mapper. any suggestion please put comment!

What's new in glass mapper V5 just go through the below reference link.


Happy Coding!














Comments

Popular posts from this blog

Difference between shared, Versioned and Unversioned fileds in sitecore

How to find broken links in Sitecore.

Setup First Sitecore Helix Example From Scratch (Blank solution)