How to stop using strings for view and controller names

I am not a fan of magic strings.

It's not just about compile-time vs runtime checking. It's about not littering your code with stuff sensitive to typos and having to do a global search and replace it in case something gets changed. Plus, there's no way to have them gathered somewhere all together so you can see how many different things you have as strings inside your code.

One thing that particularly annoys me is viewing names and paths. Although it's common practice to have them in code as strings, I personally find it too messy. And then, there are controller and action names, although there are some ways to avoid that part.

I have seen a lot of approaches to this, including people maintaining a custom-made class (or a bunch of constants) with all the names, referencing it in their code, and manually updating it when something changes (yikes!)

For this reason, I tried T4MVC on some projects. For those of you who don't know what this is, it's a T4 template that automatically generates view, controller, and action names in code (it actually creates a hierarchy of classes with those names as properties) so you can use them in code instead of strings and take advantage of autocomplete as well.

But T4MVC has some restrictions, and the most important is that it cannot "sit" outside the project your views and controllers are in. There have been some third-party modifications to allow it to exist on a second project, but it's still troublesome since it still dictates your project structure.

So the only viable solution was to go on and create my own version, which I'm sharing with you in case it proves useful to someone. This T4 template will create view names and controller names (for all projects in a solution) as constants that can be used by any project (no action names though). I usually either put it in a project referenced by all other projects, or in its own project. 

Note that this is not limited to Umbraco CMS - it can be used in any ASP.NET MVC solution. After all, "run custom tool" sounds way better than "I changed something, now let's go find all the points where it breaks stuff" :)

So what this does is essentially generate code where all your view paths and controller names (in all projects) are represented as constants. All you have to do is specify the folder the views are in, the name of the controller's folder (must be common on all projects containing controllers), and the name of the project your T4 file will be in (to create the namespace). The resulting code looks something like the below:

public const string Views_Shared_Molecules__SectionTitle = "~/Views/Shared/Molecules/_SectionTitle.cshtml"; 
 
public const string Views_Shared_Organisms__Contact = "~/Views/Shared/Organisms/_Contact.cshtml"; 
 
public const string Views_Shared_Organisms__ListFilters = "~/Views/Shared/Organisms/_ListFilters.cshtml"; 
 
public const string Views_Shared_Organisms__ListFiltersForSearch = "~/Views/Shared/Organisms/_ListFiltersForSearch.cshtml"; 
 
public const string Views_Shared_Organisms__SearchResultsList = "~/Views/Shared/Organisms/_SearchResultsList.cshtml"; 
 
public const string Controllers_DotSee_PageMetas__Hreflang = "Hreflang"; 
 
public const string Controllers_DotSee_PageMetas__PageMetas = "PageMetas";

So now you can have Intellisense and get your code to use those constants instead of ugly strings, like below:

@Html.Partial(ViewNameConstants.Views_Shared_Molecules__SectionBlockTitleAlt, Model)

If you need to change the separator from an underscore to something else it's very easy to do so.

You can find the T4 template along with instructions and examples here: https://github.com/sotirisf/StronglyTypedViewNames

Happy coding!

Banner Planes Red

Looking for Umbraco
Development & Support Services?

Get up to 25% off standard prices when you combine a Monthly Support plan with Retainer package and / or Hosting

 

Check out our plans