Skip to content

Commit 7e5219f

Browse files
Merge pull request #105 from Tasteful/valuetype
Support for value types
2 parents f8f6cbe + 959754d commit 7e5219f

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using AutoMapper.EquivalencyExpression;
7+
using Xunit;
8+
9+
namespace AutoMapper.Collection
10+
{
11+
public class ValueTypeTests
12+
{
13+
[Fact]
14+
public void MapValueTypes()
15+
{
16+
Mapper.Reset();
17+
Mapper.Initialize(c =>
18+
{
19+
c.AddCollectionMappers();
20+
21+
c.CreateMap<Country, CountryDto>()
22+
.ForMember(x => x.Nationalities, m => m.MapFrom(x => x.Persons))
23+
.ReverseMap();
24+
25+
c.CreateMap<int, PersonNationality>()
26+
.EqualityComparison((src, dest) => dest.NationalityCountryId == src);
27+
});
28+
29+
var persons = new[]
30+
{
31+
new PersonNationality{PersonId = 1, NationalityCountryId = 101},
32+
new PersonNationality{PersonId = 2, NationalityCountryId = 102},
33+
new PersonNationality{PersonId = 3, NationalityCountryId = 103},
34+
new PersonNationality{PersonId = 4, NationalityCountryId = 104},
35+
};
36+
37+
var country = new Country { Persons = new List<PersonNationality>(persons) };
38+
var countryDto = new CountryDto { Nationalities = new List<int> { 104, 103, 105 } };
39+
40+
Mapper.Map(countryDto, country);
41+
42+
Assert.NotStrictEqual(new[] { persons[3], persons[2], country.Persons.Last() }, country.Persons);
43+
Assert.Equal(0, country.Persons.Last().PersonId);
44+
}
45+
46+
public class PersonNationality
47+
{
48+
public int PersonId { get; set; }
49+
public int NationalityCountryId { get; set; }
50+
}
51+
52+
public class Country
53+
{
54+
public IList<PersonNationality> Persons { get; set; }
55+
}
56+
57+
public class CountryDto
58+
{
59+
public IList<int> Nationalities { get; set; }
60+
}
61+
}
62+
}

src/AutoMapper.Collection/EquivalencyExpression/EquivalentExpression.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ public bool IsEquivalent(object source, object destination)
2525
}
2626
}
2727

28-
internal class EquivalentExpression<TSource,TDestination> : IEquivalentComparer<TSource, TDestination>
29-
where TSource : class
30-
where TDestination : class
28+
internal class EquivalentExpression<TSource, TDestination> : IEquivalentComparer<TSource, TDestination>
3129
{
3230
private readonly Expression<Func<TSource, TDestination, bool>> _equivalentExpression;
3331
private readonly Func<TSource, TDestination, bool> _equivalentFunc;
3432
private readonly Func<TSource, int> _sourceHashCodeFunc;
3533
private readonly Func<TDestination, int> _destinationHashCodeFunc;
3634

37-
public EquivalentExpression(Expression<Func<TSource,TDestination,bool>> equivalentExpression)
35+
public EquivalentExpression(Expression<Func<TSource, TDestination, bool>> equivalentExpression)
3836
{
3937
_equivalentExpression = equivalentExpression;
4038
_equivalentFunc = _equivalentExpression.Compile();
@@ -43,22 +41,25 @@ public EquivalentExpression(Expression<Func<TSource,TDestination,bool>> equivale
4341
var destinationParameter = equivalentExpression.Parameters[1];
4442

4543
var members = HashableExpressionsVisitor.Expand(sourceParameter, destinationParameter, equivalentExpression);
46-
44+
4745
_sourceHashCodeFunc = members.Item1.GetHashCodeExpression<TSource>(sourceParameter).Compile();
4846
_destinationHashCodeFunc = members.Item2.GetHashCodeExpression<TDestination>(destinationParameter).Compile();
4947
}
5048

5149
public bool IsEquivalent(object source, object destination)
5250
{
53-
var src = source as TSource;
54-
var dest = destination as TDestination;
5551

56-
if (src == null && dest == null)
52+
if (source == null && destination == null)
5753
{
5854
return true;
5955
}
6056

61-
if (src == null || dest == null)
57+
if (source == null || destination == null)
58+
{
59+
return false;
60+
}
61+
62+
if (!(source is TSource src) || !(destination is TDestination dest))
6263
{
6364
return false;
6465
}
@@ -77,10 +78,10 @@ public Expression<Func<TDestination, bool>> ToSingleSourceExpression(TSource sou
7778

7879
public int GetHashCode(object obj)
7980
{
80-
if (obj is TSource)
81-
return _sourceHashCodeFunc(obj as TSource);
82-
if (obj is TDestination)
83-
return _destinationHashCodeFunc(obj as TDestination);
81+
if (obj is TSource src)
82+
return _sourceHashCodeFunc(src);
83+
if (obj is TDestination dest)
84+
return _destinationHashCodeFunc(dest);
8485
return default(int);
8586
}
8687
}

src/AutoMapper.Collection/EquivalencyExpression/EquivalentExpressions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ internal static IEquivalentComparer GetEquivalentExpression(IConfigurationProvid
104104
/// <param name="EquivalentExpression">Equivalent Expression between <typeparamref name="TSource"/> and <typeparamref name="TDestination"/></param>
105105
/// <returns></returns>
106106
public static IMappingExpression<TSource, TDestination> EqualityComparison<TSource, TDestination>(this IMappingExpression<TSource, TDestination> mappingExpression, Expression<Func<TSource, TDestination, bool>> EquivalentExpression)
107-
where TSource : class
108-
where TDestination : class
109107
{
110108
var typePair = new TypePair(typeof(TSource), typeof(TDestination));
111109

src/AutoMapper.Collection/Mappers/EquivalentExpressionAddRemoveCollectionMapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class EquivalentExpressionAddRemoveCollectionMapper : IConfigurationObjec
1616

1717
public static TDestination Map<TSource, TSourceItem, TDestination, TDestinationItem>(TSource source, TDestination destination, ResolutionContext context, IEquivalentComparer equivalentComparer)
1818
where TSource : IEnumerable<TSourceItem>
19-
where TDestination : class, ICollection<TDestinationItem>
19+
where TDestination : ICollection<TDestinationItem>
2020
{
2121
if (source == null || destination == null)
2222
{

0 commit comments

Comments
 (0)