I'm building an app in C# that needs a fixed-length id, which is a string representation of a hex number. E.g. "0fa5"
is an example of such an id of length 4. The length would not need to change within the program at runtime, but may be changed in the future or for example in tests.
My question is about how to approach designing such a class. I've come up with two methods so far:
The first would be making a single class, IdType
that has a static method SetExpectedLength(int)
that should only be called once, otherwise it gives an exception:
class IdType
{
public static int ExpectedLength {get; private set;} = 0;
public string Id {get; private set;} = "";
public IdType(string id)
{
if (IdType.ExpectedLength== 0)
throw new Exception("SetExpectedLength needs to be called first");
if (id.Length != IdType.ExpectedLength)
throw new Exception("Length constraints not satisfied!");
this.Id = id;
}
public static void SetExpectedLength(int length)
{
if (IdType.Length == 0)
IdType.ExpectedLength = length;
else throw new Exception("SetExpectedLength can only be called once");
}
// other operations here...
}
The other approach would be making a base abstract class and deriving IdType
s of different lengths from it:
abstract class IdType
{
public string Id {get; protected set;}
}
// IdType of length 4
class IdType4 : IdType
{
public readonly int Length = 4;
public IdType4(string id)
{
if (id.Length != this.Length)
throw new Exception("Length constraints not satisfied!");
this.Id = id;
}
}
The second approach seems more clean, however if I need a new length for the ids I would have to make a new class AND replace the instance of the old class everywhere it is used.
What is the best practice in this case? Is there another design pattern that I can use?