Skip to content

Commit

Permalink
fix #68 - Mapster modifies underlying collection which is being itera…
Browse files Browse the repository at this point in the history
…ted over, when TypeAdapterConfig.GlobalSettings.Compile() is called
  • Loading branch information
chaowlert committed Jul 4, 2016
1 parent 0ac3cfa commit f5b311e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/Mapster.Tests/Mapster.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Compile Include="Classes\Product.cs" />
<Compile Include="Classes\TypeTestClass.cs" />
<Compile Include="WhenCloningConfig.cs" />
<Compile Include="WhenCompilingConfig.cs" />
<Compile Include="WhenCreatingConfigInstance.cs" />
<Compile Include="WhenMappingWithDictionary.cs" />
<Compile Include="WhenMappingRecordTypes.cs" />
Expand Down
52 changes: 52 additions & 0 deletions src/Mapster.Tests/WhenCompilingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Mapster.Tests
{
[TestFixture]
public class WhenCompilingConfig
{
[TearDown]
public void TearDown()
{
TypeAdapterConfig.GlobalSettings.Clear();
}

[Test]
public void Compile_Success_When_Contain_Collection()
{
TypeAdapterConfig<SrcItem, DestItem>.ForType();

//Validate globally
TypeAdapterConfig<MainSrc, MainDest>.ForType()
.Map(d => d.DestItems, s => s.SrcItems);

TypeAdapterConfig.GlobalSettings.Compile();
}

class MainSrc
{
public int SrcId { get; set; }
public List<SrcItem> SrcItems { get; set; }
}
class MainDest
{
public int DestId { get; set; }
public List<DestItem> DestItems { get; set; }
}
class SrcItem
{
public int ItemId { get; set; }
public string StringData { get; set; }
}
class DestItem
{
public int ItemId { get; set; }
public string StringData { get; set; }
}
}
}
6 changes: 0 additions & 6 deletions src/Mapster/Mapster.nuget.targets

This file was deleted.

12 changes: 7 additions & 5 deletions src/Mapster/TypeAdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,11 @@ orderby priority.Value descending

public void Compile()
{
foreach (var kvp in RuleMap)
var keys = RuleMap.Keys.ToList();
foreach (var key in keys)
{
_mapDict[kvp.Key] = CreateMapFunction(kvp.Key);
_mapToTargetDict[kvp.Key] = CreateMapToTargetFunction(kvp.Key);
_mapDict[key] = CreateMapFunction(key);
_mapToTargetDict[key] = CreateMapToTargetFunction(key);
}
}

Expand All @@ -415,9 +416,10 @@ public void Compile(Type sourceType, Type destinationType)

public void CompileProjection()
{
foreach (var kvp in RuleMap)
var keys = RuleMap.Keys.ToList();
foreach (var key in keys)
{
_projectionDict[kvp.Key] = CreateProjectionCallExpression(kvp.Key);
_projectionDict[key] = CreateProjectionCallExpression(key);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"projects": [ ],
"sdk": {
"version": "1.0.0-preview1-002702"
"version": "1.0.0-preview2-003121"
}
}
12 changes: 6 additions & 6 deletions src/Mapster/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.4.1",
"version": "2.4.2",
"description": "A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, just simpler and way, way faster.",
"packOptions": {
"summary": "A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, but simpler and way faster.",
Expand All @@ -22,11 +22,11 @@
"net45": { },
"netstandard1.3": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-rc2-24027",
"NETStandard.Library": "1.5.0-rc2-24027",
"System.Collections.NonGeneric": "4.0.1-rc2-24027",
"System.Linq.Queryable": "4.0.1-rc2-24027",
"System.Reflection.TypeExtensions": "4.1.0-rc2-24027"
"Microsoft.CSharp": "4.0.1",
"NETStandard.Library": "1.6.0",
"System.Collections.NonGeneric": "4.0.1",
"System.Linq.Queryable": "4.0.1",
"System.Reflection.TypeExtensions": "4.1.0"
}
}
}
Expand Down

0 comments on commit f5b311e

Please sign in to comment.