From 4343cab0221584806930af2a9e74a52ecbdf1706 Mon Sep 17 00:00:00 2001
From: Marc Gravell <marc.gravell@gmail.com>
Date: Fri, 8 Dec 2023 12:01:43 +0000
Subject: [PATCH] mirror Dapper behaviour re string length; see #95 (#96)

---
 .../DapperInterceptorGenerator.cs             | 25 +++++++++++++++----
 src/Dapper.AOT/CommandFactory.cs              | 23 +++++++++++++++++
 .../Interceptors/BaseCommandFactory.output.cs |  3 +--
 .../BaseCommandFactory.output.netfx.cs        |  3 +--
 .../Interceptors/BatchSize.output.cs          |  6 ++---
 .../Interceptors/BatchSize.output.netfx.cs    |  6 ++---
 .../Interceptors/CacheCommand.output.cs       |  6 ++---
 .../Interceptors/CacheCommand.output.netfx.cs |  6 ++---
 .../Interceptors/CommandProperties.output.cs  |  6 ++---
 .../CommandProperties.output.netfx.cs         |  6 ++---
 .../Interceptors/Execute.output.cs            |  3 +--
 .../Interceptors/Execute.output.netfx.cs      |  3 +--
 .../Interceptors/ExecuteBatch.output.cs       |  6 ++---
 .../Interceptors/ExecuteBatch.output.netfx.cs |  6 ++---
 .../Interceptors/ExecuteScalar.output.cs      |  3 +--
 .../ExecuteScalar.output.netfx.cs             |  3 +--
 .../Interceptors/GlobalFetchSize.output.cs    |  3 +--
 .../GlobalFetchSize.output.netfx.cs           |  3 +--
 .../Interceptors/Query.output.cs              |  3 +--
 .../Interceptors/Query.output.netfx.cs        |  3 +--
 .../Interceptors/QueryDetection.output.cs     |  3 +--
 .../QueryDetection.output.netfx.cs            |  3 +--
 .../Interceptors/QueryNonGeneric.output.cs    |  3 +--
 .../QueryNonGeneric.output.netfx.cs           |  3 +--
 .../Interceptors/QueryUntyped.output.cs       |  3 +--
 .../Interceptors/QueryUntyped.output.netfx.cs |  3 +--
 .../Interceptors/RequiredProperties.output.cs |  3 +--
 .../RequiredProperties.output.netfx.cs        |  3 +--
 .../Interceptors/RowCountHint.output.cs       |  6 ++---
 .../Interceptors/RowCountHint.output.netfx.cs |  6 ++---
 .../Interceptors/Single.output.cs             |  3 +--
 .../Interceptors/Single.output.netfx.cs       |  3 +--
 .../Interceptors/TsqlTips.output.cs           |  3 +--
 .../Interceptors/TsqlTips.output.netfx.cs     |  3 +--
 34 files changed, 85 insertions(+), 89 deletions(-)

diff --git a/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs b/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs
index fd611ffe..c3d598d4 100644
--- a/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs
+++ b/src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs
@@ -17,7 +17,6 @@
 using System.Globalization;
 using System.Linq;
 using System.Text;
-using System.Text.RegularExpressions;
 using System.Threading;
 
 namespace Dapper.CodeAnalysis;
@@ -1034,6 +1033,7 @@ private static void WriteArgs(ITypeSymbol? parameterType, CodeWriter sb, WriteAr
 
                     var dbType = member.GetDbType(out _);
                     var size = member.TryGetValue<int>("Size");
+                    bool useSetValueWithDefaultSize = false;
                     if (dbType is not null)
                     {
                         sb.Append("p.DbType = global::System.Data.DbType.").Append(dbType.GetValueOrDefault().ToString()).Append(";").NewLine();
@@ -1044,7 +1044,14 @@ private static void WriteArgs(ITypeSymbol? parameterType, CodeWriter sb, WriteAr
                                 case DbType.Binary:
                                 case DbType.String:
                                 case DbType.AnsiString:
-                                    size = -1; // default to [n]varchar(max)/varbinary(max)
+                                    if (member.CodeType.SpecialType == SpecialType.System_String)
+                                    {
+                                        useSetValueWithDefaultSize = true;
+                                    }
+                                    else
+                                    {
+                                        size = -1; // default to [n]varchar(max)/varbinary(max)
+                                    }
                                     break;
                             }
                         }
@@ -1066,15 +1073,23 @@ private static void WriteArgs(ITypeSymbol? parameterType, CodeWriter sb, WriteAr
                         ParameterDirection.Output => nameof(ParameterDirection.Output),
                         ParameterDirection.ReturnValue => nameof(ParameterDirection.ReturnValue),
                         _ => direction.ToString(),
-                    }).Append(";").NewLine().Append("p.Value = ");
+                    }).Append(";").NewLine();
+                    // the actual value expression
                     switch (direction)
                     {
                         case ParameterDirection.Input:
                         case ParameterDirection.InputOutput:
-                            sb.Append("AsValue(").Append(source).Append(".").Append(member.CodeName).Append(");").NewLine();
+                            if (useSetValueWithDefaultSize)
+                            {
+                                sb.Append("SetValueWithDefaultSize(p, ").Append(source).Append(".").Append(member.CodeName).Append(");").NewLine();
+                            }
+                            else
+                            {
+                                sb.Append("p.Value = ").Append("AsValue(").Append(source).Append(".").Append(member.CodeName).Append(");").NewLine();
+                            }
                             break;
                         default:
-                            sb.Append("global::System.DBNull.Value;").NewLine();
+                            sb.Append("p.Value = global::System.DBNull.Value;").NewLine();
                             break;
                     }
                     sb.Append("ps.Add(p);").NewLine();
diff --git a/src/Dapper.AOT/CommandFactory.cs b/src/Dapper.AOT/CommandFactory.cs
index ea0e0dda..0c97df16 100644
--- a/src/Dapper.AOT/CommandFactory.cs
+++ b/src/Dapper.AOT/CommandFactory.cs
@@ -119,6 +119,29 @@ internal static object AsGenericValue<T>(T value)
         return AsValue((object?)value);
     }
 
+    /// <summary>
+    /// Gets the default size to use for parameters; to override, see <see cref="DbValueAttribute.Size"/>
+    /// </summary>
+    [MethodImpl(MethodImplOptions.AggressiveInlining)]
+    protected static void SetValueWithDefaultSize(DbParameter parameter, string? value)
+    {
+        // fixing the defined length avoids query plan mismatch due to constantly different
+        // parameters; mirror Dapper vanilla size behaviour; see
+        // https://github.com/DapperLib/DapperAOT/issues/95 for more context
+        const int DefaultLength = 4000, Max = -1; // see DbString.DefaultLength in Dapper
+        if (value is null)
+        {
+            parameter.Value = DBNull.Value;
+            parameter.Size = DefaultLength;
+        }
+        else
+        {
+            parameter.Value = value;
+            var len = value.Length;
+            parameter.Size = len <= DefaultLength ? DefaultLength : Max;
+        }
+    }
+
     /// <summary>
     /// Flexibly parse an <see cref="object"/> as a value of type <typeparamref name="T"/>.
     /// </summary>
diff --git a/test/Dapper.AOT.Test/Interceptors/BaseCommandFactory.output.cs b/test/Dapper.AOT.Test/Interceptors/BaseCommandFactory.output.cs
index 5dfa50c0..3eb0555a 100644
--- a/test/Dapper.AOT.Test/Interceptors/BaseCommandFactory.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/BaseCommandFactory.output.cs
@@ -54,9 +54,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/BaseCommandFactory.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/BaseCommandFactory.output.netfx.cs
index 5dfa50c0..3eb0555a 100644
--- a/test/Dapper.AOT.Test/Interceptors/BaseCommandFactory.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/BaseCommandFactory.output.netfx.cs
@@ -54,9 +54,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/BatchSize.output.cs b/test/Dapper.AOT.Test/Interceptors/BatchSize.output.cs
index 8c603f88..522793a7 100644
--- a/test/Dapper.AOT.Test/Interceptors/BatchSize.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/BatchSize.output.cs
@@ -68,9 +68,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
@@ -104,9 +103,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/BatchSize.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/BatchSize.output.netfx.cs
index 8c603f88..522793a7 100644
--- a/test/Dapper.AOT.Test/Interceptors/BatchSize.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/BatchSize.output.netfx.cs
@@ -68,9 +68,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
@@ -104,9 +103,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/CacheCommand.output.cs b/test/Dapper.AOT.Test/Interceptors/CacheCommand.output.cs
index da70b0f4..5927aa72 100644
--- a/test/Dapper.AOT.Test/Interceptors/CacheCommand.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/CacheCommand.output.cs
@@ -99,9 +99,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
@@ -197,9 +196,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                     p = cmd.CreateParameter();
                     p.ParameterName = "bar";
                     p.DbType = global::System.Data.DbType.String;
-                    p.Size = -1;
                     p.Direction = global::System.Data.ParameterDirection.Input;
-                    p.Value = AsValue(typed.bar);
+                    SetValueWithDefaultSize(p, typed.bar);
                     ps.Add(p);
 
                 }
diff --git a/test/Dapper.AOT.Test/Interceptors/CacheCommand.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/CacheCommand.output.netfx.cs
index da70b0f4..5927aa72 100644
--- a/test/Dapper.AOT.Test/Interceptors/CacheCommand.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/CacheCommand.output.netfx.cs
@@ -99,9 +99,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
@@ -197,9 +196,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                     p = cmd.CreateParameter();
                     p.ParameterName = "bar";
                     p.DbType = global::System.Data.DbType.String;
-                    p.Size = -1;
                     p.Direction = global::System.Data.ParameterDirection.Input;
-                    p.Value = AsValue(typed.bar);
+                    SetValueWithDefaultSize(p, typed.bar);
                     ps.Add(p);
 
                 }
diff --git a/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.cs b/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.cs
index 409c95a9..d40b277b 100644
--- a/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.cs
@@ -236,9 +236,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
@@ -342,9 +341,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.netfx.cs
index 409c95a9..d40b277b 100644
--- a/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/CommandProperties.output.netfx.cs
@@ -236,9 +236,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
@@ -342,9 +341,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/Execute.output.cs b/test/Dapper.AOT.Test/Interceptors/Execute.output.cs
index 2eee2359..c88e316e 100644
--- a/test/Dapper.AOT.Test/Interceptors/Execute.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/Execute.output.cs
@@ -116,9 +116,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/Execute.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/Execute.output.netfx.cs
index 2eee2359..c88e316e 100644
--- a/test/Dapper.AOT.Test/Interceptors/Execute.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/Execute.output.netfx.cs
@@ -116,9 +116,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/ExecuteBatch.output.cs b/test/Dapper.AOT.Test/Interceptors/ExecuteBatch.output.cs
index b9b1efed..32be50b4 100644
--- a/test/Dapper.AOT.Test/Interceptors/ExecuteBatch.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/ExecuteBatch.output.cs
@@ -165,9 +165,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
                 p = cmd.CreateParameter();
                 p.ParameterName = "Y";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(args.Y);
+                SetValueWithDefaultSize(p, args.Y);
                 ps.Add(p);
 
                 p = cmd.CreateParameter();
@@ -208,9 +207,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/ExecuteBatch.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/ExecuteBatch.output.netfx.cs
index b9b1efed..32be50b4 100644
--- a/test/Dapper.AOT.Test/Interceptors/ExecuteBatch.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/ExecuteBatch.output.netfx.cs
@@ -165,9 +165,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
                 p = cmd.CreateParameter();
                 p.ParameterName = "Y";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(args.Y);
+                SetValueWithDefaultSize(p, args.Y);
                 ps.Add(p);
 
                 p = cmd.CreateParameter();
@@ -208,9 +207,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/ExecuteScalar.output.cs b/test/Dapper.AOT.Test/Interceptors/ExecuteScalar.output.cs
index ea64d934..1615d0db 100644
--- a/test/Dapper.AOT.Test/Interceptors/ExecuteScalar.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/ExecuteScalar.output.cs
@@ -198,9 +198,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/ExecuteScalar.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/ExecuteScalar.output.netfx.cs
index ea64d934..1615d0db 100644
--- a/test/Dapper.AOT.Test/Interceptors/ExecuteScalar.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/ExecuteScalar.output.netfx.cs
@@ -198,9 +198,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/GlobalFetchSize.output.cs b/test/Dapper.AOT.Test/Interceptors/GlobalFetchSize.output.cs
index 50f19aba..3a99b072 100644
--- a/test/Dapper.AOT.Test/Interceptors/GlobalFetchSize.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/GlobalFetchSize.output.cs
@@ -104,9 +104,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/GlobalFetchSize.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/GlobalFetchSize.output.netfx.cs
index 50f19aba..3a99b072 100644
--- a/test/Dapper.AOT.Test/Interceptors/GlobalFetchSize.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/GlobalFetchSize.output.netfx.cs
@@ -104,9 +104,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/Query.output.cs b/test/Dapper.AOT.Test/Interceptors/Query.output.cs
index 62f7cca7..6e8f3a26 100644
--- a/test/Dapper.AOT.Test/Interceptors/Query.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/Query.output.cs
@@ -239,9 +239,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/Query.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/Query.output.netfx.cs
index 34f93b88..017f4a07 100644
--- a/test/Dapper.AOT.Test/Interceptors/Query.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/Query.output.netfx.cs
@@ -211,9 +211,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/QueryDetection.output.cs b/test/Dapper.AOT.Test/Interceptors/QueryDetection.output.cs
index 6aa26d85..cac3c0ee 100644
--- a/test/Dapper.AOT.Test/Interceptors/QueryDetection.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/QueryDetection.output.cs
@@ -188,9 +188,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
                 p = cmd.CreateParameter();
                 p.ParameterName = "Name";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(args.Name);
+                SetValueWithDefaultSize(p, args.Name);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/QueryDetection.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/QueryDetection.output.netfx.cs
index 6aa26d85..cac3c0ee 100644
--- a/test/Dapper.AOT.Test/Interceptors/QueryDetection.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/QueryDetection.output.netfx.cs
@@ -188,9 +188,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
                 p = cmd.CreateParameter();
                 p.ParameterName = "Name";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(args.Name);
+                SetValueWithDefaultSize(p, args.Name);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/QueryNonGeneric.output.cs b/test/Dapper.AOT.Test/Interceptors/QueryNonGeneric.output.cs
index 123c1b40..b44b6036 100644
--- a/test/Dapper.AOT.Test/Interceptors/QueryNonGeneric.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/QueryNonGeneric.output.cs
@@ -272,9 +272,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/QueryNonGeneric.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/QueryNonGeneric.output.netfx.cs
index 008600ae..11129719 100644
--- a/test/Dapper.AOT.Test/Interceptors/QueryNonGeneric.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/QueryNonGeneric.output.netfx.cs
@@ -246,9 +246,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/QueryUntyped.output.cs b/test/Dapper.AOT.Test/Interceptors/QueryUntyped.output.cs
index 568f320f..9da4f652 100644
--- a/test/Dapper.AOT.Test/Interceptors/QueryUntyped.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/QueryUntyped.output.cs
@@ -404,9 +404,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/QueryUntyped.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/QueryUntyped.output.netfx.cs
index f6cefc3b..0abc1808 100644
--- a/test/Dapper.AOT.Test/Interceptors/QueryUntyped.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/QueryUntyped.output.netfx.cs
@@ -348,9 +348,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/RequiredProperties.output.cs b/test/Dapper.AOT.Test/Interceptors/RequiredProperties.output.cs
index 37e3ec31..64916607 100644
--- a/test/Dapper.AOT.Test/Interceptors/RequiredProperties.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/RequiredProperties.output.cs
@@ -118,9 +118,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
                 p = cmd.CreateParameter();
                 p.ParameterName = "Title";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(args.Title);
+                SetValueWithDefaultSize(p, args.Title);
                 ps.Add(p);
 
                 p = cmd.CreateParameter();
diff --git a/test/Dapper.AOT.Test/Interceptors/RequiredProperties.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/RequiredProperties.output.netfx.cs
index 37e3ec31..64916607 100644
--- a/test/Dapper.AOT.Test/Interceptors/RequiredProperties.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/RequiredProperties.output.netfx.cs
@@ -118,9 +118,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
                 p = cmd.CreateParameter();
                 p.ParameterName = "Title";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(args.Title);
+                SetValueWithDefaultSize(p, args.Title);
                 ps.Add(p);
 
                 p = cmd.CreateParameter();
diff --git a/test/Dapper.AOT.Test/Interceptors/RowCountHint.output.cs b/test/Dapper.AOT.Test/Interceptors/RowCountHint.output.cs
index 722d94b1..804044a1 100644
--- a/test/Dapper.AOT.Test/Interceptors/RowCountHint.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/RowCountHint.output.cs
@@ -152,9 +152,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
@@ -187,9 +186,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
                 p = cmd.CreateParameter();
                 p.ParameterName = "Bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(args.Bar);
+                SetValueWithDefaultSize(p, args.Bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/RowCountHint.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/RowCountHint.output.netfx.cs
index 722d94b1..804044a1 100644
--- a/test/Dapper.AOT.Test/Interceptors/RowCountHint.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/RowCountHint.output.netfx.cs
@@ -152,9 +152,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
@@ -187,9 +186,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, global:
                 p = cmd.CreateParameter();
                 p.ParameterName = "Bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(args.Bar);
+                SetValueWithDefaultSize(p, args.Bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/Single.output.cs b/test/Dapper.AOT.Test/Interceptors/Single.output.cs
index 99525a95..13dd2d3f 100644
--- a/test/Dapper.AOT.Test/Interceptors/Single.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/Single.output.cs
@@ -222,9 +222,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/Single.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/Single.output.netfx.cs
index 99525a95..13dd2d3f 100644
--- a/test/Dapper.AOT.Test/Interceptors/Single.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/Single.output.netfx.cs
@@ -222,9 +222,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "bar";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.bar);
+                SetValueWithDefaultSize(p, typed.bar);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/TsqlTips.output.cs b/test/Dapper.AOT.Test/Interceptors/TsqlTips.output.cs
index 22782b8a..585b99fe 100644
--- a/test/Dapper.AOT.Test/Interceptors/TsqlTips.output.cs
+++ b/test/Dapper.AOT.Test/Interceptors/TsqlTips.output.cs
@@ -261,9 +261,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "b";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.b);
+                SetValueWithDefaultSize(p, typed.b);
                 ps.Add(p);
 
             }
diff --git a/test/Dapper.AOT.Test/Interceptors/TsqlTips.output.netfx.cs b/test/Dapper.AOT.Test/Interceptors/TsqlTips.output.netfx.cs
index 22782b8a..585b99fe 100644
--- a/test/Dapper.AOT.Test/Interceptors/TsqlTips.output.netfx.cs
+++ b/test/Dapper.AOT.Test/Interceptors/TsqlTips.output.netfx.cs
@@ -261,9 +261,8 @@ public override void AddParameters(in global::Dapper.UnifiedCommand cmd, object?
                 p = cmd.CreateParameter();
                 p.ParameterName = "b";
                 p.DbType = global::System.Data.DbType.String;
-                p.Size = -1;
                 p.Direction = global::System.Data.ParameterDirection.Input;
-                p.Value = AsValue(typed.b);
+                SetValueWithDefaultSize(p, typed.b);
                 ps.Add(p);
 
             }