public void ShouldFilterAnEnumerableSourceValueConditionallyInline()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicTwoFields <Product[], Product[]> >()
                .Over <PublicProperty <List <ProductDto> > >()
                .Map(ctx => ctx.Source.Value1)
                .To(t => t.Value)
                .But
                .If(ctx => ctx.Source.Value1.None())
                .Map(ctx => ctx.Source.Value2)
                .To(t => t.Value);

                var target = new PublicProperty <List <ProductDto> >();

                var bothValuesSource = new PublicTwoFields <Product[], Product[]>
                {
                    Value1 = new[] { new Product {
                                         ProductId = "111"
                                     } },
                    Value2 = new[] { new Product {
                                         ProductId = "222"
                                     } }
                };

                mapper.Map(bothValuesSource).Over(target);

                target.Value.ShouldHaveSingleItem().ProductId.ShouldBe("111");
                target.Value.Clear();

                var emptyValue1Source = new PublicTwoFields <Product[], Product[]>
                {
                    Value1 = Enumerable <Product> .EmptyArray,
                    Value2 = new[] { new Product {
                                         ProductId = "222"
                                     } }
                };

                mapper.Map(emptyValue1Source).Over(target);

                target.Value.ShouldHaveSingleItem().ProductId.ShouldBe("222");
                target.Value.Clear();

                mapper
                .Map(bothValuesSource)
                .Over(target, cfg => cfg
                      .IgnoreSources(s => s.If <Product[]>(ps => ps[0].ProductId == "111")));

                target.Value.ShouldBeEmpty();
                target.Value = null;

                mapper
                .Map(bothValuesSource)
                .Over(target, cfg => cfg
                      .IgnoreSources(s => s.If <Product[]>(ps => ps[0].ProductId == "111")));

                target.Value.ShouldBeNull();
            }
        }
        public void ShouldAllowOverlappingMemberSpecificIgnore()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .Over <PublicTwoFields <string, string> >()
                .IfTargetMemberMatches(member => member
                                       .IsFieldMatching(f => f.FieldType == typeof(string)))
                .Map((s, _) => s.ToString())
                .ToTarget();

                mapper.WhenMapping
                .To <PublicTwoFields <string, string> >()
                .Ignore(ptf => ptf.Value1);

                var source = new PublicTwoFields <string, string>
                {
                    Value1 = "Tata",
                    Value2 = "Goodbye"
                };

                var target = new PublicTwoFields <string, string>
                {
                    Value1 = "Cya",
                    Value2 = "Laterz"
                };

                mapper.Map(source).Over(target);

                target.Value1.ShouldBe("Cya");
                target.Value2.ShouldBe(source.ToString());
            }
        }
        public void ShouldApplyASequentialDataSourceToARootArray()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicTwoFields <Address[], Address[]> >()
                .ToANew <Address[]>()
                .Map((src, _) => src.Value1)
                .Then.Map((src, _) => src.Value2)
                .ToTarget();

                var source = new PublicTwoFields <Address[], Address[]>
                {
                    Value1 = new[] { new Address {
                                         Line1 = "Address 1"
                                     } },
                    Value2 = new[]
                    {
                        new Address {
                            Line1 = "Address 2"
                        },
                        new Address {
                            Line1 = "Address 3"
                        }
                    }
                };

                var result = mapper.Map(source).ToANew <Address[]>();

                result.Length.ShouldBe(3);
                result.First().Line1.ShouldBe("Address 1");
                result.Second().Line1.ShouldBe("Address 2");
                result.Third().Line1.ShouldBe("Address 3");
            }
        }
示例#4
0
        public void ShouldMapMultipleRuntimeTypedChildMembers()
        {
            var source = new PublicTwoFields <IEnumerable <object>, object[]>
            {
                Value1 = new[] { new Product {
                                     ProductId = "Giant Trousers", Price = 100.00
                                 } },
                Value2 = new object[] { new MysteryCustomerViewModel {
                                            Name = "Mr Faff"
                                        } }
            };
            var target = new PublicTwoFields <List <MegaProduct>, ICollection <Customer> >
            {
                Value1 = new List <MegaProduct>(),
                Value2 = Enumerable <Customer> .EmptyArray
            };
            var result = Mapper.Map(source).OnTo(target);

            result.Value1.ShouldHaveSingleItem();
            result.Value1.First().ProductId.ShouldBe("Giant Trousers");
            result.Value1.First().Price.ShouldBe(100.00);

            result.Value2.ShouldHaveSingleItem();
            result.Value2.First().Name.ShouldBe("Mr Faff");
        }
        public void ShouldPairEnumMembersInline()
        {
            using (var mapper = Mapper.CreateNew())
            {
                var source = new PublicTwoFields <PaymentTypeUs, PaymentTypeUk>
                {
                    Value1 = PaymentTypeUs.Check,
                    Value2 = PaymentTypeUk.Cheque
                };

                var result = mapper
                             .Map(source)
                             .ToANew <PublicTwoParamCtor <PaymentTypeUs, PaymentTypeUk> >(cfg => cfg
                                                                                          .PairEnum(PaymentTypeUk.Cheque).With(PaymentTypeUs.Check)
                                                                                          .And
                                                                                          .Map(ctx => ctx.Source.Value1)
                                                                                          .ToCtor <PaymentTypeUk>()
                                                                                          .And
                                                                                          .Map(ctx => ctx.Source.Value2)
                                                                                          .ToCtor <PaymentTypeUs>());

                result.Value1.ShouldBe(PaymentTypeUs.Check);
                result.Value2.ShouldBe(PaymentTypeUk.Cheque);
            }
        }
        public void ShouldUseAConfiguredNonEnumerableToEnumerableFactoryForANestedArrayMappingInline()
        {
            using (var mapper = Mapper.CreateNew())
            {
                var source = new PublicTwoFields <string, Address>
                {
                    Value1 = "Hello!",
                    Value2 = new Address {
                        Line1 = "Line 1"
                    }
                };

                var result = mapper.Map(source).ToANew <PublicTwoFields <string, Address[]> >(cfg => cfg
                                                                                              .WhenMapping
                                                                                              .From <Address>()
                                                                                              .To <Address[]>()
                                                                                              .MapInstancesUsing(ctx => new[]
                {
                    new Address {
                        Line1 = ctx.Source.Line1
                    },
                    new Address {
                        Line1 = ctx.Source.Line1 + " again"
                    }
                }));

                result.Value1.ShouldBe("Hello!");
                result.Value2.ShouldNotBeNull().Length.ShouldBe(2);
                result.Value2.First().Line1.ShouldBe("Line 1");
                result.Value2.Second().Line1.ShouldBe("Line 1 again");
            }
        }
        public void ShouldMapFlattenedMembersFromANestedDynamicToANestedComplexType()
        {
            dynamic sourceDynamic = new ExpandoObject();

            sourceDynamic.Name         = "Mystery :o";
            sourceDynamic.AddressLine1 = "Over here";
            sourceDynamic.AddressLine2 = "Over there";

            var source = new PublicTwoFields <dynamic, string>
            {
                Value1 = sourceDynamic,
                Value2 = "Blimey!!"
            };

            var target = new PublicTwoFields <MysteryCustomer, string>
            {
                Value1 = new MysteryCustomer {
                    Name = "Mystery?!"
                },
                Value2 = "Nowt"
            };

            var preMappingCustomer = target.Value1;

            Mapper.Map(source).Over(target);

            target.Value1.ShouldBeSameAs(preMappingCustomer);
            target.Value1.Name.ShouldBe("Mystery :o");
            target.Value1.Address.ShouldNotBeNull();
            target.Value1.Address.Line1.ShouldBe("Over here");
            target.Value1.Address.Line2.ShouldBe("Over there");
            target.Value2.ShouldBe("Blimey!!");
        }
        public void ShouldApplyANestedOverwriteToTargetDataSource()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicTwoFields <int, PublicField <PublicTwoFields <int, int> > > >()
                .Over <PublicTwoFields <int, int> >()
                .Map((s, t) => s.Value2.Value)
                .ToTarget();

                var source = new PublicTwoFields <int, PublicField <PublicTwoFields <int, int> > >
                {
                    Value1 = 6372,
                    Value2 = new PublicField <PublicTwoFields <int, int> >
                    {
                        Value = new PublicTwoFields <int, int>
                        {
                            Value2 = 8262
                        }
                    }
                };

                var target = new PublicTwoFields <int, int>
                {
                    Value1 = 637,
                    Value2 = 728
                };

                mapper.Map(source).Over(target);

                target.Value1.ShouldBeDefault(); // <- Because Value2.Value.Value1 will overwrite 6372
                target.Value2.ShouldBe(8262);
            }
        }
        public void ShouldCloneSimpleTypeValuesInAnObjectDictionary()
        {
            var source = new PublicTwoFields <int, Dictionary <string, object> >
            {
                Value1 = 6372,
                Value2 = new Dictionary <string, object>
                {
                    ["QueryName"]       = "References",
                    ["IsDefault"]       = false,
                    ["QueryId"]         = 155,
                    ["WorkspaceTypeId"] = 1,
                    ["IsUserDefined"]   = true,
                    ["QueryTypeId"]     = 2,
                    ["Test"]            = default(int?)
                }
            };

            var result = source.DeepClone();

            result.Value1.ShouldBe(6372);
            result.Value2.ShouldNotBeNull();
            result.Value2.ShouldNotBeSameAs(source.Value2);
            result.Value2.Count.ShouldBe(source.Value2.Count);
            result.Value2.ShouldContainKeyAndValue("QueryName", "References");
            result.Value2.ShouldContainKeyAndValue("IsDefault", false);
            result.Value2.ShouldContainKeyAndValue("QueryId", 155);
            result.Value2.ShouldContainKeyAndValue("WorkspaceTypeId", 1);
            result.Value2.ShouldContainKeyAndValue("IsUserDefined", true);
            result.Value2.ShouldContainKeyAndValue("QueryTypeId", 2);
            result.Value2.ShouldContainKeyAndValue("Test", null);
        }
示例#10
0
        public void ShouldExecuteAPreMappingCallbackInARootToTargetMapping()
        {
            using (var mapper = Mapper.CreateNew())
            {
                var callbackCalled = false;

                mapper.WhenMapping
                .From <PublicTwoFields <PublicField <int>, int> >()
                .To <PublicField <int> >()
                .Map(ctx => ctx.Source.Value1)
                .ToTarget();

                mapper.WhenMapping
                .From <PublicField <int> >()
                .To <PublicField <int> >()
                .Before.MappingBegins
                .Call(md => callbackCalled = true);

                var source = new PublicTwoFields <PublicField <int>, int>
                {
                    Value1 = new PublicField <int> {
                        Value = 123
                    },
                    Value2 = 456
                };

                var result = mapper.Map(source).ToANew <PublicField <int> >();

                result.Value.ShouldBe(123);
                callbackCalled.ShouldBeTrue();
            }
        }
示例#11
0
        public void ShouldIgnoreSourceMemberByNullableIntValueFilterGlobally()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .IgnoreSources(c => c.If <int>(i => i < 10));

                var source = new PublicTwoFields <int, string> {
                    Value1 = 8, Value2 = "456"
                };
                var result = mapper.Map(source).ToANew <PublicTwoParamCtor <string, int> >();

                result.ShouldNotBeNull();
                result.Value1.ShouldBeNull();
                result.Value2.ShouldBe(456);

                var nonMatchingFilterSource = new PublicTwoFields <int, string> {
                    Value1 = 12, Value2 = "77"
                };
                var nonMatchingFilterResult = mapper.Map(nonMatchingFilterSource).ToANew <PublicTwoParamCtor <string, long> >();

                nonMatchingFilterResult.ShouldNotBeNull();
                nonMatchingFilterResult.Value1.ShouldBe("12");
                nonMatchingFilterResult.Value2.ShouldBe(77L);

                var nullableSource = new PublicTwoFields <int?, string> {
                    Value1 = 8, Value2 = "99"
                };
                var nullableResult = mapper.Map(nullableSource).ToANew <PublicTwoParamCtor <string, int> >();

                nullableResult.ShouldNotBeNull();
                nullableResult.Value1.ShouldBeNull();
                nullableResult.Value2.ShouldBe(99);
            }
        }
        public void ShouldHandleRuntimeTypedComplexAndEnumerableElementMembers()
        {
            var source = new PublicTwoFields <object, IList <object> >
            {
                Value1 = new Product {
                    ProductId = "kjdfskjnds"
                },
                Value2 = new List <object>
                {
                    new PublicProperty <string> {
                        Value = "ikjhfeslkjdw"
                    },
                    new PublicField <string> {
                        Value = "ldkjkdhusdiuoji"
                    }
                }
            };

            var result = source.DeepClone();

            result.Value1.ShouldBeOfType <Product>();
            ((Product)result.Value1).ProductId.ShouldBe("kjdfskjnds");

            result.Value2.Count.ShouldBe(2);
            result.Value2.First().ShouldBeOfType <PublicProperty <string> >();
            ((PublicProperty <string>)result.Value2.First()).Value.ShouldBe("ikjhfeslkjdw");

            result.Value2.Second().ShouldBeOfType <PublicField <string> >();
            ((PublicField <string>)result.Value2.Second()).Value.ShouldBe("ldkjkdhusdiuoji");
        }
        public void ShouldHandleMultipleRuntimeTypedMembers()
        {
            var arraysSource = new PublicTwoFields <object, object>
            {
                Value1 = new[] { 1, 2, 3 },
                Value2 = new[] { "4", "5", "6" }
            };

            var arraysResult = Mapper.Map(arraysSource).ToANew <PublicTwoFields <long[], long[]> >();

            arraysResult.Value1.ShouldBe(1L, 2L, 3L);
            arraysResult.Value2.ShouldBe(4L, 5L, 6L);

            var listsSource = new PublicTwoFields <object, object>
            {
                Value1 = new List <int> {
                    7, 8, 9
                },
                Value2 = new List <string> {
                    "10", "11", "12"
                }
            };

            var listsResult = Mapper.Map(listsSource).ToANew <PublicTwoFields <long[], long[]> >();

            listsResult.Value1.ShouldBe(7L, 8L, 9L);
            listsResult.Value2.ShouldBe(10L, 11L, 12L);
        }
        public void ShouldIgnoreMembersBySourceTypeTargetTypeAndNameMatch()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicTwoFields <int, int> >()
                .To <PublicTwoFields <int, int> >()
                .IgnoreTargetMembersWhere(member => member.Name.Contains("Value1"));

                var matchingSource = new PublicTwoFields <int, int> {
                    Value1 = 1, Value2 = 2
                };
                var nonMatchingSource = new { Value1 = -1, Value2 = -2 };

                var matchingResult = mapper.Map(matchingSource).ToANew <PublicTwoFields <int, int> >();
                matchingResult.Value1.ShouldBeDefault();
                matchingResult.Value2.ShouldBe(2);

                var nonMatchingTargetResult = mapper.Map(matchingSource).ToANew <PublicTwoParamCtor <int, int> >();
                nonMatchingTargetResult.Value1.ShouldBe(1);
                nonMatchingTargetResult.Value2.ShouldBe(2);

                var nonMatchingSourceResult = mapper.Map(nonMatchingSource).ToANew <PublicTwoFields <int, int> >();
                nonMatchingSourceResult.Value1.ShouldBe(-1);
                nonMatchingSourceResult.Value2.ShouldBe(-2);

                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <PublicTwoParamCtor <int, int> >();
                nonMatchingResult.Value1.ShouldBe(-1);
                nonMatchingResult.Value2.ShouldBe(-2);
            }
        }
        public void ShouldIgnoreSourceFieldsBySourceTypeTargetTypeAndFieldInfoMatcher()
        {
            using (var mapper = Mapper.CreateNew())
            {
                var field1 = typeof(PublicTwoFields <int, int>).GetPublicInstanceField("Value1");

                mapper.WhenMapping
                .From <PublicTwoFields <int, int> >()
                .To <PublicTwoFields <string, string> >()
                .IgnoreSourceMembersWhere(member =>
                                          member.IsFieldMatching(f => f == field1));

                var matchingSource = new PublicTwoFields <int, int> {
                    Value1 = 111, Value2 = 222
                };
                var nonMatchingSource = new { Value1 = 333, Value2 = 444 };

                var matchingResult = mapper.Map(matchingSource).ToANew <PublicTwoFields <string, string> >();
                matchingResult.Value1.ShouldBeNull();
                matchingResult.Value2.ShouldBe("222");

                var nonMatchingTargetResult = mapper.Map(matchingSource).ToANew <PublicTwoFields <string, int> >();
                nonMatchingTargetResult.Value1.ShouldBe("111");
                nonMatchingTargetResult.Value2.ShouldBe(222);

                var nonMatchingSourceResult = mapper.Map(nonMatchingSource).ToANew <PublicTwoFields <string, string> >();
                nonMatchingSourceResult.Value1.ShouldBe("333");
                nonMatchingSourceResult.Value2.ShouldBe("444");

                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <PublicTwoFields <int, string> >();
                nonMatchingResult.Value1.ShouldBe(333);
                nonMatchingResult.Value2.ShouldBe("444");
            }
        }
        public void ShouldMapFromANestedDynamicToANestedComplexType()
        {
            dynamic sourceDynamic = new ExpandoObject();

            sourceDynamic.Line1 = "Over there";

            var source = new PublicTwoFields <dynamic, string>
            {
                Value1 = sourceDynamic,
                Value2 = "Good Googley Moogley!"
            };

            var target = new PublicTwoFields <Address, string>
            {
                Value1 = new Address {
                    Line1 = "Over here", Line2 = "Somewhere else"
                },
                Value2 = "Nothing"
            };

            var preMappingAddress = target.Value1;

            Mapper.Map(source).Over(target);

            target.Value1.ShouldBeSameAs(preMappingAddress);
            target.Value1.Line1.ShouldBe("Over there");
            target.Value1.Line2.ShouldBe("Somewhere else");
            target.Value2.ShouldBe("Good Googley Moogley!");
        }
示例#17
0
        public void ShouldAllowACustomTargetFullEntryKeyForAComplexTypeSourceMember()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicTwoFields <Address, Address> >()
                .ToDictionariesWithValueType <Address>()
                .MapMember(pf => pf.Value2)
                .ToFullKey("Address");

                var source = new PublicTwoFields <Address, Address>
                {
                    Value1 = new Address {
                        Line1 = "1.1", Line2 = "1.2"
                    },
                    Value2 = new Address {
                        Line1 = "2.1", Line2 = "2.2"
                    },
                };
                var result = mapper.Map(source).ToANew <Dictionary <string, Address> >();

                result["Value1"].Line1.ShouldBe("1.1");
                result["Value1"].Line2.ShouldBe("1.2");
                result["Address"].Line1.ShouldBe("2.1");
                result["Address"].Line2.ShouldBe("2.2");
            }
        }
        public void ShouldIgnoreMembersBySourceTypeTargetTypeAndAttribute()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicTwoFields <int, int> >()
                .To <AttributeHelper>()
                .IgnoreTargetMembersWhere(member => member.HasAttribute <IgnoreMeAttribute>());

                var matchingSource = new PublicTwoFields <int, int> {
                    Value1 = 10, Value2 = 20
                };
                var matchingResult = mapper.Map(matchingSource).ToANew <AttributeHelper>();
                matchingResult.Value1.ShouldBeDefault();
                matchingResult.Value2.ShouldBe("20");

                var nonMatchingTargetResult = mapper.Map(matchingSource).ToANew <PublicTwoFields <string, string> >();
                nonMatchingTargetResult.Value1.ShouldBe("10");
                nonMatchingTargetResult.Value2.ShouldBe("20");

                var nonMatchingSource       = new { Value1 = "11", Value2 = "21" };
                var nonMatchingSourceResult = mapper.Map(nonMatchingSource).ToANew <AttributeHelper>();
                nonMatchingSourceResult.Value1.ShouldBe("11");
                nonMatchingSourceResult.Value2.ShouldBe("21");

                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <PublicTwoFields <string, string> >();
                nonMatchingResult.Value1.ShouldBe("11");
                nonMatchingResult.Value2.ShouldBe("21");
            }
        }
        public void ShouldFlattenANullNullableIntToAStringDictionary()
        {
            var source = new PublicTwoFields <int?, int?> {
                Value1 = 123, Value2 = null
            };
            var result = Mapper.Flatten(source).ToDictionary <string>();

            result["Value1"].ShouldBe("123");
            result.ShouldNotContainKey("Value2");
        }
示例#20
0
        public void ShouldUseCtorIfMoreAvailableDataSources()
        {
            var source = new PublicTwoFields <long, long> {
                Value1 = 123L, Value2 = 987L
            };

            var result = Mapper.Map(source).ToANew <SingleParameterGetMethodMultiParameterCtor>();

            result.ShouldNotBeNull();
            result.Value1.ShouldBe("123");
            result.Value2.ShouldBe("987");
        }
示例#21
0
        public void ShouldOverwriteAMemberWithAMatchingCtorParameter()
        {
            var source = new PublicTwoFields <int, int> {
                Value1 = 123, Value2 = 456
            };
            var target = new PublicTwoParamCtor <int, int>(111, 222);

            Mapper.Map(source).Over(target);

            target.Value1.ShouldBe(111);
            target.Value2.ShouldBe(456);
        }
        public void ShouldApplyASequentialDataSourceToARootArrayConditionally()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicTwoFields <Address[], Address[]> >()
                .ToANew <Address[]>()
                .Map((src, _) => src.Value1)
                .Then
                .If(ctx => ctx.Source.Value2.Length > 1)
                .Map((src, _) => src.Value2)
                .ToTarget();

                var nonMatchingSource = new PublicTwoFields <Address[], Address[]>
                {
                    Value1 = new[] { new Address {
                                         Line1 = "Address 1"
                                     } },
                    Value2 = new[] { new Address {
                                         Line1 = "Address 2"
                                     } }
                };

                var nonMatchingResult = mapper.Map(nonMatchingSource).ToANew <Address[]>();

                nonMatchingResult.ShouldHaveSingleItem().Line1.ShouldBe("Address 1");

                var matchingSource = new PublicTwoFields <Address[], Address[]>
                {
                    Value1 = new[] { new Address {
                                         Line1 = "Address 1"
                                     } },
                    Value2 = new[]
                    {
                        new Address {
                            Line1 = "Address 2"
                        },
                        new Address {
                            Line1 = "Address 3"
                        }
                    }
                };

                var matchingResult = mapper.Map(matchingSource).ToANew <Address[]>();

                matchingResult.Length.ShouldBe(3);
                matchingResult.First().Line1.ShouldBe("Address 1");
                matchingResult.Second().Line1.ShouldBe("Address 2");
                matchingResult.Third().Line1.ShouldBe("Address 3");
            }
        }
示例#23
0
        public void ShouldIgnoreAMemberComplexType()
        {
            var source = new PublicTwoFields <Guid, Address>
            {
                Value1 = Guid.NewGuid(),
                Value2 = new Address {
                    Line1 = "One", Line2 = "Two"
                }
            };
            var result = Mapper.Map(source).ToANew <PublicTwoFieldsStruct <string, Address> >();

            result.Value1.ShouldBe(source.Value1.ToString());
            result.Value2.ShouldBeNull();
        }
示例#24
0
        public void ShouldIgnoreAMemberArray()
        {
            var guid = Guid.NewGuid();

            var source = new PublicTwoFields <IEnumerable <int>, string>
            {
                Value1 = new[] { 1, 2, 3 },
                Value2 = guid.ToString()
            };
            var result = Mapper.Map(source).ToANew <PublicTwoFieldsStruct <string[], Guid> >();

            result.Value1.ShouldBeNull();
            result.Value2.ShouldBe(guid);
        }
        public void ShouldApplyAToTargetDataSourceConditionally()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicTwoFieldsStruct <PublicPropertyStruct <int>, int> >()
                .OnTo <PublicTwoFields <int, int> >()
                .If((s, t) => s.Value1.Value > 5)
                .Map((s, t) => s.Value1)
                .ToTarget();

                mapper.WhenMapping
                .From <PublicPropertyStruct <int> >()
                .OnTo <PublicTwoFields <int, int> >()
                .Map((s, t) => s.Value)
                .To(t => t.Value1);

                var matchingSource = new PublicTwoFieldsStruct <PublicPropertyStruct <int>, int>
                {
                    Value1 = new PublicPropertyStruct <int> {
                        Value = 10
                    },
                    Value2 = 627
                };

                var target = new PublicTwoFields <int, int> {
                    Value2 = 673282
                };

                mapper.Map(matchingSource).OnTo(target);

                target.Value1.ShouldBe(10);
                target.Value2.ShouldBe(673282);

                var nonMatchingSource = new PublicTwoFieldsStruct <PublicPropertyStruct <int>, int>
                {
                    Value1 = new PublicPropertyStruct <int> {
                        Value = 1
                    },
                    Value2 = 9285
                };

                target.Value1 = target.Value2 = default(int);

                mapper.Map(nonMatchingSource).OnTo(target);

                target.Value1.ShouldBeDefault();
                target.Value2.ShouldBe(9285);
            }
        }
示例#26
0
        public void ShouldSetAnExistingSimpleTypePropertyValueToDefault()
        {
            var source = new PublicTwoFields <double?, int>();
            var target = new PublicTwoFieldsStruct <double?, int> {
                Value1 = 537.0, Value2 = 6382
            };

            var result = Mapper.Map(source).Over(target);

            target.Value1.ShouldBe(537.0m);
            target.Value2.ShouldBe(6382);

            result.Value1.ShouldBeNull();
            result.Value2.ShouldBeDefault();
        }
示例#27
0
        public void ShouldIgnoreSourceMemberByStringValueFilterGlobally()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .IgnoreSources(c => c.If <string>(str => str == "456"));

                var source = new PublicTwoFields <int, string> {
                    Value1 = 123, Value2 = "456"
                };
                var result = mapper.Map(source).ToANew <PublicTwoParamCtor <string, int> >();

                result.ShouldNotBeNull();
                result.Value1.ShouldBe("123");
                result.Value2.ShouldBeDefault();
            }
        }
示例#28
0
        public void ShouldUseANestedAlternateDataSourceConditionally()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <PublicField <PublicField <int> > >()
                .ToANew <PublicField <int> >()
                .If(ctx => ctx.Source.Value.Value > 100)
                .Map((s, t) => s.Value)
                .ToTargetInstead();

                var matchingSource = new PublicTwoFields <int, PublicField <PublicField <int> > >
                {
                    Value2 = new PublicField <PublicField <int> >
                    {
                        Value = new PublicField <int> {
                            Value = 200
                        }
                    }
                };

                var matchingResult = mapper
                                     .Map(matchingSource)
                                     .ToANew <PublicTwoFields <int, PublicField <int> > >();

                matchingResult.Value1.ShouldBeDefault();
                matchingResult.Value2.ShouldNotBeNull().Value.ShouldBe(200);

                var nonMatchingSource = new PublicTwoFields <int, PublicField <PublicField <int> > >
                {
                    Value2 = new PublicField <PublicField <int> >
                    {
                        Value = new PublicField <int> {
                            Value = 100
                        }
                    }
                };

                var nonMatchingResult = mapper
                                        .Map(nonMatchingSource)
                                        .ToANew <PublicTwoFields <int, PublicField <int> > >();

                nonMatchingResult.Value1.ShouldBeDefault();
                nonMatchingResult.Value2.ShouldBeNull();
            }
        }
示例#29
0
        public void ShouldCloneADataSource()
        {
            using (var baseMapper = Mapper.CreateNew())
            {
                baseMapper.WhenMapping
                .From <PublicTwoFields <int, int> >()
                .ToANew <PublicTwoFields <int, int> >()
                .Map((s, t) => s.Value1 * 2)
                .To(t => t.Value1);

                // Populate the derived types cache so it can be cloned:
                baseMapper.GetPlanFor <Customer>().ToANew <CustomerViewModel>();

                using (var childMapper1 = baseMapper.CloneSelf())
                    using (var childMapper2 = baseMapper.CloneSelf())
                    {
                        childMapper1.WhenMapping
                        .From <PublicTwoFields <int, int> >()
                        .ToANew <PublicTwoFields <int, int> >()
                        .Map((s, t) => s.Value2 + 1)
                        .To(t => t.Value2);

                        childMapper2.WhenMapping
                        .From <PublicTwoFields <int, int> >()
                        .ToANew <PublicTwoFields <int, int> >()
                        .Map((s, t) => s.Value2 + 2)
                        .To(t => t.Value2);

                        var source = new PublicTwoFields <int, int>
                        {
                            Value1 = 4,
                            Value2 = 8
                        };

                        var result1 = childMapper1.Map(source).ToANew <PublicTwoFields <int, int> >();
                        var result2 = childMapper2.Map(source).ToANew <PublicTwoFields <int, int> >();

                        result1.Value1.ShouldBe(8);
                        result1.Value2.ShouldBe(9);

                        result2.Value1.ShouldBe(8);
                        result2.Value2.ShouldBe(10);
                    }
            }
        }
        public void ShouldPairEnumMembers()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .PairEnum(PaymentTypeUk.Cheque).With(PaymentTypeUs.Check);

                var source = new PublicTwoFields <PaymentTypeUk, PaymentTypeUs>
                {
                    Value1 = PaymentTypeUk.Cheque,
                    Value2 = PaymentTypeUs.Check
                };
                var result = mapper.Map(source).ToANew <PublicTwoParamCtor <PaymentTypeUs, PaymentTypeUk> >();

                result.Value1.ShouldBe(PaymentTypeUs.Check);
                result.Value2.ShouldBe(PaymentTypeUk.Cheque);
            }
        }