1

So I have a large collection of classes, and when a class is updated I need to run an update method on any existing saved data. Currently I use something like:

public const decimal LatestVersion = 0.0121201313m;
private decimal LastVersion;

//...

if (Abilities.LastVersion != Abilities.LatestVersion)
{
    //...

And I manually update the LatestVersion number every time I update the class... I also have to repeat this method in every class that may need to be updated. It works well, but I have to remember to update the version number.

What I am thinking would be nice is to have the LatestVersion automatically update to the current DateTime (maybe via a debug command) if the class was changed or something like that... But I don't think that is possible.

Does anyone have any other ideas on how I can setup a auto-implementing versioning system on a class level?

2
  • Do you put each of your classes in separate files? Commented Jan 22, 2013 at 17:12
  • They are each in a seperate .cs file, but not a separate project. Commented Jan 22, 2013 at 17:17

3 Answers 3

2

You can use some sort of hash of non-whitespace content in the class file for a version. It will require 2 files per class - one source, one generated at build time but will guarantee that any change changes version. May need to add date to the hash just in case.

 // source file MyClass.cs
 partial class MyClass {}

 // MyClass.Hash.cs
 // generate during build via custom tool 
 partial class MyClass 
 {
    // hash of MyClass.cs + date
    public static string LatestVersion = "W#$@WW-2012-01-22"; 
 }

Side notes:

  • automatic system will likely be worse than manual changing the version with tests validating that the change is compatible.
  • you may consider saving data in format that more forgiving to the changes. I.e. with JSON/XML you can add fields without breaking previous versions.
Sign up to request clarification or add additional context in comments.

3 Comments

I like the hash idea, very inventive -- but I currently have about 45 classes in my project and I don't want to double the number of files. But I might be able to incorporate a class that holds the current hash values for all the files -- but even then it seems like more work than it would be worth. Thanks though!
@AnthonyNichols, I'd like to reiterate my notes: this is answer to your question, but please make sure that you actually need/want some automatic versioning before implementing something like this.
Understood -- Part of the reason for doing this project is learning, so at the moment I want to get a working solution that I am happy with... Then depending on several factors I may or may not leave that part of it in.
2

Some version control systems can use keyword expansion mechanism. You could use that to put the change/serial/revision number (or whatever it's called) in the file during checkin or checkout.

Example for TFS: Keyword Substitution / Expansion Check-In Policy

1 Comment

That's interesting and more inline with what I am looking for except that I want to do it without relying on check-ins. I was hoping there would be some #DEBUG command that could do it.
1

Off the top of my head, either create a transform-on-build T4 that builds a class/dictionary of .cs/type names to hash/timestamp or alternatively tokenize that property and hook into MSBuild's BeforeBuild event to replace it before compilation with similar hash/lastmodified.

Edit: Ok, so here are a couple of very crude examples.

T4 -- Create a Text Template in your project (.tt) and paste the following

<#@ template hostspecific="true" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
namespace ClassLibrary1
{
    public class Version
    {
        <#  var project = new FileInfo(Host.ResolvePath("ClassLibrary1.csproj"));
            WriteLine("// " + project.FullName);

            var files = project.Directory.GetFileSystemInfos("*.cs", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                var name = Path.GetFileNameWithoutExtension(file.Name);
                if (name == "Version")
                    continue; #>
               public const long <#= name #> = <#= file.LastWriteTime.Ticks #>;
        <#  } #>
    }
}

You'll now have a class that you can use in comparisons via Version.ClassName constants.

MSBuild -- Edit your .csproj and add the following at the end.

<UsingTask TaskName="Version" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  <ParameterGroup>
    <Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
  </ParameterGroup>
  <Task>
    <Using Namespace="System.IO" />
    <Using Namespace="System.Text.RegularExpressions" />
    <Code Type="Fragment" Language="cs"><![CDATA[
          var regex = new Regex(@"Version = (\d+);//(-?\d+)");
          foreach (var file in Files)
          {
              var source = File.ReadAllText(file.ItemSpec);
              var match = regex.Match(source);
              if (!match.Success) continue;
              var hash = regex.Replace(source, string.Empty).GetHashCode();
              if (int.Parse(match.Groups[2].Value) == hash) continue;
              source = regex.Replace(source, string.Format("Version = {0};//{1}", int.Parse(match.Groups[1].Value) + 1, hash));
              File.WriteAllText(file.ItemSpec, source);
          }
      ]]></Code>
  </Task>
</UsingTask>
<Target Name="BeforeBuild">
  <Version Files="@(Compile)" />
</Target>

Then add public const int Version = 0;//0 to your class. Before compilation MSBuild will get hash of the file and update the counter if previous one doesn't match.

There is a crapton that can be done better in these examples, but hopefully they'll illustrate the directions you can take. May be there is a better way, those are first two ideas that poped into my head after hashing IL with Mono.Cecil, I found the question quite interesting. If you do go either route I recommend reading into T4 and its Host and how to force regeneration on build, or MSBuild and extending events, custom and inline tasks, maybe even go into EnvDTE, CodeDOM, Microsoft.Build.Evaluation namespaces, etc rather than just searching for .cs files and regexing.

3 Comments

Any chance you can give me some more information on what all that means. It sounds like those are going in the right direction -- but it also sounds like it's way over my head.
Awesome, thanks - the MSBuild is right in line with what I was wanting to do... It will take some work to figure it all out, but that's the idea. The T4 stuff might come in handy some day so thanks for that too!
Good to know that String.GetHashCode() result is not portable, so the change calculation above may result in unnecessary version increment if executed on different computers. One might use standard hashing methods to avoid that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.