Sitecore Habitat 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 the time. The code was very clean, easy to understand and of course very flexiable. 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 were 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 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 use 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, few of them in the presentation layer, and some of them are in the foundation layer.

The questions and the conserns now are:

1- Where Glass Mapper should be installed ?
2- Where the Models should be located ?
3- How we are going to reference 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 do anything.

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

1- In my case, I have two projects 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 visual studio.

So I think the answer of the first question "Where Glass Mapper should be installed ?" is to install 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 have all the base fields such as: Id, Language, DisplayName, etc.

The answer of the next question "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.

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

The fisrt thing we will do:

1- Reference the Models project from the foundation layer.

2- Create all of the needed models for the navigation componants 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 it's 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 archticture, and how to keep everything loosely coupled when using Sitecore HABITAT!!