This project is read-only.

Pattern for controller names

Jan 9, 2015 at 5:16 PM
T4MVC can detect FooController and generates a class appropriately. But it cannot detect, for example, FooControllerA because of the trailing A.

This is a Hungarian wart we use to define abstract classes... we don't use the traditional AFooController as that messes up the order in alphabetical lists, so we use FooControllerA.

How can I make T4MVC detect this pattern?
Jan 9, 2015 at 8:02 PM
Search for IsController in T4MVC.tt. You can change the logic in there.

Potentially, we could make this more configurable by adding a call out to a method in T4MVC.tt.hooks.t4.
Jan 9, 2015 at 8:17 PM
Edited Jan 9, 2015 at 8:21 PM
Thanks for the tip!

As far as making it configurable, it would make sense to use an OPTIONAL regex in the config file. If defined, it would be used, else the default logic would apply.

This is not just for my scenario, as I've seen others name their controllers differently to the norm, for eg, without the word "Controller" in it.
Jan 9, 2015 at 8:19 PM
Yes, that would work. Would you be interested in putting this change together and sending a pull request? That's how most T4MVC changes happen :)
Jan 10, 2015 at 12:35 AM
Actually I already tried, but couldn't get it (the regex idea) to work. :( There are a number of places that need to be modified, and I don't understand how the code works...

I ended up changing only something called ControllerSuffix, as a short-term solution. I'll try again, as this regex option would be awesome...
Jan 10, 2015 at 1:14 AM
Edited Jan 10, 2015 at 1:25 AM
OK I got somewhere... Sorry but we use svn here so I don't know how to do a "git pull request" :(

Add to the T4MVC.tt.settings.xml:
....
<ControllersFolder>Controllers</ControllersFolder>
<!--A regex which identifies a controller's filename, or the default suffix of 'Controller$'-->
<ControllerNamePattern>(Controller|ControllerA)$</ControllerNamePattern>
Add to T4MVC.tt:
static bool IsController(CodeClass2 type)
{
  // Ignore any class which name doesn't end with "Controller"
  //if (!type.FullName.EndsWith(ControllerSuffix)) return false; // <-- comment out
  var regex = new System.Text.RegularExpressions.Regex(settings.ControllerNamePattern); // <-- new
  if (!regex.IsMatch(type.FullName)) return false; // <-- new

// ...

public MvcSettings()
{
    // ...
    this.ControllersFolder = "Controllers";
    this.ControllerNamePattern = "Controller$"; // <-- new

// ...
public string ControllersFolder { get; set; }
[System.ComponentModel.Description("A regex which identifies a controller's filename, or the default suffix of 'Controller$'")] // <-- new
public string ControllerNamePattern { get; set; } // <-- new
So for a class FooControllerA I get a FooControllerA.generated.cs file, cool!.

BUT, I don't know what ControllerSuffix does exactly, and so generated code is weird, eg, I get public readonly string Name = "fooc";

Any ideas?
Jan 10, 2015 at 12:04 PM
OK figured it out...
  public string Name
  {
      get
      {
          // Trim the Controller suffix
          //return ClassName.Substring(0, ClassName.Length - ControllerSuffix.Length); <-- comment out
          var regex = new System.Text.RegularExpressions.Regex(settings.ControllerNamePattern);
          var match = regex.Match(ClassName).Value;
          return ClassName.Substring(0, ClassName.Length - match.Length);
      }
  }
Jan 10, 2015 at 12:06 PM
I just don't know what to do in void ProcessAllViews(ProjectItem viewsProjectItem, AreaInfo area) which also refers to ControllerSuffix. Other than that it all works.
Jan 10, 2015 at 12:33 PM
OK I sent you a pull request...

Please have a look when you get a chance?
Jan 12, 2015 at 6:25 PM
Ok, change is in! For tracking the PR is here.