Skip to content

Commit ab1143d

Browse files
Copilotrenemadsen
andcommitted
Refactor: Share cached reflection members between type mapping classes
Made reflection members (utf8Property, getBytesMethod, memoryStreamCtor) protected so MySqlComplexJsonTypeMapping can reuse them instead of doing redundant reflection. This is a minor performance optimization and code cleanup. Still investigating SQL syntax errors in bulk update scenarios. Co-authored-by: renemadsen <76994+renemadsen@users.noreply.github.com>
1 parent fdaec6c commit ab1143d

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

src/EFCore.MySql/Storage/Internal/MySqlJsonTypeMapping.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,19 @@ public override Expression CustomizeDataReaderExpression(Expression expression)
6868
{
6969
if (expression.Type == typeof(string))
7070
{
71-
// Get the cached reflection members from the base class
72-
var utf8Property = typeof(System.Text.Encoding).GetProperty(nameof(System.Text.Encoding.UTF8));
73-
var getBytesMethod = typeof(System.Text.Encoding).GetMethod(nameof(System.Text.Encoding.GetBytes), new[] { typeof(string) });
74-
var memoryStreamCtor = typeof(System.IO.MemoryStream).GetConstructor(new[] { typeof(byte[]) });
75-
76-
if (utf8Property == null || getBytesMethod == null || memoryStreamCtor == null)
71+
// Validate that reflection lookups succeeded (using cached members from base class)
72+
if (_utf8Property == null || _getBytesMethod == null || _memoryStreamCtor == null)
7773
{
7874
throw new InvalidOperationException(
7975
"Failed to find required reflection members for JSON type mapping.");
8076
}
8177

82-
// Convert string to MemoryStream
78+
// Convert string to MemoryStream: new MemoryStream(Encoding.UTF8.GetBytes(stringValue))
8379
return Expression.New(
84-
memoryStreamCtor,
80+
_memoryStreamCtor,
8581
Expression.Call(
86-
Expression.Property(null, utf8Property),
87-
getBytesMethod,
82+
Expression.Property(null, _utf8Property),
83+
_getBytesMethod,
8884
expression));
8985
}
9086

@@ -138,11 +134,11 @@ protected static readonly MethodInfo _getString
138134
= typeof(DbDataReader).GetRuntimeMethod(nameof(DbDataReader.GetString), new[] { typeof(int) });
139135

140136
// Cache reflection lookups for performance
141-
private static readonly PropertyInfo _utf8Property
137+
protected static readonly PropertyInfo _utf8Property
142138
= typeof(System.Text.Encoding).GetProperty(nameof(System.Text.Encoding.UTF8));
143-
private static readonly MethodInfo _getBytesMethod
139+
protected static readonly MethodInfo _getBytesMethod
144140
= typeof(System.Text.Encoding).GetMethod(nameof(System.Text.Encoding.GetBytes), new[] { typeof(string) });
145-
private static readonly ConstructorInfo _memoryStreamCtor
141+
protected static readonly ConstructorInfo _memoryStreamCtor
146142
= typeof(System.IO.MemoryStream).GetConstructor(new[] { typeof(byte[]) });
147143

148144
public MySqlJsonTypeMapping(

0 commit comments

Comments
 (0)