-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Imported Tingle.Extensions.EntityFrameworkCore
- Loading branch information
1 parent
f565a36
commit d5c65da
Showing
22 changed files
with
828 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"cSpell.words": [ | ||
"Bson", | ||
"EFCORE", | ||
"etag", | ||
"Ksuid", | ||
"libphonenumber", | ||
|
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
55 changes: 55 additions & 0 deletions
55
src/Tingle.Extensions.EntityFrameworkCore/Conventions/LengthAttributeConvention.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,55 @@ | ||
#if NET8_0_OR_GREATER | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.EntityFrameworkCore.Metadata.Conventions; | ||
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Reflection; | ||
|
||
namespace Tingle.Extensions.EntityFrameworkCore.Conventions; | ||
|
||
/// <summary> | ||
/// A convention that configures the maximum length based on the <see cref="LengthAttribute" /> applied on the property. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see> for more information and examples. | ||
/// </remarks> | ||
/// <param name="dependencies">Parameter object containing dependencies for this convention.</param> | ||
public class LengthAttributeConvention(ProviderConventionSetBuilderDependencies dependencies) : PropertyAttributeConventionBase<LengthAttribute>(dependencies), IComplexPropertyAddedConvention | ||
{ | ||
/// <inheritdoc /> | ||
protected override void ProcessPropertyAdded( | ||
IConventionPropertyBuilder propertyBuilder, | ||
LengthAttribute attribute, | ||
MemberInfo clrMember, | ||
IConventionContext context) | ||
{ | ||
if (attribute.MaximumLength > 0) | ||
{ | ||
propertyBuilder.HasMaxLength(attribute.MaximumLength, fromDataAnnotation: true); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void ProcessPropertyAdded( | ||
IConventionComplexPropertyBuilder propertyBuilder, | ||
LengthAttribute attribute, | ||
MemberInfo clrMember, | ||
IConventionContext context) | ||
{ | ||
var property = propertyBuilder.Metadata; | ||
#pragma warning disable EF1001 | ||
var member = property.GetIdentifyingMemberInfo(); | ||
#pragma warning restore EF1001 | ||
if (member != null | ||
&& Attribute.IsDefined(member, typeof(ForeignKeyAttribute), inherit: true)) | ||
{ | ||
throw new InvalidOperationException( | ||
CoreStrings.AttributeNotOnEntityTypeProperty( | ||
"MaxLength", property.DeclaringType.DisplayName(), property.Name)); | ||
} | ||
} | ||
} | ||
#endif |
24 changes: 24 additions & 0 deletions
24
src/Tingle.Extensions.EntityFrameworkCore/Converters/ByteSizeConverter.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,24 @@ | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using Tingle.Extensions.Primitives; | ||
|
||
namespace Tingle.Extensions.EntityFrameworkCore.Converters; | ||
|
||
/// | ||
public class ByteSizeConverter : ValueConverter<ByteSize, long> | ||
{ | ||
/// | ||
public ByteSizeConverter() : base(convertToProviderExpression: v => v.Bytes, | ||
convertFromProviderExpression: v => v == default ? default : new ByteSize(v)) | ||
{ } | ||
} | ||
|
||
/// | ||
public class ByteSizeComparer : ValueComparer<ByteSize> | ||
{ | ||
/// | ||
public ByteSizeComparer() : base(equalsExpression: (l, r) => l == r, | ||
hashCodeExpression: v => v.GetHashCode(), | ||
snapshotExpression: v => new ByteSize(v.Bytes)) | ||
{ } | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Tingle.Extensions.EntityFrameworkCore/Converters/DurationConverter.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,24 @@ | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using Tingle.Extensions.Primitives; | ||
|
||
namespace Tingle.Extensions.EntityFrameworkCore.Converters; | ||
|
||
/// | ||
public class DurationConverter : ValueConverter<Duration, string> | ||
{ | ||
/// | ||
public DurationConverter() : base(convertToProviderExpression: v => v.ToString(), | ||
convertFromProviderExpression: v => v == null ? default : Duration.Parse(v)) | ||
{ } | ||
} | ||
|
||
/// | ||
public class DurationComparer : ValueComparer<Duration> | ||
{ | ||
/// | ||
public DurationComparer() : base(equalsExpression: (l, r) => l == r, | ||
hashCodeExpression: v => v.GetHashCode(), | ||
snapshotExpression: v => Duration.Parse(v.ToString())) | ||
{ } | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Tingle.Extensions.EntityFrameworkCore/Converters/EtagConverter.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,24 @@ | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using Tingle.Extensions.Primitives; | ||
|
||
namespace Tingle.Extensions.EntityFrameworkCore.Converters; | ||
|
||
/// | ||
public class EtagConverter : ValueConverter<Etag, byte[]> | ||
{ | ||
/// | ||
public EtagConverter() : base(convertToProviderExpression: v => v.ToByteArray(), | ||
convertFromProviderExpression: v => v == null ? default : new Etag(v)) | ||
{ } | ||
} | ||
|
||
/// | ||
public class EtagComparer : ValueComparer<Etag> | ||
{ | ||
/// | ||
public EtagComparer() : base(equalsExpression: (l, r) => l == r, | ||
hashCodeExpression: v => v.GetHashCode(), | ||
snapshotExpression: v => new Etag(v.ToByteArray())) | ||
{ } | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Tingle.Extensions.EntityFrameworkCore/Converters/IPNetworkConverter.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,26 @@ | ||
#if NET8_0_OR_GREATER | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using System.Net; | ||
|
||
namespace Tingle.Extensions.EntityFrameworkCore.Converters; | ||
|
||
/// | ||
public class IPNetworkConverter : ValueConverter<IPNetwork, string> | ||
{ | ||
/// | ||
public IPNetworkConverter() : base(convertToProviderExpression: v => v.ToString(), | ||
convertFromProviderExpression: v => v == null ? default : IPNetwork.Parse(v)) | ||
{ } | ||
} | ||
|
||
/// | ||
public class IPNetworkComparer : ValueComparer<IPNetwork> | ||
{ | ||
/// | ||
public IPNetworkComparer() : base(equalsExpression: (l, r) => l == r, | ||
hashCodeExpression: v => v.GetHashCode(), | ||
snapshotExpression: v => IPNetwork.Parse(v.ToString())) | ||
{ } | ||
} | ||
#endif |
26 changes: 26 additions & 0 deletions
26
src/Tingle.Extensions.EntityFrameworkCore/Converters/JsonElementConverter.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,26 @@ | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using System.Text.Json; | ||
using SC = Tingle.Extensions.EntityFrameworkCore.EfCoreJsonSerializerContext; | ||
|
||
namespace Tingle.Extensions.EntityFrameworkCore.Converters; | ||
|
||
/// | ||
public class JsonElementConverter : ValueConverter<JsonElement, string> | ||
{ | ||
/// | ||
public JsonElementConverter() : base(convertToProviderExpression: v => v.ToString(), | ||
convertFromProviderExpression: v => v == null ? default : JsonDocument.Parse(v, default).RootElement) | ||
{ } | ||
} | ||
|
||
/// | ||
public class JsonElementComparer : ValueComparer<JsonElement> | ||
{ | ||
/// | ||
public JsonElementComparer() : base( | ||
equalsExpression: (l, r) => JsonSerializer.Serialize(l, SC.Default.JsonElement) == JsonSerializer.Serialize(r, SC.Default.JsonElement), | ||
hashCodeExpression: v => v.GetHashCode(), | ||
snapshotExpression: v => JsonDocument.Parse(JsonSerializer.Serialize(v, SC.Default.JsonElement), default).RootElement) | ||
{ } | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Tingle.Extensions.EntityFrameworkCore/Converters/JsonNodeConverter.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,27 @@ | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using System.Text.Json.Nodes; | ||
|
||
#pragma warning disable CS8603 // Possible null reference return. | ||
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code | ||
|
||
namespace Tingle.Extensions.EntityFrameworkCore.Converters; | ||
|
||
/// | ||
public class JsonNodeConverter : ValueConverter<JsonNode, string> | ||
{ | ||
/// | ||
public JsonNodeConverter() : base(convertToProviderExpression: v => v.ToJsonString(default), | ||
convertFromProviderExpression: v => v == null ? default : JsonNode.Parse(v, default, default)) | ||
{ } | ||
} | ||
|
||
/// | ||
public class JsonNodeComparer : ValueComparer<JsonNode> | ||
{ | ||
/// | ||
public JsonNodeComparer() : base(equalsExpression: (l, r) => (l == null ? null : l.ToJsonString(default)) == (r == null ? null : r.ToJsonString(default)), | ||
hashCodeExpression: v => v == null ? 0 : v.ToJsonString(default).GetHashCode(), | ||
snapshotExpression: v => JsonNode.Parse(v.ToJsonString(default), default, default)) | ||
{ } | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Tingle.Extensions.EntityFrameworkCore/Converters/JsonObjectConverter.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,27 @@ | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using System.Text.Json.Nodes; | ||
|
||
#pragma warning disable CS8603 // Possible null reference return. | ||
#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code | ||
|
||
namespace Tingle.Extensions.EntityFrameworkCore.Converters; | ||
|
||
/// | ||
public class JsonObjectConverter : ValueConverter<JsonObject, string> | ||
{ | ||
/// | ||
public JsonObjectConverter() : base(convertToProviderExpression: v => v.ToJsonString(default), | ||
convertFromProviderExpression: v => v == null ? default : JsonNode.Parse(v, default, default)!.AsObject()) | ||
{ } | ||
} | ||
|
||
/// | ||
public class JsonObjectComparer : ValueComparer<JsonObject> | ||
{ | ||
/// | ||
public JsonObjectComparer() : base(equalsExpression: (l, r) => (l == null ? null : l.ToJsonString(default)) == (r == null ? null : r.ToJsonString(default)), | ||
hashCodeExpression: v => v == null ? 0 : v.ToJsonString(default).GetHashCode(), | ||
snapshotExpression: v => v == null ? null : JsonNode.Parse(v.ToJsonString(default), default, default)!.AsObject()) | ||
{ } | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Tingle.Extensions.EntityFrameworkCore/Converters/SequenceNumberConverter.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,24 @@ | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using Tingle.Extensions.Primitives; | ||
|
||
namespace Tingle.Extensions.EntityFrameworkCore.Converters; | ||
|
||
/// | ||
public class SequenceNumberConverter : ValueConverter<SequenceNumber, long> | ||
{ | ||
/// | ||
public SequenceNumberConverter() : base(convertToProviderExpression: v => v.Value, | ||
convertFromProviderExpression: v => v == SequenceNumber.Empty ? default : new SequenceNumber(v)) | ||
{ } | ||
} | ||
|
||
/// | ||
public class SequenceNumberComparer : ValueComparer<SequenceNumber> | ||
{ | ||
/// | ||
public SequenceNumberComparer() : base(equalsExpression: (l, r) => l == r, | ||
hashCodeExpression: v => v.GetHashCode(), | ||
snapshotExpression: v => new SequenceNumber(v.Value)) | ||
{ } | ||
} |
Oops, something went wrong.