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

Add support for DefaultTypeMap.MatchNamesWithUnderscores #171

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Dapper.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
version.json = version.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib", "Dapper.Contrib\Dapper.Contrib.csproj", "{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib", "src\Dapper.Contrib\Dapper.Contrib.csproj", "{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Tests.Contrib", "tests\Dapper.Tests.Contrib\Dapper.Tests.Contrib.csproj", "{DAB3C5B7-BCD1-4A5F-BB6B-50D2BB63DB4A}"
EndProject
Expand Down
10 changes: 10 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ Dapper.Contrib makes use of some optional attributes:
* `[Write(true/false)]` - this property is (not) writeable
* `[Computed]` - this property is computed and should not be part of updates

Support for Snake Casing
-------
When Dapper's `DefaultTypeMap.MatchNamesWithUnderscores` is set to true,
Dapper will match a property named 'EmployeeId' to a
column named 'employee_id'.

(If using this setting, it's generally best to avoid capitalised acronyms in property
names. For example, a prroperty named 'EmployeeID' would be mapped to a column
named 'employee_i_d', which is unlikely to be the desired behavior.)

Limitations and caveats
-------

Expand Down
10 changes: 9 additions & 1 deletion src/Dapper.Contrib/Dapper.Contrib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<NoWarn>$(NoWarn);CA1050</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="Dapper" Version="2.1.35" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
Expand All @@ -19,4 +19,12 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageReference Update="Nerdbank.GitVersioning" Version="3.6.139" />
</ItemGroup>
</Project>
17 changes: 10 additions & 7 deletions src/Dapper.Contrib/SqlMapperExtensions.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ public static partial class SqlMapperExtensions
public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
var type = typeof(T);
if (!GetQueries.TryGetValue(type.TypeHandle, out string sql))
var keyType = new GetQueryCacheKey(type.TypeHandle, DefaultTypeMap.MatchNamesWithUnderscores);
if (!GetQueries.TryGetValue(keyType, out string sql))
{
var key = GetSingleKey<T>(nameof(GetAsync));
var name = GetTableName(type);

sql = $"SELECT * FROM {name} WHERE {key.Name} = @id";
GetQueries[type.TypeHandle] = sql;
sql = $"SELECT * FROM {name} WHERE {ColumnMapping.ColumnName(key.Name)} = @id";
GetQueries[keyType] = sql;
}

var dynParams = new DynamicParameters();
Expand All @@ -49,7 +50,7 @@ public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic i

foreach (var property in TypePropertiesCache(type))
{
var val = res[property.Name];
var val = res[ColumnMapping.ColumnName(property.Name)];
if (val == null) continue;
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Expand Down Expand Up @@ -83,13 +84,15 @@ public static Task<IEnumerable<T>> GetAllAsync<T>(this IDbConnection connection,
var type = typeof(T);
var cacheType = typeof(List<T>);

if (!GetQueries.TryGetValue(cacheType.TypeHandle, out string sql))
var cacheKeyType = new GetQueryCacheKey(cacheType.TypeHandle, DefaultTypeMap.MatchNamesWithUnderscores);

if (!GetQueries.TryGetValue(cacheKeyType, out string sql))
{
GetSingleKey<T>(nameof(GetAll));
var name = GetTableName(type);

sql = "SELECT * FROM " + name;
GetQueries[cacheType.TypeHandle] = sql;
GetQueries[cacheKeyType] = sql;
}

if (!type.IsInterface)
Expand All @@ -108,7 +111,7 @@ private static async Task<IEnumerable<T>> GetAllAsyncImpl<T>(IDbConnection conne
var obj = ProxyGenerator.GetInterfaceProxy<T>();
foreach (var property in TypePropertiesCache(type))
{
var val = res[property.Name];
var val = res[ColumnMapping.ColumnName(property.Name)];
if (val == null) continue;
if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Expand Down
Loading