Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds MatchConstructorParametersWithUnderscores #1786

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
}