RSCG – StronglyTypedUid
name | StronglyTypedUid |
nuget | https://www.nuget.org/packages/StronglyTypedUid/ |
link | https://github.com/vicosanz/StronglyTypedUid |
author | Victor Sánchez |
Transforming a record into a GUID
This is how you can use StronglyTypedUid .
The code that you start with is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | < Project Sdk = "Microsoft.NET.Sdk" > < PropertyGroup > < OutputType >Exe</ OutputType > < TargetFramework >net8.0</ TargetFramework > < ImplicitUsings >enable</ ImplicitUsings > < Nullable >enable</ Nullable > </ PropertyGroup > < ItemGroup > < PackageReference Include = "StronglyTypedUid" Version = "1.0.1" /> < PackageReference Include = "StronglyTypedUid.Common" Version = "1.0.1" /> < PackageReference Include = "StronglyTypedUid.Generator" Version = "1.0.1" /> </ ItemGroup > < PropertyGroup > < EmitCompilerGeneratedFiles >true</ EmitCompilerGeneratedFiles > < CompilerGeneratedFilesOutputPath >$(BaseIntermediateOutputPath)\GX</ CompilerGeneratedFilesOutputPath > </ PropertyGroup > </ Project > |
The code that you will use is
1 2 3 4 5 6 | using RecordToGuid; PersonId personId = PersonId.Empty; Console.WriteLine(personId); personId = PersonId.NewPersonId(); Console.WriteLine(personId); |
1 2 3 4 5 6 7 | using StronglyTypedUid; namespace RecordToGuid; [StronglyTypedUid] public readonly partial record struct PersonId { } |
The code that is generated is
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 | using System; using System.ComponentModel; using System.Globalization; using System.Text.Json.Serialization; using System.Text.Json; using System.Buffers; using StronglyTypedUid; #nullable enable namespace RecordToGuid; [TypeConverter( typeof (PersonIdTypeConverter))] [System.Text.Json.Serialization.JsonConverter( typeof (PersonIdJsonConverter))] public readonly partial record struct PersonId(Guid Value) : IStronglyTypedUid { public static PersonId Empty => new (Guid.Empty); public static PersonId NewPersonId() => new (Guid.NewGuid()); public static implicit operator PersonId(Guid value) => new (value); public static explicit operator Guid(PersonId value) => value.Value; public bool IsEmpty => Value == Guid.Empty; public override string ToString() => Value.ToString(); public static PersonId Parse( string text) => new PersonId(Guid.Parse(text)); public static bool TryParse( string text, out PersonId result) { try { if (Guid.TryParse(text, out Guid uid)) { result = uid; return true ; } } catch (Exception) { } result = default ; return false ; } } public class PersonIdTypeConverter : TypeConverter { private static readonly Type StringType = typeof ( string ); private static readonly Type UidType = typeof (Guid); public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) => sourceType == StringType || sourceType == UidType || base .CanConvertFrom(context, sourceType); public override object ? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) => value switch { Guid g => new PersonId(g), string stringValue => PersonId.Parse(stringValue), _ => base .ConvertFrom(context, culture, value), }; public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) => destinationType == StringType || destinationType == UidType || base .CanConvertTo(context, destinationType); public override object ? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object ? value, Type destinationType) { if (value is PersonId result) { if (destinationType == StringType) { return result.ToString(); } if (destinationType == UidType) { return (Guid)result; } } return base .ConvertTo(context, culture, value, destinationType); } } public class PersonIdJsonConverter : JsonConverter<PersonId> { public override PersonId Read( ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { try { if (reader.TokenType != JsonTokenType.String) throw new JsonException( "Expected string" ); return new PersonId( new Guid(reader.GetString())); } catch (IndexOutOfRangeException e) { throw new JsonException( "PersonId invalid: length must be 36" , e); } catch (OverflowException e) { throw new JsonException( "PersonId invalid: invalid character" , e); } } public override void Write(Utf8JsonWriter writer, PersonId value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToString()); } } |
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/StronglyTypedUid
Leave a Reply