Skip to content

Commit

Permalink
Add custom EF converter for Ulid showcase
Browse files Browse the repository at this point in the history
  • Loading branch information
kzu committed Dec 21, 2024
1 parent e9bd923 commit 144056c
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/StructId.FunctionalTests/UlidTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ public override void SetValue(IDbDataParameter parameter, Ulid value)

// showcases a custom EF value converter trumps the built-in templatized
// support for types that provide IParsable<T> and IFormattable
//public partial class UlidToStringConverter : ValueConverter<Ulid, string>
//{
// public UlidToStringConverter() : this(null) { }
public partial class UlidToStringConverter : ValueConverter<Ulid, string>
{
public UlidToStringConverter() : this(null) { }

// public UlidToStringConverter(ConverterMappingHints? mappingHints = null)
// : base(id => id.ToString(), value => Ulid.Parse(value), mappingHints) { }
//}
public UlidToStringConverter(ConverterMappingHints? mappingHints = null)
: base(id => id.ToString(), value => Ulid.Parse(value), mappingHints)
{
Instantiated = true;
}

public static bool Instantiated { get; private set; }
}

// showcases alternative serialization
//public class BinaryUlidHandler : TypeHandler<Ulid>
Expand Down Expand Up @@ -147,6 +152,8 @@ public void EntityFramework()

var product3 = context.Products.FirstOrDefault(x => guid == x.Id);
Assert.Equal(product, product3);

Assert.True(UlidToStringConverter.Instantiated, "Custom EF converter for Ulid was not used.");
}

public class UlidContext : DbContext
Expand Down

0 comments on commit 144056c

Please sign in to comment.