Senior Software Developer
Generating Glass Mapper Class with TDS using T4 Template Code Generation
Team Development for Sitecore
Team Development for Sitecore has been around for a while, and it has proven a great tool to help Sitecore Developer reduce deployment time and synchronize their content across the environment. It's easy to use and has a friendly user interface. Glass Mapper is just another well-known Object Relationship Mapper (ORM) that translates your Sitecore data template in Strong type objects. In this blog, I am going to share with you all how to generate Glass Mapper classes with TDS using the T4 Template Code Generation.
Team Development for Sitecore Project
Click here to navigate to the TDS website to download the TDS package. For the demo, I am going to use Sitecore 9.2, and the TDS version that is compatible is Sitecore TDS 220.127.116.11. After downloading it, you will install the TDS version that is compatible with your Visual Studio_._
Figure 1: Sitecore TDS 2019
I am assuming you have installed TDS successfully, so after open Visual Studio, you should be able to see two TDS Visual Studio Templates: Sitecore TDS with Wizard and Sitecore TDS Project. When creating a Visual Studio TDS project, we are going to use the Sitecore TDS project template.
Figure 2: Visual Studio TDS Template
Once you create a project, you are going to have an empty TDS project. All the Sitecore Content will be downloaded after connecting the TDS project with Sitecore. Find your Sitecore Instance URL / Physical folder in inetpub and fill out the property, as shown in the figure below.
Figure 3: TDS Project Windows Properties
Then, right-click on XC.TDS project and select Get Sitecore Items. The windows popup below will show up and choose which Sitecore Items you want to have on the TDS project. Just In case You wish to have all children under the Sitecore item, you have to right-click on the Sitecore item and select all children.
Figure 4: Get all Sitecore Items
Glass Mapper Models Project
Create a new Visual Studio project using the Class Library (.NET Framework) template. It is required before installing the Glass.Mapper.Sc.92, you install both Sitecore.Kernel and Sitecore.MVC dlls. Ensure you select ignore Dependencies before adding Sitecore dlls to the project, but only for Sitecore dlls. When downloading Glass.Mapper.Sc.92 choose lower from Dependency behavior dropdownlist.
Figure 5: Sitecore Kernel and Sitecore MVC dlls
Generate Glass Mapper strong type class with TDS using T4 Template code generation.
Click here to download all T4 templates from the TDS GitHub repository. Before you download the T4 template, I strongly recommend you know about the known issue regarding the T4 template.
Compilers are properly handling both types of line endings, processing the text line by line, and not complaining about problems. This does not appear to be true for template transformation operations. In essence, the string from the template is returned in such a way that it keeps the wrong line ending formatting, which in turn causes unforeseen behavior when compiled and errors like the one above. When dealing with a file with these types of line endings, there are two fixes:
- Make sure you get the file with Microsoft line endings that will not break your build.
- When setting up code generation for the first time and get the T4 templates, do not download; using the "Download ZIP," since the text files of the templates will come with Git's F (Unix / line feed) file endings which is something Visual Studio is sensitive about.
- Instead, by navigating to the template file in GitHub and opting to view the Raw version of the file and copying the text inside to an empty text file should do the trick.
- To do this, one needs to navigate to the location of the .tt T4 template files in GitHub, open one inside a browser and select "Raw":
After being aware of the ending lines character issue, let's enable the code generation on the TDS project, as shown screenshot below.
- Target project: XC.Models
- Code Generation Target File: Entities.cs
- Base Namespace: XC.Models
Figure 6: Enable Code generation
Below are ist of T4 templates that need to add to the TDS project under Code Generation Templates. Then open the XC.TDS project properties to add Header Transform File ( ClassV5Header.tt) and Base Project Transformation File ( GlassV5Item.tt).
Figure 7: T4 Templates Code Generation
Figure 8: TDS project with Sitecore Template and T4 templates
Either click saves on the TDS windows properties or right-click on the TDS project and selects Re-Generate Code for all Items; you should get the following Glass Mapper class based on template synchronized on TDS project.
Figure 9: Glass Mapper Class for Sitecore Site Template
I hope you found this blog post helpful for what you want to accomplish with TDS and Glass Mapper; however, in case you need any help with your implementation do not hesitate in contact us - hit Contact Us .