GuidRecycler 1.0.5

dotnet add package GuidRecycler --version 1.0.5
NuGet\Install-Package GuidRecycler -Version 1.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GuidRecycler" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GuidRecycler --version 1.0.5
#r "nuget: GuidRecycler, 1.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GuidRecycler as a Cake Addin
#addin nuget:?package=GuidRecycler&version=1.0.5

// Install GuidRecycler as a Cake Tool
#tool nuget:?package=GuidRecycler&version=1.0.5

GuidRecycler

GuidRecycler

CI NuGet NuGet Guids Recycled Code coverage Uptime Stars

Save the planet, recycle your guids!

What is it?

GuidRecycler is a library that allows you to recycle guids. It is a simple concept.

Installation

GuidRecycler is available as a NuGet package.

Install-Package GuidRecycler

Usage:

Guid recyclers implement the common interface IGuidRecycler which has the following methods:

void Recycle(Guid guid);

To recycle a guid to the recycle bin.

Guid GetGuid();

To retrieve a recycled (or new) guid.

Strategies

There are curently two strategies implemented:

  • ConcurrentGuidRecycler
    A robust thread-safe concurrent guid recycler.
  • GuidRecyclerSlim
    A non-thread-safe guid recycler that is faster than the concurrent guid recycler.

Example

The guid recycler can be created manually or retrieved through dependency injection.

class UserService
{
    private readonly DatabaseContext _context;
    private readonly IGuidRecycler recycler = new GuidRecyclerSlim();

    public async Task DeleteUser(User user)
    {
        _context.Users.Remove(user);
        recycler.Recycle(user.Id);
        await _context.SaveChangesAsync();
    }

    public async Task<User> CreateUser(string name)
    {
        var user = new User
        {
            Id = recycler.GetGuid(),
            Name = name
        };
        _context.Users.Add(user);
        await _context.SaveChangesAsync();
        return user;
    }

As you can see, this code is very simple and eco-friendly. The only thing you need to do is to recycle the guid when you delete the entity and to get a new guid when you create a new entity.

Microsoft.Extensions.DependencyInjection

Coming soon

Performance

See the benchmarks in the benchmarks folder. It clearly shows that the both the concurrent guid recycler and the guid recycler slim are faster than the default guid generator. | Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Allocated | Alloc Ratio | |----------------------- |---------:|----------:|----------:|---------:|------:|--------:|----------:|------------:| | Guid.NewGuid | 647.9 ns | 113.68 ns | 327.99 ns | 600.0 ns | 1.00 | 0.00 | 600 B | 1.00 | | ConcurrentGuidRecycler | 307.1 ns | 49.97 ns | 146.56 ns | 300.0 ns | 0.58 | 0.39 | 600 B | 1.00 | | GuidRecyclerSlim | 224.0 ns | 28.33 ns | 81.75 ns | 200.0 ns | 0.45 | 0.30 | 600 B | 1.00 |

Contributing

PRs welcome. Merging not guaranteed.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 166 7/1/2023
1.0.4 127 6/26/2023
1.0.3 124 6/26/2023
1.0.2 120 6/26/2023
1.0.1 119 6/26/2023
1.0.0 129 6/26/2023
0.0.1 127 6/26/2023