Mecesoft.ShortGuid
1.0.4
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Mecesoft.ShortGuid --version 1.0.4
NuGet\Install-Package Mecesoft.ShortGuid -Version 1.0.4
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="Mecesoft.ShortGuid" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mecesoft.ShortGuid" Version="1.0.4" />
<PackageReference Include="Mecesoft.ShortGuid" />
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 Mecesoft.ShortGuid --version 1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Mecesoft.ShortGuid, 1.0.4"
#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.
#:package Mecesoft.ShortGuid@1.0.4
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mecesoft.ShortGuid&version=1.0.4
#tool nuget:?package=Mecesoft.ShortGuid&version=1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Mecesoft.ShortGuid
Guid için 22 karakterlik, yalnızca alfasayısal (Base62) kısa temsil sunan bir sarmalayıcı (ShortGuid) ve .NET/EF Core entegrasyonları.
Özellikler
- 22 karakterlik ShortGuid: 0-9, A-Z, a-z (yalnızca alfasayısal). Özel karakter yok. Base62 kodlama.
- Geriye dönük uyumluluk: 22 karakterlik URL‑güvenli Base64 ("-" ve "_") girdilerini de okuyabilir.
- System.Text.Json için gömülü JsonConverter (atribüt ile).
- Binding senaryoları için TypeConverter.
- EF Core için ValueConverter ve model geneli için kolay uzantı (UseShortGuidConverters).
- net9.0 hedefi, Nullable ve ImplicitUsings etkin.
Kurulum
- Projeyi referans alın veya NuGet paketini oluşturup yayınlayın.
- NuGet package ID: Mecesoft.ShortGuid (paketi yerel olarak
dotnet pack
ile üretebilirsiniz).
Hızlı Başlangıç
- ShortGuid oluşturma ve yazdırma
using Mecesoft.ShortGuid;
var sg = ShortGuid.NewGuid();
string s = sg.ToString(); // 22 karakter (Base62)
ShortGuid parsed = ShortGuid.Parse(s); // round‑trip
Guid g = sg; // implicit cast ShortGuid -> Guid
ShortGuid sg2 = (ShortGuid)g; // implicit cast Guid -> ShortGuid
- System.Text.Json ile kullanım (atribüt zaten tanımlı)
using System.Text.Json;
using Mecesoft.ShortGuid;
var sg = ShortGuid.NewGuid();
var json = JsonSerializer.Serialize(new { id = sg });
var obj = JsonSerializer.Deserialize<Wrapper>(json);
public record Wrapper(ShortGuid id);
- EF Core modeli genelinde dönüştürücü uygulama
using Mecesoft.ShortGuid.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
public class AppDbContext : DbContext
{
public DbSet<User> Users => Set<User>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseShortGuidConverters(); // ShortGuid <-> Guid converter tüm ShortGuid alanlarına uygulanır
}
}
public class User
{
public ShortGuid Id { get; set; }
}
ShortGuid biçimi ve uyumluluk
- Birincil çıktı: Base62 ile kodlanmış 22 karakterlik dize (yalnızca 0-9, A-Z, a-z).
- Giriş kabulü: Parse/TryParse şunları kabul eder:
- Normal Guid dizesi (ör. 32 hex + tireli standart biçimler),
- 22 karakterlik Base62 ShortGuid,
- 22 karakterlik URL‑güvenli Base64 ShortGuid ("+" → "-", "/" → "_", sondaki "==" olmadan). Bu mod geriye dönük uyumluluk içindir.
Test Etme (isteğe bağlı örnek)
- xUnit test projesi oluşturun (Mecesoft.ShortGuid klasörü ile aynı seviyede): TempTests/TempTests.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7" />
<PackageReference Include="coverlet.collector" Version="6.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Mecesoft.ShortGuid/Mecesoft.ShortGuid.csproj" />
</ItemGroup>
</Project>
- Basit test ekleyin (ShortGuidTests.cs)
using Mecesoft.ShortGuid;
using System.Text.Json;
using Xunit;
public class ShortGuidTests
{
[Fact]
public void NewGuid_ToString_Parse_Roundtrip()
{
var sg = ShortGuid.NewGuid();
var s = sg.ToString();
Assert.Equal(22, s.Length);
var parsed = ShortGuid.Parse(s);
Assert.Equal(sg, parsed);
}
[Theory]
[InlineData("5Qqz8l5wQ0mY1m6dH7e5xQ")] // URL‑safe Base64 örneği; geriye dönük kabul edilir
public void TryParse_Valid22Char_ReturnsTrue(string input)
{
var ok = ShortGuid.TryParse(input, out var sg);
Assert.True(ok);
Assert.Equal(input, sg.ToString());
}
[Fact]
public void Json_Serializes_And_Deserializes()
{
var sg = ShortGuid.NewGuid();
var json = JsonSerializer.Serialize(new { id = sg });
var obj = JsonSerializer.Deserialize<Wrapper>(json);
Assert.NotNull(obj);
Assert.Equal(sg, obj!.id);
}
private record Wrapper(ShortGuid id);
}
- Çalıştırın:
dotnet test TempTests/TempTests.csproj -c Release
Derleme ve Paketleme
- Gereksinimler: .NET SDK 9.x ve NuGet restore için internet.
- Derle:
dotnet build Mecesoft.ShortGuid/Mecesoft.ShortGuid.csproj -c Release
- Paketle:
dotnet pack Mecesoft.ShortGuid/Mecesoft.ShortGuid.csproj -c Release
- Projede
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
ayarlı olduğundandotnet build
de .nupkg üretir.
- Projede
EF Core entegrasyonu (notlar)
- Dönüştürücü: ShortGuidConverter : ValueConverter<ShortGuid, Guid> (veritabanında kolon türü Guid olarak kalır).
- Model genelinde uygulama:
modelBuilder.UseShortGuidConverters()
çağırın. - Değer nesneleri kullanıyorsanız EF’in CLR türünü gerçekten ShortGuid olarak gördüğünden emin olun; değilse özelliğe özel converter tanımlayın.
Sorun giderme
- Testler bulunamıyorsa
dotnet test
komutunu kullanın ve Microsoft.NET.Test.Sdk + xunit.runner.visualstudio paketlerinin eklendiğini doğrulayın. - JSON için System.Text.Json kullanın (Newtonsoft değil); ShortGuid üzerinde [JsonConverter] zaten tanımlıdır.
- EF Core bağlama hatalarında OnModelCreating içinde UseShortGuidConverters() çağrıldığını teyit edin.
Lisans
- MIT License (bkz. LICENSE).
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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.
-
net9.0
- Microsoft.EntityFrameworkCore (>= 9.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.