Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom EF converter for Ulid showcase #49

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading