Skip to content

Commit 0189c1e

Browse files
authored
Merge pull request #3337 from timmi-on-rails/issue-3129
Attribute-based reverse mapping with SourceMemberAttribute
2 parents bb1c612 + 1ea048a commit 0189c1e

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/AutoMapper/Configuration/MapperConfigurationExpression.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,20 @@ private void AddMapsCore(IEnumerable<Assembly> assembliesToScan)
8787
{
8888
AddProfile(type.AsType());
8989
}
90+
9091
foreach (var autoMapAttribute in type.GetCustomAttributes<AutoMapAttribute>())
9192
{
9293
var mappingExpression = (MappingExpression) autoMapAttributeProfile.CreateMap(autoMapAttribute.SourceType, type);
93-
autoMapAttribute.ApplyConfiguration(mappingExpression);
94-
94+
9595
foreach (var memberInfo in type.GetMembers(BindingFlags.Public | BindingFlags.Instance))
9696
{
9797
foreach (var memberConfigurationProvider in memberInfo.GetCustomAttributes().OfType<IMemberConfigurationProvider>())
9898
{
9999
mappingExpression.ForMember(memberInfo, cfg => memberConfigurationProvider.ApplyConfiguration(cfg));
100100
}
101101
}
102+
103+
autoMapAttribute.ApplyConfiguration(mappingExpression);
102104
}
103105
}
104106

src/UnitTests/AttributeBasedMaps.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,82 @@ public void Should_validate_successfully()
7777
}
7878
}
7979

80+
public class When_specifying_reverse_map_with_sourcemember_attribute : NonValidatingSpecBase
81+
{
82+
public class Source
83+
{
84+
public int Value { get; set; }
85+
}
86+
87+
[AutoMap(typeof(Source), ReverseMap = true)]
88+
public class Destination
89+
{
90+
[SourceMember("Value")]
91+
public int Value2 { get; set; }
92+
}
93+
94+
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
95+
{
96+
cfg.AddMaps(typeof(When_specifying_reverse_map_with_sourcemember_attribute));
97+
});
98+
99+
[Fact]
100+
public void Should_reverse_map()
101+
{
102+
var destination = new Destination { Value2 = 5 };
103+
var source = Mapper.Map<Source>(destination);
104+
source.Value.ShouldBe(5);
105+
}
106+
107+
[Fact]
108+
public void Should_validate_successfully()
109+
{
110+
typeof(AutoMapperConfigurationException).ShouldNotBeThrownBy(() => Configuration.AssertConfigurationIsValid(Configuration.FindTypeMapFor<Source, Destination>()));
111+
typeof(AutoMapperConfigurationException).ShouldNotBeThrownBy(() => Configuration.AssertConfigurationIsValid(Configuration.FindTypeMapFor<Destination, Source>()));
112+
}
113+
}
114+
115+
public class When_specifying_generic_reverse_map_with_sourcemember_attribute : NonValidatingSpecBase
116+
{
117+
public class Source<T>
118+
{
119+
public T Value { get; set; }
120+
121+
public string StringValue { get; set; }
122+
}
123+
124+
[AutoMap(typeof(Source<>), ReverseMap = true)]
125+
public class Destination<T>
126+
{
127+
[SourceMember("Value")]
128+
public int Value2 { get; set; }
129+
130+
[SourceMember("StringValue")]
131+
public string StringValue2 { get; set; }
132+
}
133+
134+
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
135+
{
136+
cfg.AddMaps(typeof(When_specifying_generic_reverse_map_with_sourcemember_attribute));
137+
});
138+
139+
[Fact]
140+
public void Should_reverse_map()
141+
{
142+
Destination<int> destination = new Destination<int> { Value2 = 5, StringValue2 = "Joe" };
143+
Source<int> source = Mapper.Map<Source<int>>(destination);
144+
source.Value.ShouldBe(5);
145+
source.StringValue.ShouldBe("Joe");
146+
}
147+
148+
[Fact]
149+
public void Should_validate_successfully()
150+
{
151+
typeof(AutoMapperConfigurationException).ShouldNotBeThrownBy(() => Configuration.AssertConfigurationIsValid(Configuration.FindTypeMapFor(typeof(Source<>), typeof(Destination<>))));
152+
typeof(AutoMapperConfigurationException).ShouldNotBeThrownBy(() => Configuration.AssertConfigurationIsValid(Configuration.FindTypeMapFor(typeof(Destination<>), typeof(Source<>))));
153+
}
154+
}
155+
80156
public class When_duplicating_map_configuration_with_code_and_attribute : NonValidatingSpecBase
81157
{
82158
public class Source

0 commit comments

Comments
 (0)