public void ShouldUseSameDependencyDuringMappingOperation() { //While Mapping, if requesting the same dependency on multiple maps, the same dependency should be //used (instead of going back to the IOC container). //The idea is that mapping an object is a single function (and therefore the dependencies should be resolved only once). var foo = new FooX(); var resolver = new ResolverMock(); resolver.Add(() => new Dependency1()); var mapper = CreateMapper(resolver, builder => { builder.CreateMap <FooX, BarX>() .WithDependencies <Dependency1>() .MapProperty((x, dep) => dep.Item1.RandomProperty, x => x.Property4) .MapProperty((x, dep) => dep.Item1.RandomProperty, x => x.Property3); }); var bar1 = mapper.Map <BarX>(foo); var bar2 = mapper.Map <BarX>(foo); Assert.AreEqual(bar1.Property3, bar1.Property4, "Those properties must be the same!"); Assert.AreEqual(bar2.Property3, bar2.Property4, "Those properties must be the same!"); Assert.AreNotEqual(bar1.Property3, bar2.Property3, "Those properties cannot be the same!"); Assert.AreNotEqual(bar1.Property4, bar2.Property4, "Those properties cannot be the same!"); }
public void ShouldMapWithDifferentDependenciesResolvedByName() { var foo = new FooX() { Property1 = 10, Property2 = 100 }; var resolver = new ResolverMock(); resolver.Add(() => new Dependency1() { GlobalProperty1 = 1 }, "dependency1"); resolver.Add(() => new Dependency1() { GlobalProperty1 = 2 }, "dependency2"); var mapper = CreateMapper(resolver, builder => { builder.CreateMap <Foo, Bar>() .WithDependencies <Dependency1>("dependency1") .MapProperty((x, dep) => dep.Item1.GlobalProperty1 + x.Property1, x => x.Property3); builder.CreateMap <FooX, BarX>() .WithDependencies <Dependency1>("dependency2") .MapProperty((x, dep) => dep.Item1.GlobalProperty1 + x.Property2, x => x.Property4); }); var bar = (BarX)mapper.Map <Bar>(foo); Assert.AreEqual(11, bar.Property3); Assert.AreEqual(102, bar.Property4); }
public void ShouldNotLookForParentsWhenMappingInheritedProperties() { //NOTE: By design, when calling Map<FooX>, no mappings for Foo will be used - eventhough FooX inherits from Foo. //To get all mappings, ca var foo = new FooX() { Property1 = 18, Property2 = 7 }; var mapper = CreateMapper(new ResolverMock(), builder => { builder.CreateMap <Foo, Bar>() .MapProperty(x => x.Property1, x => x.Property3); builder.CreateMap <FooX, BarX>() .MapProperty(x => x.Property2, x => x.Property4); }); //Not specifying TTargetBase will skip all mappings from BarX upwards (the inheritance chain). var barX1 = mapper.Map <BarX, BarX>(foo); Assert.AreEqual(0, barX1.Property3); Assert.AreEqual(7, barX1.Property4); //Specifying TTargetBase will enture all mappings from Bar downwards (the inheritance chain) are applied. var barX2 = mapper.Map <BarX, Bar>(foo); Assert.AreEqual(18, barX2.Property3); Assert.AreEqual(7, barX2.Property4); }
public void ShouldUseMappingFunctionInherited() { var fooX = new FooX() { Property1 = 1, Property2 = 7 }; var mapper = CreateMapper(new ResolverMock(), builder => { //NOTE: This is interesting because FooBarMapping function take Foo and Bar, not FooX and BarX builder.CreateMap <FooX, BarX>() .MapFunction(FooBarMappingFunction) .MapProperty(x => x.Property2, x => x.Property4); }); var barX = mapper.Map <BarX>(fooX); Assert.AreEqual(1, barX.Property3); Assert.AreEqual(7, barX.Property4); }
public void ShouldMapInheritedPropertyExistingObject() { var foo = new FooX() { Property1 = 18, Property2 = 7 }; var mapper = CreateMapper(new ResolverMock(), builder => { builder.CreateMap <Foo, Bar>() .MapProperty(x => x.Property1, x => x.Property3); builder.CreateMap <FooX, BarX>() .MapProperty(x => x.Property2, x => x.Property4); }); var bar = new Bar(); mapper.Map(foo, bar); Assert.AreEqual(foo.Property1, bar.Property3); }
public void ShouldMapInheritedProperty() { var foo = new FooX() { Property1 = 18, Property2 = 7 }; var mapper = CreateMapper(new ResolverMock(), builder => { builder.CreateMap <Foo, Bar>() .MapProperty(x => x.Property1, x => x.Property3); builder.CreateMap <FooX, BarX>() .MapProperty(x => x.Property2, x => x.Property4); }); var bar = mapper.Map <Bar>(foo); Assert.IsInstanceOf <BarX>(bar, "Even if you have requested 'Bar', the mapper found a more specific type and will return that instead."); var barX = (BarX)bar; Assert.AreEqual(foo.Property1, barX.Property3); Assert.AreEqual(foo.Property2, barX.Property4); }