8

So I am architecting a solution that uses Glass in a Helix implementation. We would like to automatically generate the Glass models using a T4 template, but one of the challenges we are running into is that we have a feature that relies on base item types and it's recreating the model for those base types multiple times.

So an example from the habitat project, is that you have the "Feature" Metadata, which contains a base template _PageMetadata. We would then have either other Feature or Project templates that define _PageMetadata as a base template. When the T4 generator runs, it generates models for the pages that we created in the "Feature" or "Project", but also it recreates the _PageMetadata model, even though it would also get generated in the Sitecore.Feature.MetaData project.

Has anyone come across this challenge with Helix and the use of auto-generated glass models?

3

2 Answers 2

4

Code generation can be difficult when working on Sitecore templates (where multiple inheritance is possible) for C# models (where only single inheritance can work). The example code gen templates for Glass Mapper use interfaces to overcome this, which means there's duplication of the properties across models....however another possibility is model composition, which trims down the amount of generated code by not re-defining the properties.

Either way, when doing this inheritance across TDS projects, you need your TDS project to know about any other TDS projects that contains base templates. This is done on the 'Multi-project Properties' tab on the TDS Project Properties. enter image description here

Now for the current project above, my TDS items will be able to 'see' the base templates my items inherit from the the other project, and will be able to do code gen on them successfully. For the interface method, this means adding an interface for the base model, and then defining all of the properties from that base model, in my own template.

An example of this can be seen in the LaunchSitecoreTDS repository, when TDS.MVC.Master has a link to TDS.MVC.Base for it's base template items.

This won't recreate the base model entirely.... but with the default Glass T4 templates, it will use the base model as an interface, so will kind of 'recreate' the fields from that base model in your own model. Using composition will avoid this property duplication, however it shouldn't be a code-breaking issue.

If, however, you're finding the properties for the base templates are recreated multiple times in your descendant template.... then check if you have multiple inheritance of that base template somewhere. It's possible the code gen is adding the fields twice from the multiple inheritance of the same base template.

3

Kam Figy wrote a tool called Leprechaun that can do what you're trying to do. It uses Roslyn code gen instead of T4 templates, but the premise is about the same.

It uses Sitecore items, serialized by Rainbow, and creates models around them. It's currently built for Synthesis out of the box, but you can create a new template based off your Glass T4 template and it'll work just fine. I actually used it to create Fortis models a couple weeks ago.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.