SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Matt Ward

Using T4MVC with SharpDevelop

SharpDevelop 4.3 now has support for T4MVC.

T4MVC is a set of T4 templates, created by David Ebbo, that will generate strongly typed helpers for an ASP.NET MVC application. It will allow you to remove strings from your MVC application making your application easier to maintain.

So let us take a look at how to use T4MVC with SharpDevelop. First you should open or create a new ASP.NET MVC application in SharpDevelop.

Installing T4MVC

T4MVC is available as a NuGet package. The NuGet package you should download is T4MVC.SharpDevelop. This contains a modified version of the original T4MVC template that can be used with SharpDevelop. Further details on all the modifications made to the T4MVC template can be found at the end of this post. Install the T4MVC.SharpDevelop NuGet package either from the NuGet package management console or by using the Manage Packages dialog.

Note that T4MVC 2.13.0 requires SharpDevelop 4.3.0.9132 or above.

After installation two new T4 template files will be added to your project.

  1. T4MVC.tt - main template that generates the strongly typed helpers.
  2. T4MVC.tt.settings.t4 - holds configuration settings used by main T4MVC.tt template.

Generating T4MVC's Strongly Typed Helpers

To run the T4MVC template manually, select it in the Projects window, right click and select Execute Custom Tool. This will generate a set of files as dependencies of the T4MVC.tt file.

T4MVC Generated Files in Projects window

It will also make some modifications to your controller classes. The T4MVC template will change your controller classes so they are partial. It will also change your controller methods so they are virtual. What has been changed will be recorded as warnings in the Errors window.

T4MVC modified code warnings in Errors window

When you make modifications to your application you do not want to have to keep running the T4MVC template manually each time to regenerate the strongly typed helpers. Instead you can configure your project to re-generate the strongly typed helpers on each build.

Generating T4MVC's Strongly Typed Helpers on each Build

To do this you should open the options for the project. From the Projects menu and select Project Options. Open the Custom Tool tab. In this tab you can choose to run custom tools on each build. In the screenshot below the project is configured to run T4MVC on each build.

Project options configured to run T4MVC on every build

If you want to run other custom tools you can add filenames for items in your project either as a comma separated list or with each file on a separate line. Currently there is no support for wildcards when specifying filenames.

Now that we have generated the strongly typed helpers let us take a look at how to use a selection of them.

Using T4MVC's Strongly Typed Helpers

View Names

In your _ViewStart.cshtml you may have a reference to a Razor layout page.

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

You can replace this string with a strongly typed helper.

@{
    Layout = MVC.Shared.Views._Layout;
}

Action Links

In your views you may be passing action names and controller names as strings HtmlHelper.ActionLink(string linkText, string actionName, string controllerName).

    <li>@Html.ActionLink("Home", "Index", "Home")</li>
    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>

Instead you can use code that looks like you are calling the controller method.

    <li>@Html.ActionLink("Home", MVC.Home.Index())</li>
    <li>@Html.ActionLink("Contact", MVC.Home.Contact())</li>

CSS Links

You may be using strings for links, such as for CSS files, in your views.

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css">

These can be changed to use the Links.Content helper class.

<link href="@Links.Content.Site_css" rel="stylesheet" type="text/css">

That is a very quick introduction to T4MVC and covers only a few of the helpers it provides. Further information on T4MVC can be found on the T4MVC Codeplex site.

Changes Made to Original T4MVC Template

  1. Visual Studio assembly references have been removed and replaced with a SharpDevelop assembly reference.
  2. Visual Studio namespace imports have been removed and replaced with namespace imports for SharpDevelop.
  3. Removed use of BeginInvoke/EndInvoke which was causing SharpDevelop to hang.

The SharpDevelop specific T4MVC template is maintained in a repository on github.

[Update 2012-10-11] Added information on T4MVC 2.13.0 requiring a newer version of SharpDevelop 4.3. Updated "Changes made to original T4MVC template" section after SharpDevelop EnvDTE API was modified to follow Visual Studio's API when using parameterised properties such as ProjectItem.FileNames() and CodeType.IsDerivedFrom()

Published Sep 15 2012, 01:40 PM by MattWard
Filed under:

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.