-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutoMapperHelper.cs
More file actions
61 lines (56 loc) · 2.38 KB
/
AutoMapperHelper.cs
File metadata and controls
61 lines (56 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using AutoMapper;
namespace FluentMetadata.AutoMapper
{
internal class AutoMapperHelper
{
/// <summary>
/// Gets the mapped members for a source/destination type pair.
/// </summary>
/// <param name="source">The source Type.</param>
/// <param name="destination">The destination Type.</param>
/// <returns></returns>
internal static IEnumerable<MemberMap> GetMemberMapsOf(Type source, Type destination)
{
var maps = new Collection<MemberMap>();
foreach (var propertyMap in GetRelevantMappedMembersOf(source, destination))
{
// get the source property of the PropertyMap
var sourceValueResolvers = propertyMap.GetSourceValueResolvers()
// just plain property maps, there's no interesting metadata on a custom mapping function
.OfType<IMemberGetter>();
var sourceProperty = sourceValueResolvers
// "There can be only one"
.SingleOrDefault(svr => svr.MemberType == propertyMap.DestinationProperty.MemberType);
if (sourceProperty != null)
{
maps.Add(new MemberMap
{
SourceName = sourceValueResolvers.Aggregate(string.Empty, (result, svr) => result + svr.Name),
DestinationName = propertyMap.DestinationProperty.Name
});
}
}
return maps;
}
/// <summary>
/// Gets the mapped members for a source/destination type pair
/// leaving out mapped members that are irrelevant.
/// </summary>
/// <param name="source">The source type.</param>
/// <param name="destination">The destination type.</param>
/// <returns></returns>
static IEnumerable<PropertyMap> GetRelevantMappedMembersOf(Type source, Type destination)
{
var typeMap = Mapper.FindTypeMapFor(source, destination);
return
typeMap != null ?
// filter by non-ignored PropertyMaps
typeMap.GetPropertyMaps().Where(m => m.IsIgnored() == false) :
Enumerable.Empty<PropertyMap>();
}
}
}