TIHIDI: Hardcode Constants vs. Sitecore Configuration Factory

This blog post will go through how I setup my solution to be able to read everything I need from configuration rather than hardcoding it as part of code.

During a project, you might have the need to store Id’s or strings in your project. What ever you do, DONOT store them as part of code or in a constants file. Use Id’s over paths always.

TIHIDI: Stands for This Is How I Do It. I am going to write a series of blog posts going through how I do Sitecore related work. Hope it helps you!

In the TIHIDI post: TIHIDI: Glass Mapper with Custom Models and Mapping using Fluent Configuration, I ended up adding in the Sitecore configuration factory code to pull in the template Ids.

The concept is very similar to what Mike GoldSuit Reynolds did in one of his blog posts, which I am unable to find the link.

First lets define the interface with all the config elements we need:

Next, lets implement a class inheriting this interface and loading the settings.

Finally create a custom config with the settings you need and specify type to point to the new class.

Now that we have that all setup, we can use it in code. Here is an example in one of my Model mapping classes.

You are not limited to just Ids, you can use this for all your constants needs.

If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).


Add your comment

Your email address will not be published.