Skip to content

Commit 84cf4b2

Browse files
committed
Enabling mapping from interfaces, re: issue #11
1 parent 37e4eb8 commit 84cf4b2

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

AgileMapper.UnitTests/AgileMapper.UnitTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<Compile Include="Dictionaries\Configuration\WhenConfiguringNestedDictionaryMapping.cs" />
8282
<Compile Include="Dictionaries\Configuration\WhenConfiguringTargetDictionaryMapping.cs" />
8383
<Compile Include="Dictionaries\WhenCreatingRootDictionaryMembers.cs" />
84+
<Compile Include="TestClasses\IPublicInterface.cs" />
8485
<Compile Include="TestClasses\MegaProduct.cs" />
8586
<Compile Include="TestClasses\ProductDto.cs" />
8687
<Compile Include="TestClasses\ProductDtoMega.cs" />
@@ -115,6 +116,7 @@
115116
<Compile Include="TestClasses\PaymentTypeUk.cs" />
116117
<Compile Include="TestClasses\PaymentTypeUs.cs" />
117118
<Compile Include="TestClasses\PublicFactoryMethod.cs" />
119+
<Compile Include="TestClasses\PublicImplementation.cs" />
118120
<Compile Include="TestClasses\PublicSealed.cs" />
119121
<Compile Include="TestClasses\PublicTwoFields.cs" />
120122
<Compile Include="TestClasses\PublicTwoParamCtor.cs" />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace AgileObjects.AgileMapper.UnitTests.TestClasses
2+
{
3+
public interface IPublicInterface<T>
4+
{
5+
T Value { get; set; }
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace AgileObjects.AgileMapper.UnitTests.TestClasses
2+
{
3+
public class PublicImplementation<T> : IPublicInterface<T>
4+
{
5+
public T Value { get; set; }
6+
}
7+
}

AgileMapper.UnitTests/WhenMappingToNewComplexTypes.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,19 @@ public void ShouldMapFromASimpleTypeToObject()
7171

7272
result.Value.ShouldBe("Oi 'Arold!");
7373
}
74+
75+
// See https://github.com/agileobjects/AgileMapper/issues/11
76+
[Fact]
77+
public void ShouldMapFromAnInterface()
78+
{
79+
IPublicInterface<string> source = new PublicImplementation<string>
80+
{
81+
Value = "Interfaces!"
82+
};
83+
84+
var result = Mapper.Map(source).ToANew<PublicField<string>>();
85+
86+
result.Value.ShouldBe("Interfaces!");
87+
}
7488
}
7589
}

AgileMapper/Configuration/DerivedTypePairSet.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ private static bool SkipDerivedTypePairsLookup(
239239

240240
private static Type GetRootType(Type type)
241241
{
242+
if (type.IsInterface)
243+
{
244+
return type;
245+
}
246+
242247
var parentType = type;
243248

244249
while (parentType != typeof(object))

0 commit comments

Comments
 (0)