Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a downloadable c# class from within an ASP.NET MVC website?

I'm sure this is a very weird requirement, but here goes.

We have some customer configuration code that is represented by C# classes that we compile into a class library. Creating these configuration classes is currently monkey work that I'd like to assign to someone other than a developer. We're therefore building a "Configurator" website that a business analyst can use to generate valid C# classes by filling in a form with the relevant options selected.

What is the best way to generate a C# class from a template in an MVC website? Note that I'm not talking here about dynamically generating and executing a class at runtime: we're essentially generating a text file that just happens to contain C# code that will be included in a totally different project.

This is proving to be a very difficult topic to google properly! Options that have come up so far...

  • just use a tokenized text file and replace values using String.Format
  • somehow use Razor as a templating engine (but not sure how, and it's not html we're generating)
  • somehow use T4 templates (at least I know it's used to generate C#, but not sure how to execute at runtime)

Any ideas on how best to approach this?

As requested, here's an example of the output class/file. Values in here override values set in a base class with customer-specific labels, validation, etc. This is how we customise our product for customers.

public class AreaRiskTemplate: AreaRiskTemplateBase
{
    public override string EntityName { get { return "risk"; } }
    public override string EntityNamePlural { get { return "risks"; } }
    public override string EntityNameArticle { get { return "a"; } }
    public override string CGovItemRefPrefix { get { return "R"; } }
    public override void SetFieldDefinitions()
    {

        FieldDefinitions.Level1.IsImplemented = true;
        FieldDefinitions.Level1.Label = "Category";
        FieldDefinitions.Level1.IsVisibleInGrid = true;

        FieldDefinitions.Level2.IsImplemented = true;
        FieldDefinitions.Level2.Label = "Team";
        FieldDefinitions.Level2.IsVisibleInGrid = true;

        FieldDefinitions.Description.IsImplemented = true;
        FieldDefinitions.Description.Label = "Description";
        FieldDefinitions.Description.IsVisibleInGrid = true;

    }
    public override ModelStateDictionary Validate()
    {
        var msd = new ModelStateDictionary();
        // add template-specific validation here
        msd.Merge(base.Validate());
        return msd;
    }
    public override List<string> Level1Options
    {
        get
        {
            return new List<string>
                {
                    "Organisational",
                    "Financial",
                    "Operational",
                    "External",
            "IT"
                };
        }
    }
}

Update: currently investigating T4 templates at http://msdn.microsoft.com/en-us/library/ee844259(v=vs.100).aspx (thanks to comments below). Looks promising.

like image 518
centralscru Avatar asked Sep 20 '12 13:09

centralscru


People also ask

Is C free to download?

C-Free is a free IDE software for PC developed by Program Arts Software.


1 Answers

I suggest avoiding the website, and instead simply going with CodeSmith. You can design CodeSmith scripts so that it will ask for information and then generate the required code. Seems like a lot less work, although you do have to buy CodeSmith.

like image 151
Erik Funkenbusch Avatar answered Oct 05 '22 07:10

Erik Funkenbusch