public void MapSourceToDestination_NonGenericMetod_TSourceAndSourceObjHaveDifferentTypes() { var mapper = new Mapper(); mapper.CreateConfiguration<MockSource, MockDestination>() .AddMap(s => s.GuidProp, d => d.GuidProp) .AddMap(s => s.StringProp, d => d.StringProp) .AddMap(s => s.IntProp, d => d.IntProp) .AddMap(s => s.DateTimeProp, d => d.DateTimeProp) .AddMap(s => s.DoubleProp, d => d.DoubleProp); var source = new MockSource1() { GuidProp = Guid.NewGuid(), StringProp = "anyString", IntProp = 1234 }; var result = (MockDestination)mapper.Map(typeof(MockSource), typeof(MockDestination), source); }
public void MapSourceWithCollectionAndReferenceProperty_MapperImplemented_AllOk() { var mapper = new Mapper(); mapper.CreateConfiguration<MockSource1, MockDestination1>() .AddMap(s => s.GuidProp, d => d.GuidProp) .AddMap(s => s.StringProp, d => d.StringProp) .AddMap(s => s.IntProp, d => d.IntProp); mapper.CreateConfiguration<MockSource, MockDestination>() .AddMap(s => s.GuidProp, d => d.GuidProp) .AddMap(s => s.StringProp, d => d.StringProp) .AddMap(s => s.IntProp, d => d.IntProp) .AddMap(s => s.DateTimeProp, d => d.DateTimeProp) .AddMap(s => s.DoubleProp, d => d.DoubleProp) .AddMap(s => s.ReferenceProperty, d => d.ReferenceProperty) .AddMap(s => s.CollectionOfReferenceProperties, d => d.CollectionOfReferenceProperties); var sourceRef1 = new MockSource1() { GuidProp = Guid.NewGuid(), StringProp = "anySourceRef1", IntProp = 12 }; var sourceRef2 = new MockSource1() { GuidProp = Guid.NewGuid(), StringProp = "anySourceRef2", IntProp = 23 }; var source = new MockSource() { GuidProp = Guid.NewGuid(), StringProp = "anyString", IntProp = 1234, DateTimeProp = DateTime.Now, DoubleProp = 123.456, ReferenceProperty = new MockSource1() { GuidProp = Guid.NewGuid(), StringProp = "anyRefString", IntProp = 456 }, CollectionOfReferenceProperties = new List<MockSource1>() { sourceRef1, sourceRef2 } }; var result = mapper.Map<MockSource, MockDestination>((new List<MockSource>() { source }).AsQueryable()); var destination = result.FirstOrDefault(d => d.GuidProp == source.GuidProp); Assert.IsNotNull(destination, "destination is null."); Assert.AreEqual(source.StringProp, destination.StringProp, "source StringProp error."); Assert.AreEqual(source.IntProp, destination.IntProp, "source IntProp error."); Assert.AreEqual(source.DateTimeProp, destination.DateTimeProp, "source DateTimeProp error."); Assert.AreEqual(source.DoubleProp, destination.DoubleProp, "source DoubleProp error."); Assert.AreEqual(source.ReferenceProperty.GuidProp, destination.ReferenceProperty.GuidProp, "source ReferenceProperty.GuidProp error."); Assert.AreEqual(source.ReferenceProperty.StringProp, destination.ReferenceProperty.StringProp, "source ReferenceProperty.StringProp error."); Assert.AreEqual(source.ReferenceProperty.IntProp, destination.ReferenceProperty.IntProp, "source ReferenceProperty.IntProp error."); var destinationCollectionElement = destination.CollectionOfReferenceProperties.FirstOrDefault(d => d.GuidProp == sourceRef1.GuidProp); Assert.IsNotNull(destinationCollectionElement, "sourceRef1 destination is null."); Assert.AreEqual(sourceRef1.StringProp, destinationCollectionElement.StringProp, "sourceRef1 ReferenceProperty.StringProp error."); Assert.AreEqual(sourceRef1.IntProp, destinationCollectionElement.IntProp, "sourceRef1 ReferenceProperty.IntProp error."); destinationCollectionElement = destination.CollectionOfReferenceProperties.FirstOrDefault(d => d.GuidProp == sourceRef2.GuidProp); Assert.IsNotNull(destinationCollectionElement, "sourceRef2 destination is null."); Assert.AreEqual(sourceRef2.StringProp, destinationCollectionElement.StringProp, "sourceRef2 ReferenceProperty.StringProp error."); Assert.AreEqual(sourceRef2.IntProp, destinationCollectionElement.IntProp, "sourceRef2 ReferenceProperty.IntProp error."); }
public void MapperForSourceIsNotImplemented_Exception() { var mapper = new Mapper(); mapper.CreateConfiguration<MockSource, MockDestination>() .AddMap(s => s.GuidProp, d => d.GuidProp) .AddMap(s => s.StringProp, d => d.StringProp) .AddMap(s => s.IntProp, d => d.IntProp) .AddMap(s => s.DateTimeProp, d => d.DateTimeProp) .AddMap(s => s.DoubleProp, d => d.DoubleProp); var source = new MockSource1() { GuidProp = Guid.NewGuid(), StringProp = "anyString", IntProp = 1234 }; var result = mapper.Map<MockSource1, MockDestination>(source); }