T4 templates for strongly typed ASP.MVC
MVC is a software pattern, that has been first introduced 1979 by Norwegian scientist Trygve Reenskaug. The idea was to decouple the tight knot between views and models, to have a much more control over the software.
For example, very known ASP.NET 2.0 technology had the implementation of views, but controllers and models were combined in the code behind, which has the impact on the testing and other features, which is needed to build a easy proficient sustainable web solution.
Year ago Microsoft has launched a framework ASP.NET MVC for supporting software pattern and add additional value with different view engines , such as Razor, to eliminate this requirement. Since then, developers are having few consideration about choosing between ASP.NET WebForms and ASP.NET MVC, but that is topic for another blog post.
MVC grew very quickly and alot of developers are using the new .NET framework to write efficient, light weight, powerfull web apps. Even though we do have a great enviroment for writing, testing, debugging .NET apps, there is still a lack of string and mis-machted typos.
Consider following example:
<%:Html.ActionLink(»My link«,«Indeks«,«Home«) %>
This is a link to the Home controller, with a method Indeks. But wait, there is no indeks method there. It is a typo. Since we forgot to write x, instead of ks, we got a runtime error. We have to run the site, click of the link, read the error message, ask ourself, what did we do, use the debugger, run the site, check again, go through bunch of step, just because we made a typo.
This is the reason, why expert developers, such as david Ebbo, created a helper (T4 templates), that goes through the code and created a strongly typed strings for controllers, views, even a content.
Fixed upper example:
<%:Html.ActionLink(»My link«, MVC.Home.Index)%>
Its so easy. No more runtime checking, no repro bugs, even intellisense helps us understand, which name and even parameters we will use.
You can use the following strongly typed helpers in the whole app, not only the views ( also in controllers, etc.)
It is quite simple to use it:
1. Download the zip file on the codeplex site
2. Required watching the David Ebbo intro with examples
3. Unzip the file
4. Add the 2 files in the root of your MVC app (T4MVC.tt and T4MVC.setting.t4)
5. Build it
6. Use it J
You can change the setting for t4 in the settings file to bend it to you will J
Happy no-typo coding.