This blog post will go through how I setup Glass Mapper in a Visual Studio 2015 MVC solution for Sitecore 8.1. I have used other Sitecore ORM solutions but finally ended up liking Glass. Mike Edwards and Nathanael Mann were patient enough to teach me Glass in a short amount of time and the Glass training helped a lot as well.

TIHIDI: Stands for This Is How I Do It. I am going to write a series of blog posts going through how I do Sitecore related work. Hope it helps you!

As usual we will be using NuGet to get the current stable version of Glass, which at this moment is 4.1.2.67.

Before we run the install package for Glass, make sure you have Sitecore.Kernel.dll, Sitecore.Mvc.dll and System.Web.Mvc.dll referenced in your project, which I do since I have been using the same solution for my blog posts.

Once Glass is installed, create two class library solutions, one called Mapping and one called Models.

I create hand coded (yes hand coded you lazy farts!) Models which are true .NET classes or interfaces. These will use .NET types along with Glass field types.

I do the mapping separately in the Mapping project. If the Model can auto map we can add it to the auto maps. If not, we create a specific mapping class for that Model to tackle the mapping.

Install Glass on these two projects as well.

In the SimpleInjector.cs class we will be registering all business logic functionality along with Glass mapping classes. Here is the sample code for SimpleInjector.cs:

Once this is done we have to go and modify our App_Config > Include > SimpleInjector > SimpleInjector.config file to load the Initialize of the Simple Injector before Glass. Here is the updated config file:

For all simple mappings I dump them into a class called PureAutoMaps, so lets create a class and add that mapping. Here is the sample code:

XHTML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

usingGlass.Mapper.Sc.Maps;

usingTIHIDI.Models;

usingTIHIDI.Models.Templates.Content;

namespaceTIHIDI.Mapping

{

publicclassPureAutoMaps

{

}

publicclassTextBlockMap:SitecoreGlassMap<ITextBlock>

{

publicoverridevoidConfigure()

{

Map(x=>

{

ImportMap<IGlassBase>();

x.AutoMap();

});

}

}

}

There is no reason to inherit the IGlassBase except that it provides some additional attributes. You would need the Id attribute in order for you to make this editable in the Experience editor. Again this is just an example, we can highly simplify this as well.

Notice the way you are able to use ImportMap and pull in inherited classes/interfaces. Also notice how we used Automap and .Info in order to specify what Glass needs to do with the BaseTemplateIds. We will get into this in a lot more detail later.

Related

4 comments

Hi Akshay,
Can you please explain the pipeline (TIHIDI.SCExtensions.DI.Pipelines.InitializeSimpleInjectorControllerFactory) which is patched in “SimpleInjector.config” . I don’t see this pipeline explained in this blog .

This is a personal blog. Any views or opinions represented in this blog are my own and do not represent those of people, institutions or organizations that the I may or may not be associated with in professional or personal capacity including past, current and future employers, unless explicitly stated.