Skip to content

Commit a820fb8

Browse files
authored
Changed Map() method so it will accept a second call to Map() for any property type, not just for properties where IsClass == true. This allows us to Map().Ignore() properties that the AutoClassMapper has mapped automatically, such as lists. (#268)
1 parent 2989ad7 commit a820fb8

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

DapperExtensions.Test/Mapper/AutoClassMapperFixture.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DapperExtensions.Mapper;
22
using NUnit.Framework;
33
using System;
4+
using System.Collections.Generic;
45
using System.Diagnostics.CodeAnalysis;
56
using System.Linq;
67

@@ -10,6 +11,44 @@ namespace DapperExtensions.Test.Mapper
1011
[Parallelizable(ParallelScope.All)]
1112
public static class AutoClassMapperFixture
1213
{
14+
[TestFixture]
15+
public class IListTests
16+
{
17+
[ExcludeFromCodeCoverage]
18+
public class Foo
19+
{
20+
public IList<Bar> Bars { get; set; }
21+
}
22+
23+
[ExcludeFromCodeCoverage]
24+
public class Bar
25+
{
26+
public string Name { get; set; }
27+
}
28+
29+
public class FooClassMapper : AutoClassMapper<Foo>
30+
{
31+
public FooClassMapper()
32+
: base()
33+
{
34+
Map(f => f.Bars).Ignore();
35+
}
36+
}
37+
38+
private static bool MappingIsIgnored(FooClassMapper mapper)
39+
{
40+
return mapper.Properties.Any(w => w.Name == "Bars" && w.Ignored);
41+
}
42+
43+
[Test]
44+
public void IListIsIgnored()
45+
{
46+
var target = new FooClassMapper();
47+
48+
Assert.IsTrue(MappingIsIgnored(target));
49+
}
50+
}
51+
1352
[TestFixture]
1453
public class AutoClassMapperTableName
1554
{

DapperExtensions/Mapper/ClassMapper.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,7 @@ protected virtual MemberMap Map(PropertyInfo propertyInfo, MemberMap parent = nu
175175
var result = new MemberMap(propertyInfo, this, parent: parent);
176176
if (GuardForDuplicatePropertyMap(result))
177177
{
178-
if (propertyInfo.PropertyType.IsClass)
179-
{
180-
result = (MemberMap)Properties.FirstOrDefault(p => p.Name.Equals(result.Name) && p.ParentProperty == result.ParentProperty);
181-
}
182-
else
183-
{
184-
throw new ApplicationException("Unable to UnMap because mapping does not exist.");
185-
}
178+
result = (MemberMap)Properties.FirstOrDefault(p => p.Name.Equals(result.Name) && p.ParentProperty == result.ParentProperty);
186179
}
187180
else
188181
{

0 commit comments

Comments
 (0)