T4 Template

Apr 4, 2009 at 8:09 AM
I'm working on a t4 template that will generate controller classes and action methods based off the sitemap nodes. I'll post it up when I'm done.
Apr 6, 2009 at 3:28 AM
After hours of learning how to use T4 templates and debugging I finished the template.

To use it you need T4toolbox and Visual Studio 2008:

You can download T4toolbox from http://www.codeplex.com/t4toolbox

You need to replace two paths:
template.Output.Project
and siteMapFile

First Template File

<#@ template language="C#" hostspecific="true"#>
<#@ output extension=".cs" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ include file="CreateController.tt" #>
<#@ import namespace="System.Xml" #>

<#
List<MvcController> listControllers=controllers();
foreach(MvcController controller in listControllers)
{
    ControllerTemplate template = new ControllerTemplate(controller.Name, controller.Actions);
    template.Output.File = @"GeneratedControllers\"+controller.Name+"Controller.cs";
    template.Output.Project = @"C:\Main\MvcSiteMap\MvcSiteMap.Web\MvcSiteMap.Web.csproj";
    template.Render();
}
#>

<#+
     private List<MvcController> controllers()
        {
            List<MvcController> controllers = new List<MvcController>();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;
            settings.IgnoreComments = true;
            string siteMapFile = @"C:Main\MvcSiteMap\MvcSiteMap.Web\Web.sitemap";
            using (XmlReader node = XmlReader.Create(siteMapFile, settings))
            {
                while (node.Read())
                {
                    string controllerName = node["controller"];
                    if (controllerName != null)
                    {
                        string action = node["action"];
                        List<string> listParameters = new List<string>();
                        string param = node["dynamicParameters"];
                        if (param != null)
                        {
                            string[] paramaters = param.Split(',');
                            foreach (string s in paramaters)
                            {
                                if (!s.Contains("*")) listParameters.Add(s);
                            }
                        }

                        if (controllers.Exists(delegate(MvcController c) { return c.Name.Equals(controllerName); }))
                        {
                            MvcController controller =
                                controllers.Find(delegate(MvcController c) { return c.Name.Equals(controllerName); });
                            if (action != null)
                                if (!controller.Actions.Exists(delegate(MvcAction a) { return a.Name.Equals(action); }))
                                {
                                    controller.Actions.Add(new MvcAction(action, listParameters));
                                }
                        }
                        else
                        {
                            MvcController controller = new MvcController(controllerName);
                            if (action != null)
                                controller.Actions.Add(new MvcAction(action, listParameters));
                            controllers.Add(controller);
                        }
                    }
                }
            }
            return controllers;
        }
#>

<#+
   public class MvcController
    {
        public string Name;
        public List<MvcAction> Actions;

        public MvcController(string controllerName)
        {
            Name = controllerName;
            Actions = new List<MvcAction>();
        }
        public MvcController(string controllerName, List<MvcAction> actions)
        {
            Name = controllerName;
            Actions = actions;
        }
    }
#>

Second Template File:

<#@ template language="C#" hostspecific="True" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ include file="T4Toolbox.tt" #>

<#+
public class ControllerTemplate : Template
    {
        string controllerName;
        List<MvcAction> listActions;
        public ControllerTemplate(string ControllerName, List<MvcAction> ListActions)
        {
            controllerName=ControllerName;
            listActions=ListActions;
        }
        protected override void RenderCore()
        {
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcSiteMap.Web.Controllers
{    
    public class <#= controllerName #>Controller : Controller
    {
<#+     foreach(MvcAction action in listActions)
{#>
        public ActionResult <#=action.Name#>(<#+
    bool first=true;
    foreach(string paramater in action.Paramaters)
    {
        if(first)
        {
            #>string <#=paramater#><#+
        first=false;
        }
        else
        {
            #>, string <#=paramater#><#+}#><#+
    }#>)
        {
            // TODO: Add logic here
              throw new NotImplementedException();
        }
<#+
}#>
    }
}
<#+
        }
    }#>
<#+
    
   public class MvcAction
    {
        public string Name;
        public List<string> Paramaters;

        public MvcAction(string actionName)
        {
            Name = actionName;

            Paramaters = new List<string>();
        }
        public MvcAction(string actionName, List<string> paramaters)
        {
            Name = actionName;
            Paramaters = paramaters;
        }
    }
#>

Apr 6, 2009 at 3:32 AM
I uploaded them to
http://rapidshare.com/files/217945592/CreateControllers.zip.html