Skip to content

Commit

Permalink
Added support for MatchNamesWithUnderscores and ctor parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Jan 6, 2020
1 parent 61fa083 commit a3b4a9c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Dapper.Tests/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,5 +1130,26 @@ private class HazGetOnly
public int Id { get; }
public string Name { get; } = "abc";
}

[Fact]
public void GetOnlyPropertiesWithNonDefaultConstructor()
{
DefaultTypeMap.MatchNamesWithUnderscores = 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;
}
}
}
}
15 changes: 13 additions & 2 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))
if ((!MatchNamesWithUnderscores && !string.Equals(ctorParameters[i].Name, names[i], StringComparison.OrdinalIgnoreCase)) ||
(MatchNamesWithUnderscores && !string.Equals(ctorParameters[i].Name, names[i].Replace("_", ""), StringComparison.OrdinalIgnoreCase)))
break;
if (types[i] == typeof(byte[]) && ctorParameters[i].ParameterType.FullName == SqlMapper.LinqBinary)
continue;
Expand Down Expand Up @@ -121,7 +122,17 @@ public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor,
{
var parameters = constructor.GetParameters();

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

This comment has been minimized.

Copy link
@agjini

agjini Jan 7, 2020

Instead ok making 3 FirstOrDefault call (= 3 iterations), you could make only one call.


if (property == null && MatchNamesWithUnderscores)
{
property = parameters.FirstOrDefault(p => string.Equals(p.Name, columnName.Replace("_", ""), StringComparison.Ordinal))

This comment has been minimized.

Copy link
@agjini

agjini Jan 7, 2020

I don't understand why you first test with Ordinal before comparing with ignore case?
Maybe you can use vars to avoid multiple call to replace.

This comment has been minimized.

Copy link
@andreas-antoniou-cko

andreas-antoniou-cko Jan 7, 2020

I copied this code from SqlMapper.IMemberMap GetMember(string columnName) assuming it was some sort of micro-optimization. But I changed it now with your suggestion to remove the 3 FirstOrDefault() calls so it is simplified.

?? parameters.FirstOrDefault(p => string.Equals(p.Name, columnName.Replace("_", ""), StringComparison.OrdinalIgnoreCase));
}

return new SimpleMemberMap(
MatchNamesWithUnderscores ? columnName.Replace("_", "") : columnName,
property);
}

/// <summary>
Expand Down

0 comments on commit a3b4a9c

Please sign in to comment.