Skip to content

Auto Generation of Constants #66

Closed
@mcquiggd

Description

@mcquiggd

@RehanSaeed

If you are interested, I have created T4 Templates to automatically generate the Constants for:

  • Controller Names
  • 'Action' Names, which detect if an HTTPGet or HTTPost, or a Route definition attribute was applied and creates constants accordingly. The names reflect the HTTP Verb, Get, Post, and I settled on Route for 'unspecified'.
  • View Names
  • I have others for creating constants for content files - e.g. CSS, Images, JavaScripts etc.

I am currently using VS 2015 Update 1 with the ASP.Net 5 MVC6 version. I see no reason why they would not work with other combinations. They automatically update on Build by default.

If you are interested, let me know and I will add a sample on GitHub to see what you think. You might notice I have used a slightly different naming convention from the base template, as there are a few areas where things are not quite fitting.

I would be interested in contributing to the project.

Examples:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool 28/01/2016 00:06:42 UTC.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Pro.Website.Constants
{
    public static class ActionNames
    {
        public static class ErrorController
        {
            public static class Get
            {
                public const string Error = "Error";
            }
        }
        public static class HomeController
        {
            public static class Get
            {
                public const string About = "About";
                public const string Contact = "Contact";
                public const string Index = "Index";
            }
            public static class Route
            {
                public const string BrowserConfigXml = "BrowserConfigXml";
                public const string Feed = "Feed";
                public const string ManifestJson = "ManifestJson";
                public const string OpenSearchXml = "OpenSearchXml";
                public const string RobotsText = "RobotsText";
                public const string Search = "Search";
                public const string SitemapXml = "SitemapXml";
            }
        }
    }
}

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool 30/01/2016 00:09:24 UTC.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Pro.Website.Constants
{
    public static class ViewNames
    {
        public static class ErrorController
        {
            public const string Error= "Error";
        }
        public static class HomeController
        {
            public const string About= "About";
            public const string Contact= "Contact";
            public const string Index= "Index";
        }
        public static class SharedController
        {
            public const string _Footer00= "_Footer00";
            public const string _Header00= "_Header00";
            public const string _Header01= "_Header01";
            public const string _Header02= "_Header02";
            public const string _Header03= "_Header03";
            public const string _Header04= "_Header04";
            public const string _Layout= "_Layout";
        }
    }
}
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool 28/01/2016 00:08:08 UTC.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Pro.Website.Constants
{
    public static class ControllerNames
    {
        public const string Error = "Error";
        public const string Home = "Home";
    }
}

Which can then be called as follows:

<a asp-controller="@ControllerNames.Home" asp-action="@ActionNames.HomeController.Get.Contact">Contact Us</a>`
[Route("browserconfig.xml", Name = ActionNames.HomeController.Route.BrowserConfigXml)]`
<link href="@Url.RouteUrl(ActionNames.HomeController.Route.BrowserConfigXml)"
[HttpGet("contact", Name = ActionNames.HomeController.Get.Contact)]
public IActionResult Contact()
{
    return View(ViewNames.HomeController.Contact);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementIssues describing an enhancement or pull requests adding an enhancement.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions