Sitecore Habitat/Helix with Glass Mapper

I have used Sitecore Habitat ( you can call it the Modular Architecture) on the latest Sitecore project I have worked on.

It was really a good experience for me. I can say that this project was one of my favorite Sitecore projects of all time. The code was very clean, easy to understand, and of course very flexible. Even I can use the same features on the next projects.

The client liked the structure of the project and they found it very easy to take over the site maintenance. "Everything was separated as features".

I always use Glass Mapper. This time When I started implementing the new project using habitat architecture, I thought of all the ways to use/integrate Glass mapper with the modular architecture, and how to keep all of the features lossly coupled in the presence of glass mapper.

The common approach that I always go with when it comes to using Glass Mapper:

1- Installing Glass mapper in the main web project.

2- Creating a new Library Project that contains all of the Glass models in the website.

In short, the approach above means that we have a single web application project that has a glass mapper installed on, and one library project has all of the glass models.

This is the common approach, and I have seen it on many projects by many developers.

But when it comes to using Sitecore Habitat with glass mapper, things are a little bit different. We have many web applications projects are in the solution, most of them live in the Feature layer, a few of them in the presentation layer, and some of them are in the foundation layer.

The questions and the concerns now are:

1- Where Glass Mapper should be installed?
2- Where the Models should be located?
3- How we are going to reference the glass mapper and where?
4- What about Multi-site? And how to make the features loosely coupled.

Ok! it's a little bit headache, we have to think of many areas, and what can affect our implementation in the future before doing anything.

The approach that I used, and I think it is a good approach can answer the questions above is:

1- In my case, I have two projects that exist in the presentation layer. One is the main public site, and the other is a project that I created for all custom Sitecore configurations or any Sitecore module that need to be maintained from the visual studio.

So I think the answer to the first question "Where Glass Mapper should be installed ?" is to install a Glass mapper in the public website project.

If we have multi-sites and they use Glass mapper, it is better to install Glass in a new project called "Common Site".

2- In Glass Mapper we have a Base Item Model, that has all the base fields such as Id, Language, DisplayName, etc.

The answer to the next question is "Where the models should be located ?"

I created a new project in the foundation layer called "PrjoectName.Foundation.Models", this project contains the base item models and anything common. The Model project will be referenced by the features projects.

3- Ok now we have Glass Mapper installed, and we located the base model. How we are going to use it.

Let's assume that we are going to use a glass mapper for the navigation feature. The navigation feature project implements all of the navigation components in the site (main navigation, footer navigation, breadcrumbs, site navigation, etc).


The first thing we will do:

1- Reference the Models project from the foundation layer.

2- Create all of the needed models for the navigation components in this feature project as below.

The following code is a sample for a glass model that lives in a feature project:

3- Also you have the ability to use Glass Controller in this feature.

Now each feature has its own models and uses the base model from the foundation layer.

This was the approach that I used, I think there are might be other good approaches. The main concern here is to think about the architecture, and how to keep everything loosely coupled when using Sitecore HABITAT!!