Boost Your .NET Projects with Spargine: Simplify Unique Key Generation with KeyGenerator

Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.

Get Spargine

You can access the source code and NuGet packages here:

When working with applications, there’s often a need to generate unique keys—whether for object identifiers, database records, or cloud solutions like CosmosDB. Instead of writing boilerplate code or relying on less efficient approaches, Spargine provides a simple, fast, and reliable solution: the KeyGenerator.

What Is KeyGenerator?

The KeyGenerator in Spargine is a lightweight utility that creates unique keys based on a GUID. These keys are guaranteed to be unique, making them ideal for object IDs, database operations, and distributed applications where collisions must be avoided.

An example of a generated key looks like this:
f7f0af78003d4ab194b5a4024d02112a

Methods

KeyGenerator offers flexible methods for generating keys, depending on your use case:

  • GenerateCustomKey(char separator = ControlChars.Dash, bool addTimeStamp, params string[] items)
    Generates a custom key by joining the provided items with a separator and optionally appending a unique key. Example with timestamp: User_Admin_609B7E7F9B010000_2008d168407e41d78e696a3b94efd42f
  • GenerateKey()
    Creates a random key from a GUID without dashes.
  • GenerateKey(string prefix)
    Creates a random key from a GUID with a specified prefix (useful for categorization or scoping).
  • GenerateSortableKey()
    Creates a sortable random key from a GUID using version 7 GUIDs.
  • GenerateSortableKey(string prefix)
    Creates a sortable random key from a GUID with a specified prefix using version 7 GUIDs.

Summary

The next time your .NET project needs a unique key, the KeyGenerator in Spargine can help you eliminate complexity and streamline your workflow. And if you need more advanced identifiers, check out the Ulid type—also included in Spargine—for generating Universally Unique Lexicographically Sortable Identifiers (ULIDs).

Get Involved!

The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:

  • Submitting pull requests
  • Reporting issues
  • Suggesting new features

Your input is invaluable in making Spargine an even more powerful tool for the .NET community.

If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!

Thank you, and happy coding!

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.