-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ef core json columns helper extension. Addded todo comments to …
…repository.
- Loading branch information
1 parent
2cea080
commit be6a08a
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/DotNetElements.Core/Core/Extensions/PropertyBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Text.Json; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
namespace DotNetElements.Core.Extensions; | ||
|
||
public static class PropertyBuilderExtensions | ||
{ | ||
public static PropertyBuilder<T> HasJsonConversion<T>(this PropertyBuilder<T> propertyBuilder) where T : class, new() | ||
{ | ||
JsonSerializerOptions options = new() | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
WriteIndented = true, // todo check if we want to make that a parameter (might make sense to only use it in debug/test builds) | ||
AllowTrailingCommas = true, | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
ValueConverter<T, string> converter = new ValueConverter<T, string> | ||
( | ||
value => JsonSerializer.Serialize(value, options), | ||
value => JsonSerializer.Deserialize<T>(value, options) ?? new T() | ||
); | ||
|
||
ValueComparer<T> comparer = new ValueComparer<T> | ||
( | ||
(valueA, valueB) => JsonSerializer.Serialize(valueA, options) == JsonSerializer.Serialize(valueB, options), | ||
value => value == null ? 0 : JsonSerializer.Serialize(value, options).GetHashCode(), | ||
value => JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(value, options), options) | ||
Check warning on line 30 in src/DotNetElements.Core/Core/Extensions/PropertyBuilderExtensions.cs GitHub Actions / build-and-test-ubuntu-latest
|
||
); | ||
|
||
propertyBuilder.HasConversion(converter); | ||
propertyBuilder.Metadata.SetValueConverter(converter); | ||
propertyBuilder.Metadata.SetValueComparer(comparer); | ||
propertyBuilder.HasColumnType("nvarchar(max)"); | ||
|
||
return propertyBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters