Id22 0.1.5

dotnet add package Id22 --version 0.1.5
                    
NuGet\Install-Package Id22 -Version 0.1.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="Id22" Version="0.1.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Id22" Version="0.1.5" />
                    
Directory.Packages.props
<PackageReference Include="Id22" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Id22 --version 0.1.5
                    
#r "nuget: Id22, 0.1.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.
#addin nuget:?package=Id22&version=0.1.5
                    
Install as a Cake Addin
#tool nuget:?package=Id22&version=0.1.5
                    
Install as a Cake Tool

Id22

A wrapper against Guid.

Purpose

I made this library to solve the issue of serializing Guid to string for web apis.

It makes them url friendly with the usage of Base64 and short (22 characters).

While it's possible to use libraries for the purpose of converting to such, no library (that I know of) has made a type that can be stored as Guid, yet presented as string.

So this library provides, with the help of converters, Id22 can be used instead of Guid, to be stored as Guid, but viewed as a nice short string.

Usage

To use in you code, you just need this:

public class Book
{
    // No need to add or prepare anything, except for ef DbContext.
    public Id22 Id { get; set; } 
    
    public required string Name { get; set; }

    // Maybe this attribute is not necessary. 
    // Testing is required.
    [JsonConverter(typeof(Id22Converters.NullableJsonConverter))]
    public Id22? PublisherId { get; set; }
}

Utils

Id22 provides:

  • New to create new value.
  • ValueOrNew checks if value is valid for use. Returns new one if not.
  • ValueOrDefault checks if value is valid for use. Returns default if not.
  • IsEmpty to check if value is null, default or empty guid.

String Utils:

  • ToShortId to convert Id22 to base64 22-character string.
  • FromShortId to convert string to Id22 if possible.
  • StringIsNotValidShortId to check whether it's possible to convert string back to Id22 or not.
  • Parse to convert string to Id22 whether it's guid format or short id format.

Converters

Converters are provided by System.Id22Converters and Microsoft.EntityFramework.Id22Converters classes.

All converters are using ToString() when converting to string value so implementation is unified across them. This means if you use string converter for DbContext, it will store short ids. This may cause issues, so better not to use it, but use guid converter instead.

System.Id22Converters

This class provides implementations for:

  • TypeConverter: applied by [TypeConverter] annotation. No need for extra setup.
  • JsonConverter: also applied by [JsonConverter] annotation. Might need to apply nullable converter.

Microsoft.EntityFramework.Id22Converters

This class provides implementations of ValueConverter for EF Core.

It's available through nuget package Id22.EntityFrameworkCore.

Setup

Usage with DbContext:

public class DataContext : DbContext
{
    protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
    {
        base.ConfigureConventions(configurationBuilder);

        // ... configs

        configurationBuilder.MapId22ToGuid(); 

        // or to store as string

        // If string values can be generated as guid from the db,
        // then avoid using this.
        configurationBuilder.MapId22ToString();

        // ... configs
    }
}

Tips

If you have a global using file, you can add:

global using Id = Id22;

so you use Id instead of Id22.

Swagger

When using swagger, it can't pickup the json serializer.

So add this config line to show al Id22 properties as string:

builder.Services.AddSwaggerGen(
    (opt) =>
    {

        ...configs

        opt.MapType<Id>(
            () =>
                new OpenApiSchema
                {
                    Type = "string",
                    Example = new OpenApiString("A1C2E3G4I5K6M7O8Q9E0-_"),
                }
        );

        ...configs
    }
)
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Id22:

Package Downloads
Id22.EntityFrameworkCore

EntityFrameworkCore utils for Id22.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.5 136 9/14/2024
0.1.3-prerelease 100 9/13/2024
0.1.2-prerelease 102 9/13/2024
0.1.0-prerelease 105 9/13/2024