forked from AutoMapper/AutoMapper.Archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfaceMapping.cs
More file actions
770 lines (643 loc) · 19.3 KB
/
Copy pathInterfaceMapping.cs
File metadata and controls
770 lines (643 loc) · 19.3 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
namespace AutoMapper.UnitTests.InterfaceMapping;
public class InterfaceWithObjectProperty : AutoMapperSpecBase
{
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<ISourceModel, IDestModel>());
public interface ISourceModel
{
object Id { get; set; }
}
public interface IDestModel
{
object Id { get; set; }
}
public class SourceModel : ISourceModel
{
public object Id { get; set; }
}
public class DestModel : IDestModel
{
public object Id { get; set; }
}
[Fact]
public void Should_work() => Mapper.Map(new SourceModel { Id = 42 }, new DestModel()).Id.ShouldBe(42);
}
public class InterfaceInheritance : AutoMapperSpecBase
{
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Source, IProperties>()
.IncludeBase<Source, IPropertyA>()
.IncludeBase<Source, IPropertyB>();
cfg.CreateMap<Source, IPropertyA>();
cfg.CreateMap<Source, IPropertyB>();
});
[Fact]
public void Should_choose_the_derived_interface()
{
var source = new Source
{
Name = "Name",
PropertyA = "PropertyA",
PropertyB = "PropertyB"
};
var destination = new Target();
Mapper.Map(source, destination);
destination.Name.ShouldBe(source.Name);
destination.PropertyA.ShouldBe(source.PropertyA);
destination.PropertyB.ShouldBe(source.PropertyB);
}
public class Source
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
public string Name { get; set; }
}
public class Target : IProperties
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
public string Name { get; set; }
}
public interface IProperties : IPropertyA, IPropertyB
{
string Name { get; set; }
}
public interface IPropertyA
{
string PropertyA { get; set; }
}
public interface IPropertyB
{
string PropertyB { get; set; }
}
}
public class MapToInterface : NonValidatingSpecBase
{
protected override MapperConfiguration CreateConfiguration() => new(c=>c.CreateMap<object, IEnumerable<object>>());
[Fact]
public void Should_throw() => new Action(()=>Mapper.Map<IEnumerable<object>>(new object())).ShouldThrow<AutoMapperMappingException>().Message.ShouldStartWith(
"Cannot create interface System.Collections.Generic.IEnumerable`1[System.Object]");
}
public class GenericsAndInterfaces : AutoMapperSpecBase
{
MyClass<ContainerClass> source = new MyClass<ContainerClass> { Container = new ContainerClass { MyProperty = 3 } };
public interface IMyInterface<T>
{
T Container { get; set; }
}
public class ContainerClass
{
public int MyProperty { get; set; }
}
public class ImplementedClass : IMyInterface<ContainerClass>
{
public ContainerClass Container
{
get;
set;
}
}
public class MyClass<T>
{
public T Container { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap(typeof(MyClass<>), typeof(IMyInterface<>)).AsProxy());
[Fact]
public void ShouldMapToExistingObject()
{
var destination = new ImplementedClass();
Mapper.Map(source, destination, typeof(MyClass<ContainerClass>), typeof(IMyInterface<ContainerClass>));
destination.Container.MyProperty.ShouldBe(3);
}
[Fact]
public void ShouldMapToNewObject()
{
var destination = (IMyInterface<ContainerClass>) Mapper.Map(source, typeof(MyClass<ContainerClass>), typeof(IMyInterface<ContainerClass>));
destination.Container.MyProperty.ShouldBe(3);
}
}
public class When_mapping_generic_interface : AutoMapperSpecBase
{
public class Source<T> : List<T>
{
public String PropertyToMap { get; set; }
public String PropertyToIgnore { get; set; } = "I am not ignored";
}
public class Destination : DestinationBase<String>
{
}
public abstract class DestinationBase<T> : DestinationBaseBase, IDestinationBase<T>
{
private String m_PropertyToIgnore;
public virtual String PropertyToMap { get; set; }
[IgnoreMap]
public override String PropertyToIgnore
{
get
{
return m_PropertyToIgnore ?? (m_PropertyToIgnore = "Ignore me");
}
set
{
m_PropertyToIgnore = value;
}
}
public virtual List<T> Items { get; set; }
}
public abstract class DestinationBaseBase
{
[IgnoreMap]
public virtual String PropertyToIgnore { get; set; }
}
public interface IDestinationBase<T>
{
String PropertyToMap { get; set; }
List<T> Items { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg=>
cfg.CreateMap(typeof(IList<>), typeof(IDestinationBase<>))
.ForMember(nameof(IDestinationBase<Object>.Items), p_Expression => p_Expression.MapFrom(p_Source => p_Source))
.ForMember("PropertyToMap", o=>o.Ignore())
.AsProxy());
[Fact]
public void Should_work()
{
var source = new Source<String>{"Cat", "Dog"};
source.PropertyToMap = "Hello World";
var destination = Mapper.Map<IDestinationBase<string>>(source);
destination.PropertyToMap.ShouldBeNull();
destination.Items.ShouldBe(source);
}
}
public class When_mapping_an_interface_with_getter_only_member : AutoMapperSpecBase
{
interface ISource
{
int Id { get; set; }
}
public interface IDestination
{
int Id { get; set; }
int ReadOnly { get; }
}
class Source : ISource
{
public int Id { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(c=>c.CreateMap<ISource, IDestination>().AsProxy());
[Fact]
public void ShouldMapOk()
{
Mapper.Map<IDestination>(new Source { Id = 5 }).Id.ShouldBe(5);
}
}
public class When_mapping_base_interface_members
{
public interface ISource
{
int Id { get; set; }
}
public interface ITarget : ITargetBase
{
int Id { get; set; }
}
public interface ITargetBase
{
int BaseId { get; set; }
}
[Fact]
public void Should_find_inherited_members_by_name()
{
new MapperConfiguration(c=>c.CreateMap<ISource, ITarget>().ForMember("BaseId", opt => opt.Ignore()));
}
}
public class When_mapping_to_existing_object_through_interfaces : AutoMapperSpecBase
{
private class2DTO _result;
public class class1 : iclass1
{
public string prop1 { get; set; }
}
public class class2 : class1, iclass2
{
public string prop2 { get; set; }
}
public class class1DTO : iclass1DTO
{
public string prop1 { get; set; }
}
public class class2DTO : class1DTO, iclass2DTO
{
public string prop2 { get; set; }
}
public interface iclass1
{
string prop1 { get; set; }
}
public interface iclass2
{
string prop2 { get; set; }
}
public interface iclass1DTO
{
string prop1 { get; set; }
}
public interface iclass2DTO
{
string prop2 { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<iclass1, iclass1DTO>();
cfg.CreateMap<iclass2, iclass2DTO>();
});
protected override void Because_of()
{
var bo = new class2 { prop1 = "PROP1", prop2 = "PROP2" };
_result = Mapper.Map(bo, new class2DTO());
}
[Fact]
public void Should_use_the_most_derived_interface()
{
_result.prop2.ShouldBe("PROP2");
}
}
public class When_mapping_an_interface_to_an_abstract_type : AutoMapperSpecBase
{
private DtoObject _result;
public class ModelObject
{
public IChildModelObject Child { get; set; }
}
public interface IChildModelObject
{
string ChildProperty { get; set; }
}
public class SubChildModelObject : IChildModelObject
{
public string ChildProperty { get; set; }
}
public class DtoObject
{
public DtoChildObject Child { get; set; }
}
public abstract class DtoChildObject
{
public virtual string ChildProperty { get; set; }
}
public class SubDtoChildObject : DtoChildObject
{
}
protected override void Because_of()
{
var model = new ModelObject
{
Child = new SubChildModelObject {ChildProperty = "child property value"}
};
_result = Mapper.Map<ModelObject, DtoObject>(model);
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<ModelObject, DtoObject>();
cfg.CreateMap<IChildModelObject, DtoChildObject>()
.Include<SubChildModelObject, SubDtoChildObject>();
cfg.CreateMap<SubChildModelObject, SubDtoChildObject>();
});
[Fact]
public void Should_map_Child_to_SubDtoChildObject_type()
{
_result.Child.ShouldBeOfType(typeof (SubDtoChildObject));
}
[Fact]
public void Should_map_ChildProperty_to_child_property_value()
{
_result.Child.ChildProperty.ShouldBe("child property value");
}
}
public class When_mapping_a_concrete_type_to_an_interface_type : AutoMapperSpecBase
{
private IDestination _result;
public class Source
{
public int Value { get; set; }
public int Value1 { get; set; }
}
public interface IDestination
{
int Value { get; set; }
int Value2 { get; set; }
string Value3 { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>cfg.CreateMap<Source, IDestination>().AsProxy()
.ForMember(x => x.Value2, o => o.MapFrom(x => x.Value1))
.ForMember(x => x.Value3, o => o.Ignore())
.AfterMap((_, d) =>
{
d.Value3 = "value 3";
}));
protected override void Because_of()
{
_result = Mapper.Map<Source, IDestination>(new Source {Value = 5, Value1 = 50});
}
[Fact]
public void Should_create_an_implementation_of_the_interface()
{
_result.Value.ShouldBe(5);
}
[Fact]
public void Should_apply_rules_after_proxying()
{
_result.Value2.ShouldBe(50);
_result.Value3.ShouldBe("value 3");
}
[Fact]
public void Should_not_derive_from_INotifyPropertyChanged()
{
_result.ShouldNotBeOfType<INotifyPropertyChanged>();
}
}
public class When_mapping_an_interface_type_to_a_concrete_type_and_reverse : AutoMapperSpecBase
{
public interface ISource
{
int Value { get; set; }
}
public class Destination
{
public int Value { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
cfg.CreateMap<ISource, Destination>().ReverseMap());
[Fact]
public void Should_not_convert_to_interface()
{
Should.Throw<AutoMapperMappingException>(() => Mapper.Map<Destination, ISource>(new Destination {Value = 5}))
.Message.ShouldStartWith("Cannot create interface " + typeof(ISource).FullName);
}
}
public class When_mapping_an_interface_type_to_an_interface_type_and_reverse : AutoMapperSpecBase
{
public interface ISource
{
int Value { get; set; }
}
public class Source: ISource
{
public int Value { get; set; }
}
public interface IDestination
{
int Value { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
cfg.CreateMap<ISource, IDestination>().AsProxy().ReverseMap().AsProxy());
[Fact]
public void Should_create_an_implementation_of_the_destination_interface()
{
var destination = Mapper.Map<ISource, IDestination>(new Source {Value = 5});
destination.Value.ShouldBe(5);
}
[Fact]
public void Should_map_implementation_of_the_interface_to_the_proxied_implementation()
{
var destination = Mapper.Map<ISource, IDestination>(new Source {Value = 5});
var reversed = Mapper.Map<IDestination, ISource>(destination);
reversed.ShouldNotBeOfType<Source>();
reversed.Value.ShouldBe(5);
}
}
public class When_mapping_a_concrete_type_to_an_interface_type_and_reverse : AutoMapperSpecBase
{
public class Source
{
public int Value { get; set; }
}
public interface IDestination
{
int Value { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
cfg.CreateMap<Source, IDestination>().AsProxy().ReverseMap());
[Fact]
public void Should_create_an_implementation_of_the_destination_interface()
{
var destination = Mapper.Map<Source, IDestination>(new Source {Value = 5});
destination.Value.ShouldBe(5);
}
[Fact]
public void Should_map_implementation_of_the_interface_to_the_class()
{
var destination = Mapper.Map<Source, IDestination>(new Source {Value = 5});
var reversed = Mapper.Map<IDestination, Source>(destination);
reversed.Value.ShouldBe(5);
}
}
public class When_mapping_a_concrete_type_to_an_interface_type_that_derives_from_INotifyPropertyChanged : AutoMapperSpecBase
{
private IDestination _result;
private int _count;
public class Source
{
public int Value { get; set; }
}
public interface IDestination : INotifyPropertyChanged
{
int Value { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg => cfg.CreateMap<Source, IDestination>().AsProxy());
protected override void Because_of()
{
_result = Mapper.Map<Source, IDestination>(new Source {Value = 5});
}
[Fact]
public void Should_create_an_implementation_of_the_interface()
{
_result.Value.ShouldBe(5);
}
[Fact]
public void Should_derive_from_INotifyPropertyChanged()
{
var q = _result as INotifyPropertyChanged;
q.ShouldNotBeNull();
}
[Fact]
public void Should_notify_property_changes()
{
var count = 0;
_result.PropertyChanged += (o, e) => {
count++;
o.ShouldBeSameAs(_result);
e.PropertyName.ShouldBe("Value");
};
_result.Value = 42;
count.ShouldBe(1);
_result.Value.ShouldBe(42);
}
[Fact]
public void Should_detach_event_handler()
{
_result.PropertyChanged += MyHandler;
_count.ShouldBe(0);
_result.Value = 56;
_count.ShouldBe(1);
_result.PropertyChanged -= MyHandler;
_count.ShouldBe(1);
_result.Value = 75;
_count.ShouldBe(1);
}
private void MyHandler(object sender, PropertyChangedEventArgs e) {
_count++;
}
}
public class When_mapping_a_derived_interface_to_an_derived_concrete_type : AutoMapperSpecBase
{
private Destination _result = null;
public interface ISourceBase
{
int Id { get; }
}
public interface ISource : ISourceBase
{
int SecondId { get; }
}
public class Source : ISource
{
public int Id { get; set; }
public int SecondId { get; set; }
}
public abstract class DestinationBase
{
public int Id { get; set; }
}
public class Destination : DestinationBase
{
public int SecondId { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<ISource, Destination>();
});
protected override void Because_of()
{
_result = Mapper.Map<ISource, Destination>(new Source {Id = 7, SecondId = 42});
}
[Fact]
public void Should_map_base_interface_property()
{
_result.Id.ShouldBe(7);
}
[Fact]
public void Should_map_derived_interface_property()
{
_result.SecondId.ShouldBe(42);
}
}
public class When_mapping_a_derived_interface_to_an_derived_concrete_type_with_readonly_interface_members : AutoMapperSpecBase
{
private Destination _result = null;
public interface ISourceBase
{
int Id { get; }
}
public interface ISource : ISourceBase
{
int SecondId { get; }
}
public class Source : ISource
{
public int Id { get; set; }
public int SecondId { get; set; }
}
public interface IDestinationBase
{
int Id { get; }
}
public interface IDestination : IDestinationBase
{
int SecondId { get; }
}
public abstract class DestinationBase : IDestinationBase
{
public int Id { get; set; }
}
public class Destination : DestinationBase, IDestination
{
public int SecondId { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<ISource, Destination>();
});
protected override void Because_of()
{
_result = Mapper.Map<ISource, Destination>(new Source {Id = 7, SecondId = 42});
}
[Fact]
public void Should_map_base_interface_property()
{
_result.Id.ShouldBe(7);
}
[Fact]
public void Should_map_derived_interface_property()
{
_result.SecondId.ShouldBe(42);
}
}
public class When_mapping_to_a_type_with_explicitly_implemented_interface_members : AutoMapperSpecBase
{
private Destination _destination;
public class Source
{
public int Value { get; set; }
}
public interface IOtherDestination
{
int OtherValue { get; set; }
}
public class Destination : IOtherDestination
{
public int Value { get; set; }
int IOtherDestination.OtherValue { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Source, Destination>();
});
protected override void Because_of()
{
_destination = Mapper.Map<Source, Destination>(new Source {Value = 10});
}
[Fact]
public void Should_ignore_interface_members_for_mapping()
{
_destination.Value.ShouldBe(10);
}
}
public class MappingToInterfacesWithPolymorphism : AutoMapperSpecBase
{
private BaseDto[] _baseDtos;
public interface IBase { }
public interface IDerived : IBase { }
public class Base : IBase { }
public class Derived : Base, IDerived { }
public class BaseDto { }
public class DerivedDto : BaseDto { }
//and following mappings:
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Base, BaseDto>().Include<Derived, DerivedDto>();
cfg.CreateMap<Derived, DerivedDto>();
cfg.CreateMap<IBase, BaseDto>().Include<IDerived, DerivedDto>();
cfg.CreateMap<IDerived, DerivedDto>();
});
protected override void Because_of()
{
List<Base> list = new List<Base>() { new Derived() };
_baseDtos = Mapper.Map<IEnumerable<Base>, BaseDto[]>(list);
}
[Fact]
public void Should_use_the_derived_type_map()
{
_baseDtos.First().ShouldBeOfType<DerivedDto>();
}
}