There are so many scenarios that I faced in more than 18 years doing integration that required me to generate a unique id that I don’t know how to start explaining all of them. Unique identifiers are handy when an automation process needs to generate a unique reference “number” to identify an object or entity like a Customer account, a document id, and so on. Most of the time, we use GUIDs, which stands for a globally unique identifier, and it is usually a 128-bit text string that represents an identification (ID) that is unlikely ever to repeat or create a collision to address these scenarios unless requirements don’t allow us to use a GUID.
This is a list of 4 Functions that will allow you to generate unique identifiers:
- GUID Generator: This function has the same capabilities available on the Online GUID / UUID Generator: https://guidgenerator.com/online-guid-generator.aspx.
- Tickets Short GUID Generator: Calculate the uniqueId using the provided year.
- Youtube-like GUID Generator: Function to generate a short GUID like in YouTube (N7Et6c9nL9w).
- Tiny Id Generator: Another way that I used in the past to generate a tiny identifier.
GUID Generator Function
As mentioned above, this function has the same capabilities as the Online GUID / UUID Generator: https://guidgenerator.com/online-guid-generator.aspx. It accepts 3 optional query parameters:
- useHyphen: accepts the values true or false to decide if you want to generate a guide separated with hyphens.
- The default value is true.
- useUppercase: accepts the values true or false to decide if you want to convert the GUID to upper case.
- The default value is false.
- useBraces: accepts the values true or false to decide if you want to generate a guide between braces.
- The default value is false.
Here is a small sample of the code behind this function:
if (useHyphen)
{
Guid hyphenGuid = Guid.NewGuid();
string finalGuid = "";
if (useBraces)
finalGuid = hyphenGuid.ToString("B");
else finalGuid = hyphenGuid.ToString();
if (useUppercase)
return new OkObjectResult(finalGuid.ToUpper());
return new OkObjectResult(finalGuid);
}
Tickets Short GUID Generator Function
This function will generate a unique identifier using a year. This function accepts 1 optional query parameter:
- year: year to take as a reference for calculating the unique identifier.
- The default value is 1978.
Here is a small sample of the code behind this function:
var ticks = new DateTime(year, 1, 1).Ticks;
var ans = DateTime.Now.Ticks - ticks;
var uniqueId = ans.ToString("x");
Youtube-like GUID Generator Function
This function allows you to generate a short GUID like in YouTube (N7Et6c9nL9w).
This was made just for fun, but here is a small sample of the code behind this function:
tring base64Guid = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
Tiny Id Generator Function
This function is another way that I used in the past, in BizTalk Server projects, to generate a tiny identifier. But looking now that I’m writing this blog post, it looks like a Youtube-like GUID.
Here is a small sample of the code behind this function:
Guid guid = Guid.NewGuid();
string modifiedBase64 = Convert.ToBase64String(guid.ToByteArray())
.Replace('+', '-').Replace('/', '_') // avoid invalid URL characters
.Substring(0, 22);
Where can I download it
You can download the complete Azure Functions source code here:
Once again, thank my team member Luis Rigueira for testing and helping me develop some of these function with me!
Hope you find this helpful! So, if you liked the content or found it helpful and want to help me write more content, you can buy (or help buy) my son a Star Wars Lego!