コード例 #1
0
        public void Should_Work_With_Comparing_String_Types()
        {
            var mapper = CreateMapper(cfg =>
            {
                cfg.AddCollectionMappers();
                cfg.CreateMap <Charge, SaleCharge>()
                .ForMember(d => d.SaleId, (IMemberConfigurationExpression <Charge, SaleCharge, Guid> o) => o.Ignore())
                .EqualityComparison((Charge c, SaleCharge sc) => sc.Category == c.Category && sc.Description == c.Description);

                cfg.CreateMap <SaleCharge, Charge>()
                .ConstructUsing(
                    (saleCharge => new Charge(saleCharge.Category, saleCharge.Description, saleCharge.Value)))
                .EqualityComparison((SaleCharge sc, Charge c) => sc.Category == c.Category && sc.Description == c.Description);
            });

            var dto    = new Charge("catagory", "description", 5);
            var entity = new SaleCharge {
                Category = dto.Category, Description = dto.Description
            };
            var entityCollection = new List <SaleCharge> {
                entity
            };

            mapper.Map(new[] { dto }, entityCollection);

            entity.ShouldBeEquivalentTo(entityCollection[0]);
        }
        public void Should_Work_With_Comparing_String_Types()
        {
            Mapper.Reset();
            Mapper.Initialize(cfg =>
            {
                cfg.AddCollectionMappers();
                cfg.CreateMap <Charge, SaleCharge>()
                .ForMember(d => d.SaleId, o => o.Ignore())
                .EqualityComparison((c, sc) => sc.Category == c.Category && sc.Description == c.Description);

                cfg.CreateMap <SaleCharge, Charge>()
                .ConstructUsing(
                    (saleCharge => new Charge(saleCharge.Category, saleCharge.Description, saleCharge.Value)))
                .EqualityComparison((sc, c) => sc.Category == c.Category && sc.Description == c.Description);
            });

            var dto    = new Charge("catagory", "description", 5);
            var entity = new SaleCharge {
                Category = dto.Category, Description = dto.Description
            };
            var entityCollection = new List <SaleCharge> {
                entity
            };

            Mapper.Map(new[] { dto }, entityCollection);

            entity.ShouldBeEquivalentTo(entityCollection[0]);
        }