コード例 #1
0
        public void ShouldAllowACustomTargetEntryKey()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <MysteryCustomerViewModel>()
                .ToDynamics
                .MapMember(mcvm => mcvm.Name)
                .ToFullMemberName("CustomerName")
                .And
                .If((mcvm, d) => mcvm.Discount > 0.5)
                .Map((mcvm, d) => mcvm.Name + " (Big discount!)")
                .To(d => d["Name"]);

                var noDiscountSource = new MysteryCustomerViewModel {
                    Name = "Schumer", Discount = 0.0
                };
                var noDiscountResult = (IDictionary <string, object>)mapper.Map(noDiscountSource).ToANew <dynamic>();

                noDiscountResult["CustomerName"].ShouldBe("Schumer");
                noDiscountResult.ShouldNotContainKey("Name");

                var bigDiscountSource = new MysteryCustomerViewModel {
                    Name = "Silverman", Discount = 0.6
                };
                var bigDiscountResult = (IDictionary <string, object>)mapper.Map(bigDiscountSource).ToANew <dynamic>();

                bigDiscountResult["CustomerName"].ShouldBe("Silverman");
                bigDiscountResult["Name"].ShouldBe("Silverman (Big discount!)");
            }
        }
コード例 #2
0
        public void ShouldApplyAConfiguredConditionalTargetEntryValue()
        {
            using (var mapper = Mapper.CreateNew())
            {
                mapper.WhenMapping
                .From <MysteryCustomerViewModel>()
                .ToDynamics
                .If((mcvm, d) => mcvm.Discount > 0.5)
                .Map((mcvm, d) => mcvm.Name + " (Big discount!)")
                .To(d => d["Name"]);

                var noDiscountSource = new MysteryCustomerViewModel {
                    Name = "Schumer", Discount = 0.0
                };
                var noDiscountResult = (IDictionary <string, object>)mapper.Map(noDiscountSource).ToANew <dynamic>();

                noDiscountResult["Name"].ShouldBe("Schumer");

                var bigDiscountSource = new MysteryCustomerViewModel {
                    Name = "Silverman", Discount = 0.6
                };
                var bigDiscountResult = (IDictionary <string, object>)mapper.Map(bigDiscountSource).ToANew <dynamic>();

                bigDiscountResult["Name"].ShouldBe("Silverman (Big discount!)");
            }
        }