WTDawson.PersistentID is a library built in C# designed to make it easier to build IDs that be reproduced with the correct data. The library accepts a list of objects (Has to be a string, integer or DateTime object) and a “magic” number. The “magic” number is a key which is used when generating the ID.
Below is the result of a test run with the arguments “-f” and “-c”, the red means that it is different and the green means that they are the same. This is compared to a static set of strings in the test application as well as the static data.
The “-f” argument adds a few extra words onto the list of objects to be used which causes it to become different.

Below is the result of a test run with the argument “-c”, the green means that the are the same. Again, this is done with a static set of strings in the test application as well as the static data in the ID builder.

Using the library
The library is pretty simple to get set up. Here are some simple steps:
- Create a new C# application (E.g. Console Application)
- Find and install “WTDawson.PersistentID”
- Import the library and use the examples below
Example 1: A user ID generator based on a name and age
using WTDawson.PersistentID;
namespace IDGenerator
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Name (First and last): "); // E.g. John Doe
string[] name = Console.ReadLine().Split(' ');
Console.Write("Age: "); // E.g. 71
int age = int.Parse(Console.ReadLine());
IDBuilder builder = new IDBuilder();
builder.AddItems(name[0], name[1], age);
builder.SetMagic(age);
builder.SetLength(8);
Console.WriteLine(builder); // E.g. 7enehDeJ
}
}
}
In this example, it takes a name and age and adds them to the builder as items and uses the age as the “magic” (Otherwise known as a key). If you try this out yourself it should output “7enehDeJ”.
Example 2: Creating a Persistent User ID from User Email
using WTDawson.PersistentID;
namespace IDGenerator
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Email: "); // E.g. [email protected]
string email = Console.ReadLine();
IDBuilder builder = new IDBuilder();
builder.AddItems(PublicUtils.RemoveSpecialCharacters(email));
builder.SetMagic(email.Length);
builder.SetLength(8);
Console.WriteLine(builder); // E.g. tmooamsa
}
}
}
In this example, it takes an email address and removes any special characters with the PublicUitls class built into WTDawson.PersistentID and adds it to the builder as an item. It then uses the length of the email address as the “magic” (Otherwise known as a key). If you try this out yourself it should output “tmooamsa”.
Recommendations
For IDs, I recommend using the PublicUtils.RemoveSpecialCharacters on anything that might have special characters (Like an email address for instance) as it may cause issues.
Uses
If you’ve seen my other blog posts about WTDawson.EventPipes, you could use this library for generating pipe names based on the app name, machine name, and other data that could be the same on both sides (But slightly different for pipe A and pipe B – just make sure you get them the right way around).
You can also replace one of my older libraries called uniqueit with this, this is pretty much a better version of that library.
Limitations
- You can only use a string, integer or DateTime object as the items, anything else will throw a InvalidItemException.
- It doesn’t filter anything being used as the items like it does when generating a list of characters to use, so you may wish to manually filter them using the PublicUtils provided.
Discover more from WTDawson
Subscribe to get the latest posts sent to your email.