February 17, 2017

Implement TDS Code Generation for a Helix Generic Repository Foundation Layer

By: Craig Taylor
February 17, 2017

Implement TDS Code Generation for a Helix Generic Repository Foundation Layer
I recently sat down to implement some of the suggestions from Phil and Jason's amazing book on Sitecore development, Professional Sitecore 8 Development.  I already had a few Helix architecture implementations under my belt, but felt like I could improve on what I had done.  I think it's probably the case after each implementation that you always find things to improve on for the next implementation. In particular, I was trying to get my Helix architecture to be more consistent, efficient and repeatable.

In chapter 5 of the Professional Sitecore 8 Development book, Phil and Jason speak of improving the architectural design by using design patterns.  Specifically, they recommend creating a generic repository foundation layer.  One of the tools that can help accomplish this goal is Glass Mapper.  Using Glass, we are able to let the repository remain generic and we can tell it the type of object to return at runtime.  In order to constrain the generic repository, their examples set the interface of the generic repository to require that the type being passed in implements "ICmsEntity" (a separate foundation layer that is used to represent a Sitecore item)

So, the "ICmsEntity" interface (code lifted directly from Professional Sitecore 8 Development) looks like this:
And the generic repository (code lifted directly from Professional Sitecore 8 Development)  looks like this:
As you can see, the ICmsEntity interface is empty.  It's really just a way to differentiate Sitecore items from other classes/items.  What we want to do is make sure that any interfaces that we create via Glass that represent Sitecore items implement this ICmsEntity interface.

The example in the book goes on to explain how you can utilize an ORM foundation layer to map to all your Sitecore objects.  In my case, I'm using Team Development For Sitecore (TDS) and have have TDS projects generating my code within my feature layers.

To utilize this generic repository and to have it incorporate the ICmsEntity interface constraint, we need to modify the code generation templates that TDS uses.  This is where it gets super-easy to implement. (wait, it's supposed to be difficult, right?)

First, edit the "glassv3header.tt" T4 template to import the namespace of the foundation layer that contains the ICmsEntity interface.  I'll continue to use the namespaces specified in the example from the book for consistency.  See line #11 below:

Now, edit the "glassv3item.tt" T4 template to have the interfaces that are created implement the ICmsEntity interface.  See line #8 below:

Re-generate your code and you're done!  You now automatically have all your Sitecore objects implementing the ICmsEntity interface and your generic repository will be able to retrieve the proper objects.

Additional Reading/Resources

Professional Sitecore 8 Development - You *do* have the book already, right??
Helix - Helix documentation


1 comment:

  1. Very interesting article. I took a slightly different approach and based my repos on IGlassBase and the made sure that only the foundation TDS template project creates the GlassBase entities and then all other TDS template projects had their templates modified so that the entities would inherit the foundation one.

    ReplyDelete