Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Fix to Map Value Objects #58

Merged
merged 4 commits into from
Aug 9, 2017
Merged
Changes from 3 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
21 changes: 17 additions & 4 deletions src/Dapper.FluentMap/Utils/ReflectionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Linq.Expressions;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace Dapper.FluentMap.Utils
@@ -31,11 +33,22 @@ public static MemberInfo GetMemberInfo(LambdaExpression lambda)
case ExpressionType.MemberAccess:
var memberExpression = (MemberExpression)expr;
var baseMember = memberExpression.Member;
Type paramType;

while (memberExpression != null)
{
paramType = memberExpression.Type;
if (paramType.GetMembers().Any(member => member.Name == baseMember.Name))
{
return paramType.GetMember(baseMember.Name)[0];
}

memberExpression = memberExpression.Expression as MemberExpression;
}

// Make sure we get the property from the derived type.
var paramType = lambda.Parameters[0].Type;
var memberInfo = paramType.GetMember(baseMember.Name)[0];
return memberInfo;
paramType = lambda.Parameters.[0].Type;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameters.[0] should be Parameters[0]

return paramType.GetMember(baseMember.Name)[0];

default:
return null;
3 changes: 3 additions & 0 deletions test/Dapper.FluentMap.Tests/Dapper.FluentMap.Tests.csproj
Original file line number Diff line number Diff line change
@@ -11,4 +11,7 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>
18 changes: 18 additions & 0 deletions test/Dapper.FluentMap.Tests/ManualMappingTests.cs
Original file line number Diff line number Diff line change
@@ -138,6 +138,16 @@ public void PropertyMapShouldMapInheritedProperies()
Assert.Equal(typeof(DerivedTestEntity), nameMap.PropertyInfo.DeclaringType);
}

[Fact]
public void PropertyMapShouldMapValueObjectProperties()
{
PreTest();

var map = new ValueObjectMap();
var email = map.PropertyMaps.First();
Assert.Equal(typeof(EmailTestValueObject), email.PropertyInfo.DeclaringType);
}

private static void PreTest()
{
FluentMapper.EntityMaps.Clear();
@@ -189,5 +199,13 @@ public MapWithDuplicateMapping()
private class EmptyMap : EntityMap<TestEntity>
{
}

private class ValueObjectMap : EntityMap<ValueObjectTestEntity>
{
public ValueObjectMap()
{
Map(x => x.Email.Address).ToColumn("email");
}
}
}
}
10 changes: 10 additions & 0 deletions test/Dapper.FluentMap.Tests/TestEntity.cs
Original file line number Diff line number Diff line change
@@ -9,4 +9,14 @@ public class DerivedTestEntity : TestEntity
{
public string Name { get; set; }
}

public class ValueObjectTestEntity
{
public EmailTestValueObject Email { get; set; }
}

public class EmailTestValueObject
{
public string Address { get; set; }
}
}