Migration Guide - Taiizor/UUID GitHub Wiki
This guide helps you migrate from other UUID/Guid implementations to our UUID library.
// Before (Guid)
Guid guid = Guid.NewGuid();
string guidString = guid.ToString();
// After (UUID)
UUID uuid = new UUID();
string uuidString = uuid.ToString();
// Convert from Guid to UUID
Guid existingGuid = Guid.NewGuid();
UUID uuid = UUID.FromGuid(existingGuid);
// Convert back to Guid if needed
Guid convertedBack = uuid.ToGuid();
public class MyDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>()
.Property(e => e.Id)
.HasConversion(
uuid => uuid.ToString(),
str => UUID.Parse(str)
);
}
}
public class UUIDTypeHandler : SqlMapper.TypeHandler<UUID>
{
public override UUID Parse(object value)
{
return UUID.Parse((string)value);
}
public override void SetValue(IDbDataParameter parameter, UUID value)
{
parameter.Value = value.ToString();
}
}
// Register the handler
SqlMapper.AddTypeHandler(new UUIDTypeHandler());
// Common string formats supported
UUID uuid = new UUID();
// Standard format
string standard = uuid.ToString(); // "0123456789ABCDEF0123456789ABCDEF"
// URL-safe format
string urlSafe = uuid.ToBase32(); // "028T5CY4TQKFF028T5CY4TQKFF"
// Base64 format
string base64 = uuid.ToBase64(); // "782riWdFIwHvzauJZ0UjAQ=="
// Reusable buffers for high-performance scenarios
public class UUIDConverter
{
private readonly char[] _buffer = new char[32];
public string FastConvert(UUID uuid)
{
if (uuid.TryWriteStringify(_buffer))
{
return new string(_buffer);
}
return uuid.ToString();
}
}
- Requires .NET SDK 9.0 or newer
- Visual Studio 2022 or newer for development
// Before
public class Repository
{
public async Task<Entity> GetById(Guid id)
{
// ...
}
}
// After
public class Repository
{
public async Task<Entity> GetById(UUID id)
{
// ...
}
}
// Before
[HttpGet("{id:guid}")]
public IActionResult Get(Guid id)
{
// ...
}
// After
[HttpGet("{id}")]
public IActionResult Get(UUID id)
{
// ...
}
// Before
public class Entity
{
public Guid Id { get; set; }
}
// After
public class Entity
{
public UUID Id { get; set; }
}
-
Gradual Migration
- Start with new features
- Migrate existing features incrementally
- Use conversion methods for compatibility
-
Performance Considerations
- Use bulk operations where possible
- Implement buffer pooling for string operations
- Take advantage of thread-safe features
-
Testing
- Update unit tests to use UUID
- Test database operations thoroughly
- Verify string format compatibility