Question

I have a program that uses tables of hard-coded values. E.g.

    public static readonly IDictionary<SpecBuild, BuildInfo> Builds = new Dictionary<SpecBuild, BuildInfo> {
            { SpecBuild.WarriorArms,
                    new BuildInfo {
                            Class = Class.Warrior,
                            Name = "Arms",
                            Role = Role.Melee
                    }
                    },
            { SpecBuild.WarriorFury,
                    new BuildInfo {
                            Class = Class.Warrior,
                            Name = "Fury",
                            Role = Role.Melee
                    }
                    },
            { SpecBuild.WarriorProtection,
                    new BuildInfo {
                            Class = Class.Warrior,
                            Name = "Protection",
                            Role = Role.Tank
                    }
                    }, ...

This data seldom changes, so I am fine with hard-coding it. The problem is that it is hard to read this data, and formatting doesn't work because Resharper reformats it differently.

Ideally I would want a special format file with a custom VS2010 viewer that would present this info as a table and either generate a C# code for those table as part of compilation process, or maybe even compile it directly into MSIL (CLR).

I am looking for a turnkey solution, the problem is not severe enough to warrant any extra development.

Was it helpful?

Solution 4

Resharper lets me exclude files by extension, so I name my files *.data.cs, format data as I want it, and resharper won't reformat it.

OTHER TIPS

I would (and have, many times) push this type of data into a file (perhaps via a resx). Then load the data via your choice of deserializer. If it needs to be easily edited, maybe JSON via JavaScriptSerializer or JSON.NET. If it is told of data and performance is key, maybe something more hardcore (maybe binary). XML is of course a viable choice too.

Other options might included an embedded database, if the scenario warrants it.

Ideally I would want a special format file with a custom VS2010 viewer

This is completely possible:

  • Create a DSL
  • Write a VS2010 extension that supports the DSL
  • Write a tool to perform the build (an MSBuild Task)
  • Write an installer to update VS installs with the tooling.

Of course that is going to be a lot of learning to understand how to do it.

If you have lots of applications with lots of developers using the same type of information it would make sense. Otherwise there are other options:

  1. Configure Resharper not to reformat it (I assume this is possible).
  2. Use a data format (text, XML, JSON ...) that VS can already edit. Add as a resource at build time and create a tool to create the object graph at application startup (or on demand).

Clearly #1 (of possible) is simplest, but fragile. #2 is well established -- this is how resources are handled (and XAML can be).

You could create a design-time T4 text template with

void SpecBuild(string spec, string @class, string name, string role)
{
    Write(string.Format("{{ SpecBuild.{0}, " +
                        "new BuildInfo {{ " +
                        "Class = Class.{1}, " +
                        "Name = @\"{2}\", " +
                        "Role = Role.{3} " +
                        "}} " +
                        "}},",
                        spec,
                        @class,
                        name.Replace("\"", "\"\""),
                        role));
}

and

//        SpecBuild            Class      Name          Role
//        -------------------- ---------- ------------- -------
SpecBuild("WarriorArms",       "Warrior", "Arms",       "Melee");
SpecBuild("WarriorFury",       "Warrior", "Fury",       "Melee");
SpecBuild("WarriorProtection", "Warrior", "Protection", "Melee");
//        -------------------- ---------- ------------- -------
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top