From 629248d0f8616ce5ade8988811512a20bdb034e7 Mon Sep 17 00:00:00 2001 From: Adam Renaud Date: Mon, 6 Dec 2021 20:37:35 -0500 Subject: [PATCH] Added multi mapping to the mapper --- src/Mapr/IMapper.cs | 13 ++++++++++++- src/Mapr/Mapper.cs | 17 ++++++++++++++++- tests/Mapr.Tests/MapperTests.cs | 22 +++++++++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Mapr/IMapper.cs b/src/Mapr/IMapper.cs index 3240f00..aa1b6c6 100644 --- a/src/Mapr/IMapper.cs +++ b/src/Mapr/IMapper.cs @@ -1,4 +1,6 @@ -namespace Mapr +using System.Collections.Generic; + +namespace Mapr { /// /// Represents the interface for a Mapper. @@ -14,5 +16,14 @@ public interface IMapper /// The destination object type. /// A instance of that represents a mapped type. public TDestination Map(TSource source); + + /// + /// Map from a collection of to a collection of + /// + /// The source collection. + /// The source type + /// The destination type + /// An of the mapped values. + public IEnumerable Map(IEnumerable source); } } \ No newline at end of file diff --git a/src/Mapr/Mapper.cs b/src/Mapr/Mapper.cs index 6ca5aa2..3e34a95 100644 --- a/src/Mapr/Mapper.cs +++ b/src/Mapr/Mapper.cs @@ -1,4 +1,7 @@ -namespace Mapr +using System.Collections.Generic; +using System.Linq; + +namespace Mapr { /// /// Represents an implementation of @@ -36,5 +39,17 @@ public TDestination Map(TSource source) { return _locator.LocateMapFor().Map(source); } + + /// + /// Maps the provided collection of objects to the provided type . + /// + /// The objects that will be mapped + /// The type of the source objects. + /// The type of the destination objects. + /// A collection of objects mapped. + public IEnumerable Map(IEnumerable source) + { + return source.Select(Map); + } } } \ No newline at end of file diff --git a/tests/Mapr.Tests/MapperTests.cs b/tests/Mapr.Tests/MapperTests.cs index 5141cb1..6304e06 100644 --- a/tests/Mapr.Tests/MapperTests.cs +++ b/tests/Mapr.Tests/MapperTests.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using AutoFixture; using FluentAssertions; using NSubstitute; @@ -34,9 +35,28 @@ public void Map_ShouldNotThrow_WhenPassedNull() var mapper = new Mapper(locator); - Action act = () => mapper.Map(null); + Action act = () => mapper.Map((string)null!); act.Should().NotThrow(); } + + [Fact] + public void Map_WithCollection_ShouldMap_WhenTypeMapExists() + { + var typeMap = Substitute.For>(); + var locator = Substitute.For(); + locator.LocateMapFor().Returns(typeMap); + + var mapper = new Mapper(locator); + + var source = Fixture.CreateMany().ToList(); + var expectedResult = source.Select(s => s.ToString()).ToList(); + + typeMap.Map(Arg.Any()).Returns(c => c.Arg().ToString()); + + var result = mapper.Map(source); + + result.Should().BeEquivalentTo(expectedResult); + } } } \ No newline at end of file