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

Bug fix underscore naming convention #312

Open
wants to merge 2 commits into
base: master
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
1 change: 1 addition & 0 deletions DapperExtensions.Test/DapperExtensions.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="ReportGenerator" Version="4.8.8" />
<PackageReference Include="Slapper.AutoMapper" Version="2.0.1" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="Dapper" Version="2.0.90" />
</ItemGroup>
Expand Down
23 changes: 22 additions & 1 deletion DapperExtensions.Test/Mapper/ClassMapperFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -319,6 +320,19 @@ public void DoesNotMapPropertyWhenCanMapIsFalse()
}
}

[TestFixture]
public class UnderscoreColumnsTest: ClassMapperFixtureBase
{
[Test]
public void ValidateUnderScoreProperties()
{
var mapper = new TestMapper<FooUnderscore>();
mapper.TestProtected().RunMethod("AutoMap");
Assert.AreEqual(3, mapper.Properties.Count);
}

}

[TestFixture]
public class ReferenceMapTests : ClassMapperFixtureBase
{
Expand Down Expand Up @@ -398,7 +412,14 @@ public class Bar
public long BarId { get; set; }
public string Name { get; set; }
}

[ExcludeFromCodeCoverage]
public class FooUnderscore
{
public long FooId { get; set; }
[Column("first_name")]
public string FirstName { get; set; }
public string LastName { get; set; }
}
[ExcludeFromCodeCoverage]
public class TestMapper<T> : ClassMapper<T> where T : class
{
Expand Down
1 change: 1 addition & 0 deletions DapperExtensions/DapperExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<PackageReference Include="Newtonsoft.Json" version="13.0.1" />
<PackageReference Include="Dapper" Version="2.0.90" />
<PackageReference Include="Slapper.AutoMapper" Version="2.0.1" targetFramework="$(TargetFramework)" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.Data.Common" Version="4.3.0" />
</ItemGroup>

Expand Down
30 changes: 28 additions & 2 deletions DapperExtensions/Mapper/ClassMapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -114,7 +115,7 @@ protected virtual void AutoMap(Func<Type, PropertyInfo, bool> canMap)
{
continue;
}

this.MapUnderScoreColumns();
var map = Map(propertyInfo);
if (!hasDefinedKey)
{
Expand All @@ -134,7 +135,23 @@ protected virtual void AutoMap(Func<Type, PropertyInfo, bool> canMap)
? PropertyTypeKeyTypeMapping[keyMap.MemberType]
: KeyType.Assigned);
}

/// <summary>
/// Map Underscore columns i.e. first_name using columns attribute
/// [Column("first_name")]
/// Public FirstName {get;set;}
/// </summary>
private void MapUnderScoreColumns()
{
#region //Handling under score columns properties using Columns Attribute while Insert, Update & Delete Operations
foreach (MemberMap mp in this.Properties)
{
ColumnAttribute columnAttribute = mp.MemberInfo.GetCustomAttribute<ColumnAttribute>();
var propertyMap = mp as MemberMap;
if (propertyMap != null && columnAttribute != null)
propertyMap.Column(columnAttribute.Name);
}
#endregion
}
protected virtual IReferenceMap<T> ReferenceMap(Expression<Func<T, object>> expression)
{
var propertyInfo = ReflectionHelper.GetProperty(expression) as PropertyInfo;
Expand Down Expand Up @@ -173,6 +190,7 @@ protected virtual MemberMap Map(Expression<Func<T, object>> expression)
protected virtual MemberMap Map(PropertyInfo propertyInfo, MemberMap parent = null)
{
var result = new MemberMap(propertyInfo, this, parent: parent);

if (GuardForDuplicatePropertyMap(result))
{
result = (MemberMap)Properties.FirstOrDefault(p => p.Name.Equals(result.Name) && p.ParentProperty == result.ParentProperty);
Expand All @@ -181,6 +199,14 @@ protected virtual MemberMap Map(PropertyInfo propertyInfo, MemberMap parent = nu
{
Properties.Add(result);
}

//foreach (MemberMap mp in this.Properties)
//{
// System.ComponentModel.DataAnnotations.Schema.ColumnAttribute columnAttribute = mp.MemberInfo.GetCustomAttribute<System.ComponentModel.DataAnnotations.Schema.ColumnAttribute>();
// var propertyMap = mp as MemberMap;
// if (propertyMap != null && columnAttribute != null)
// propertyMap.Column(columnAttribute.Name);
//}
return result;
}

Expand Down