public void ComplexTypeMappingRespectsExistingMappingForOtherTypes() { var mapper = new MemberMapper(); var proposed = mapper.CreateMapProposal(typeof(NestedSourceType), typeof(NestedDestinationType), options: (ctx, option) => { if (ctx.Source.Name == "Name") { option.IgnoreMember(); } }); proposed.FinalizeMap(); var source = new ComplexSourceType { ID = 5, Complex = new NestedSourceType { ID = 10, Name = "test" } }; var destination = mapper.Map <ComplexSourceType, ComplexDestinationType>(source); Assert.AreEqual(destination.ID, 5); Assert.IsNotNull(destination.Complex); Assert.AreNotEqual(destination.Complex.Name, source.Complex.Name); }
public void ComplexTypeMappingRespectsExistingMapping() { var mapper = new MemberMapper(); var proposed = mapper.CreateMap(typeof(ComplexSourceType), typeof(ComplexDestinationType), (s, p, option) => { if (s.Name == "Name") { option.IgnoreMember(); } }); proposed.FinalizeMap(); var source = new ComplexSourceType { ID = 5, Complex = new NestedSourceType { ID = 10, Name = "test" } }; var destination = mapper.Map<ComplexSourceType, ComplexDestinationType>(source); Assert.AreEqual(destination.ID, 5); Assert.IsNotNull(destination.Complex); Assert.AreNotEqual(destination.Complex.Name, source.Complex.Name); }
public void ComplexTypeMappingHandlesNullValues() { var mapper = new MemberMapper(); var source = new ComplexSourceType { ID = 5, Complex = null }; var destination = mapper.Map<ComplexSourceType, ComplexDestinationType>(source); Assert.AreEqual(destination.ID, 5); Assert.IsNull(destination.Complex); }
public void ComplexTypeMappingHandlesNullValues() { var mapper = new MemberMapper(); var source = new ComplexSourceType { ID = 5, Complex = null }; var destination = mapper.Map <ComplexSourceType, ComplexDestinationType>(source); Assert.AreEqual(destination.ID, 5); Assert.IsNull(destination.Complex); }
static void Main(string[] args) { Mapper.Initialize(cfg => { cfg.CreateMap <ComplexSourceType, ComplexDestinationType>().ConvertUsing(new ComplexTypeConverter()); cfg.CreateMap <SourceType, DestinationType>().ConvertUsing(new TypeConverter()); }); Mapper.AssertConfigurationIsValid(); ComplexSourceType source = new ComplexSourceType { MyProperty = new SourceType { ValueToBeComputed1 = 1, ValueToBeComputed2 = 1 }, ValueToBeComputed = 1 }; var dest = Mapper.Map <ComplexSourceType, ComplexDestinationType>(source); }
public void ComplexTypeIsCorrectlyMapped() { var mapper = new MemberMapper(); var source = new ComplexSourceType { ID = 5, Complex = new NestedSourceType { ID = 10, Name = "test" } }; var destination = mapper.Map<ComplexSourceType, ComplexDestinationType>(source); Assert.AreEqual(destination.ID, 5); Assert.IsNotNull(destination.Complex); Assert.AreEqual(destination.Complex.Name, "test"); Assert.AreEqual(destination.Complex.ID, 10); }
public void ComplexTypeIsCorrectlyMapped() { var mapper = new MemberMapper(); var source = new ComplexSourceType { ID = 5, Complex = new NestedSourceType { ID = 10, Name = "test" } }; var destination = mapper.Map <ComplexSourceType, ComplexDestinationType>(source); Assert.AreEqual(destination.ID, 5); Assert.IsNotNull(destination.Complex); Assert.AreEqual(destination.Complex.Name, "test"); Assert.AreEqual(destination.Complex.ID, 10); }
static void Benchmark() { var mapper = new MemberMapper(); var map = mapper.CreateMapProposal <ComplexSourceType, ComplexDestinationType>(customMapping: src => new ComplexDestinationType { ID = Enumerable.Range(0, 100000).Count() }).FinalizeMap(); var source = new ComplexSourceType { ID = 5, Complex = new NestedSourceType { ID = 10, Name = "test" } }; source = null; f = (src, dest) => { dest.ID = src.ID; if (src.Complex != null) { var complexSource = src.Complex; var complexDestination = new NestedDestinationType(); complexDestination.ID = Enumerable.Range(0, 100000).Count(); complexDestination.Name = complexSource.Name; dest.Complex = complexDestination; } return(dest); }; var sw = Stopwatch.StartNew(); const int iterations = 100; for (int i = 0; i < iterations; i++) { Foo = new ComplexDestinationType(); Foo.ID = source.ID; if (source.Complex != null) { var complexSource = source.Complex; var complexDestination = new NestedDestinationType(); complexDestination.ID = Enumerable.Range(0, 100000).Count(); complexDestination.Name = complexSource.Name; Foo.Complex = complexDestination; } } sw.Stop(); Console.WriteLine("Manual " + sw.Elapsed); sw.Restart(); for (int i = 0; i < iterations; i++) { Foo = f(source, new ComplexDestinationType()); } sw.Stop(); Console.WriteLine("Func " + sw.Elapsed); sw.Restart(); for (int i = 0; i < iterations; i++) { Foo = mapper.Map <ComplexSourceType, ComplexDestinationType>(source); } sw.Stop(); Console.WriteLine("Map " + sw.Elapsed); var func = (Func <ComplexSourceType, ComplexDestinationType, ComplexDestinationType>)map.MappingFunction; var destination = new ComplexDestinationType(); sw.Restart(); for (int i = 0; i < iterations; i++) { Foo = func(source, new ComplexDestinationType()); } sw.Stop(); Console.WriteLine("Map 1 " + sw.Elapsed); }
static void Benchmark() { var mapper = new MemberMapper(); var map = mapper.CreateMap(typeof(ComplexSourceType), typeof(ComplexDestinationType)).FinalizeMap(); var source = new ComplexSourceType { ID = 5, Complex = new NestedSourceType { ID = 10, Name = "test" } }; Func<ComplexSourceType, ComplexDestinationType, ComplexDestinationType> f = (src, dest) => { dest.ID = src.ID; if (src.Complex != null) { var complexSource = src.Complex; var complexDestination = new NestedDestinationType(); complexDestination.ID = complexSource.ID; complexDestination.Name = complexSource.Name; dest.Complex = complexDestination; } return dest; }; var sw = Stopwatch.StartNew(); for (int i = 0; i < 1000000; i++) { Foo = new ComplexDestinationType(); Foo.ID = source.ID; if (source.Complex != null) { var complexSource = source.Complex; var complexDestination = new NestedDestinationType(); complexDestination.ID = complexSource.ID; complexDestination.Name = complexSource.Name; Foo.Complex = complexDestination; } } sw.Stop(); Console.WriteLine(sw.Elapsed); sw.Restart(); for (int i = 0; i < 1000000; i++) { Foo = f(source, new ComplexDestinationType()); } sw.Stop(); Console.WriteLine(sw.Elapsed); sw.Restart(); for (int i = 0; i < 1000000; i++) { Foo = mapper.Map<ComplexSourceType, ComplexDestinationType>(source); } sw.Stop(); Console.WriteLine(sw.Elapsed); var func = (Func<ComplexSourceType, ComplexDestinationType, ComplexDestinationType>)map.MappingFunction; var destination = new ComplexDestinationType(); sw.Restart(); for (int i = 0; i < 1000000; i++) { Foo = func(source, new ComplexDestinationType()); } sw.Stop(); Console.WriteLine(sw.Elapsed); }