Skip to content

Commit

Permalink
Added ef core json columns helper extension. Addded todo comments to …
Browse files Browse the repository at this point in the history
…repository.
  • Loading branch information
Felix-CodingClimber committed Apr 2, 2024
1 parent 2cea080 commit be6a08a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu-latest

Possible null reference return.

Check warning on line 30 in src/DotNetElements.Core/Core/Extensions/PropertyBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu-latest

Possible null reference return.
);

propertyBuilder.HasConversion(converter);
propertyBuilder.Metadata.SetValueConverter(converter);
propertyBuilder.Metadata.SetValueComparer(comparer);
propertyBuilder.HasColumnType("nvarchar(max)");

return propertyBuilder;
}
}
1 change: 1 addition & 0 deletions src/DotNetElements.Core/Core/ReadOnlyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public async Task<CrudResult<TProjection>> GetFilteredWithProjectionAsync<TProje
return CrudResult.OkIfNotNull(projectedEntity, CrudError.NotFound);
}

// todo filter makes not sense here (If we use GetById... we should only expect one entity)
public async Task<CrudResult<TProjection>> GetByIdWithProjectionAsync<TProjection>(
TKey id,
Expression<Func<IQueryable<TEntity>, IQueryable<TProjection>>> selector,
Expand Down
1 change: 1 addition & 0 deletions src/DotNetElements.Core/Core/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public virtual async Task ClearTable()
}

// todo protected would be better!
// todo check if we should make checkAlreadyTracked true by default
public TRelatedEntity AttachById<TRelatedEntity, TRelatedEntityKey>(TRelatedEntityKey id, bool checkAlreadyTracked = false)
where TRelatedEntity : Entity<TRelatedEntityKey>, IRelatedEntity<TRelatedEntity, TRelatedEntityKey>
where TRelatedEntityKey : notnull, IEquatable<TRelatedEntityKey>
Expand Down

0 comments on commit be6a08a

Please # to comment.