Step by step guide to add Glass Mapper to Helix solution
How to add Glass Mapper to Sitecore Helix solution
There are probably different ways to set this up in a Helix solution. I have done this setup in 4 of our projects, and it has become the "template" for our new Helix solutions.
We create one foundation project, which has all the glass nuget packages installed, we also choose to add a glass base in the foundation layer.
All new features will have access to this glass base interface through a project reference.
I have had a lot of discussions with my co workers on where to add common fields that would be used in several feature projects. If it should be in the Glass Mapper foundation project or in a new foundation project. On "small" projects we choose to add it in the Glass Mapper foundation project, on larger projects we created a separate foundation project to add these fields and their interfaces with Glass Mapper mapping.
1. Create the Visual studio project
From the Package Manager Console run the command Add-Foundation GlassMapper2. Install Glass Mapper
Go to the project and choose Manage Nuget packages Search for Glass.Mapper and install it3. Create new Interface GlassBase
Add the required fields.4. Update Module-Template-Feature
I uploaded my module template for Feature here.Folder Interface with GlassBase
Created a new folder Interfaces under Code
Under this folder I created a new interface _Name_.cs
Added some example code to map some simple fields.
Added the IGlassBase created in the Foundation project public interface I_Name_ : IGlassBase
Changed Controller to use GlassController
In _Name_Controller.cs controller changed Controller to GlassController
Added reference to using Glass.Mapper.Sc.Web.Mvc;
Under this folder I created a new interface _Name_.cs
Added some example code to map some simple fields.
Added the IGlassBase created in the Foundation project public interface I_Name_ : IGlassBase
Changed Controller to use GlassController
In _Name_Controller.cs controller changed Controller to GlassController
Added reference to using Glass.Mapper.Sc.Web.Mvc;
Added changes to _NamespacePrefix_._ModuleType_._Name_.csproj
Added the interface _name_cs
Added project reference to the Foundation project created earlier.
Added references to the GlassMapper .dlls.
Additional notes
Using Interfaces instead of Models
In my first Glass Mapper project I started with Models.After working with Glass Mapper a few days I found it more flexible when we started to use Interfaces instead.
I created common fields in Sitecore, and mapped those fields to Glass Mapper
On the Model(interface) they can comma separate the fields they need:
IMyComponent : IGlassBase, ITitle, IAbstract, IImage, IGeneralLink, ILinkText
They do not need to add any new mapping since this is already done in the interfaces.
The view will "magically" have access to all of the fields.
This setup has reduced a lot of development time when creating new simple components and pages in our latest projects.
Introducing this to new team members has been quite easy. Showing them a few code examples with methods like GetDataSourceItem and context.GetCurrentItem has made them capable of creating a lot of components and pages without much help from a senior developer
Comments
i have few clarification.
1) If i have TDS in my project, and the code will be auto generated by tds itself. during this situation , how the above approach will be benefical ?