Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/jo-goro/Dapper into jo-goro…
Browse files Browse the repository at this point in the history
…-main
  • Loading branch information
mgravell committed Aug 16, 2023
2 parents 19355d5 + 0bdc2ca commit 3f8eb8b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Dapper/DefaultTypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public ConstructorInfo FindConstructor(string[] names, Type[] types)
int i = 0;
for (; i < ctorParameters.Length; i++)
{
if (!string.Equals(ctorParameters[i].Name, names[i], StringComparison.OrdinalIgnoreCase))
var denseName = MatchConstructorParametersWithUnderscores ? names[i].Replace("_", "") : names[i];
if (!string.Equals(ctorParameters[i].Name, denseName, StringComparison.OrdinalIgnoreCase))
break;
if (types[i] == typeof(byte[]) && ctorParameters[i].ParameterType.FullName == SqlMapper.LinqBinary)
continue;
Expand Down Expand Up @@ -120,8 +121,9 @@ public ConstructorInfo FindExplicitConstructor()
public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)
{
var parameters = constructor.GetParameters();
var denseColumnName = MatchConstructorParametersWithUnderscores ? columnName.Replace("_", "") : columnName;

return new SimpleMemberMap(columnName, parameters.FirstOrDefault(p => string.Equals(p.Name, columnName, StringComparison.OrdinalIgnoreCase)));
return new SimpleMemberMap(columnName, parameters.FirstOrDefault(p => string.Equals(p.Name, denseColumnName, StringComparison.OrdinalIgnoreCase)));
}

/// <summary>
Expand All @@ -134,7 +136,7 @@ public SqlMapper.IMemberMap GetMember(string columnName)
var property = Properties.Find(p => string.Equals(p.Name, columnName, StringComparison.Ordinal))
?? Properties.Find(p => string.Equals(p.Name, columnName, StringComparison.OrdinalIgnoreCase));

if (property == null && MatchNamesWithUnderscores)
if (property == null && MatchFieldsAndPropertiesWithUnderscores)
{
property = Properties.Find(p => string.Equals(p.Name, columnName.Replace("_", ""), StringComparison.Ordinal))
?? Properties.Find(p => string.Equals(p.Name, columnName.Replace("_", ""), StringComparison.OrdinalIgnoreCase));
Expand All @@ -153,7 +155,7 @@ public SqlMapper.IMemberMap GetMember(string columnName)
?? _fields.Find(p => string.Equals(p.Name, columnName, StringComparison.OrdinalIgnoreCase))
?? _fields.Find(p => string.Equals(p.Name, backingFieldName, StringComparison.OrdinalIgnoreCase));

if (field == null && MatchNamesWithUnderscores)
if (field == null && MatchFieldsAndPropertiesWithUnderscores)
{
var effectiveColumnName = columnName.Replace("_", "");
backingFieldName = "<" + effectiveColumnName + ">k__BackingField";
Expand All @@ -172,7 +174,22 @@ public SqlMapper.IMemberMap GetMember(string columnName)
/// <summary>
/// Should column names like User_Id be allowed to match properties/fields like UserId ?
/// </summary>
public static bool MatchNamesWithUnderscores { get; set; }
[Obsolete("Use MatchFieldsAndPropertiesWithUnderscores for clarity")]
public static bool MatchNamesWithUnderscores
{
get { return MatchFieldsAndPropertiesWithUnderscores; }
set { MatchFieldsAndPropertiesWithUnderscores = value; }
}

/// <summary>
/// Should column names like User_Id be allowed to match properties/fields like UserId ?
/// </summary>
public static bool MatchFieldsAndPropertiesWithUnderscores { get; set; }

/// <summary>
/// Should column names like User_Id be allowed to match constructor arguments like userId ?
/// </summary>
public static bool MatchConstructorParametersWithUnderscores { get; set; }

/// <summary>
/// The settable properties for this typemap
Expand Down
22 changes: 22 additions & 0 deletions tests/Dapper.Tests/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,5 +1273,27 @@ private class HazGetOnly
public int Id { get; }
public string Name { get; } = "abc";
}

[Fact]
public void TestConstructorParametersWithUnderscoredColumns()
{
DefaultTypeMap.MatchConstructorParametersWithUnderscores = true;
DefaultTypeMap.MatchFieldsAndPropertiesWithUnderscores = true;
var obj = connection.QuerySingle<HazGetOnlyAndCtor>("select 42 as [id_property], 'def' as [name_property];");
Assert.Equal(42, obj.IdProperty);
Assert.Equal("def", obj.NameProperty);
}

private class HazGetOnlyAndCtor
{
public int IdProperty { get; }
public string NameProperty { get; }

public HazGetOnlyAndCtor(int idProperty, string nameProperty)
{
IdProperty = idProperty;
NameProperty = nameProperty;
}
}
}
}

0 comments on commit 3f8eb8b

Please sign in to comment.