Implementing a Version check between an Abstract class and it's implementation

Posted by Michael Stum on Stack Overflow See other posts from Stack Overflow or by Michael Stum
Published on 2010-03-28T09:30:08Z Indexed on 2010/03/28 9:33 UTC
Read the original article Hit count: 179

Filed under:
|
|

I have this abstract class and concrete implementation (they are in different assemblies):

public abstract class MyAbstractClass
{
    private static readonly int MyAbstractClassVersion = 1;
    public abstract int ImplementedVersion  { get; }

    protected MyAbstractClass()
    {
        CheckVersion();
    }

    private void CheckVersion()
    {
        var message =
            string.Format(
            "MyAbstractClass implements Version {0}, concrete is Version {1}",
            RepositoryVersion, ImplementedVersion);
        if (!MyAbstractClassVersion.Equals(ImplementedVersion))
          throw new InvalidOperationException(message);
    }
}


public class ConcreteClass : MyAbstractClass
{
    public ConcreteClass() : base() {
        // ConcreteClass is guaranteed to have
        // a constructor that calls the base constructor
        // I just simplified the example
}

    public override int ImplementedVersion
    {
        get { return 2; }
    }
}

As you see, I call CheckVersion() from the abstract constructor, to get rid of the "virtual member call in base constructor" message, but I am not sure if that's really the way to do it. Sure, it works, but that doesn't mean it will always work, will it?

Also, I wonder if I can get the name of the Concrete Type from the CheckVersion() function?

I know that adding new abstract members will force an error anyway (System.TypeLoadException) and I'm not sure if I want this type of strict Versioning, but I'm just curious how it would be done properly given only the abstract class and an implementation (I know I could do it by using interfaces and/or a Factory pattern).

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#