示例#1
0
            public void can_work_on_different_collection_type_list()
            {
                var source = new SourceProduct
                {
                    Id       = 10,
                    Variants = new List <SourceProductVariant>
                    {
                        new SourceProductVariant {
                            Id = 1, Name = "variant 1"
                        },
                        new SourceProductVariant {
                            Id = 2, Name = "variant 2"
                        }
                    }
                };

                var mapper  = new DefaultObjectMapper();
                var context = new MappingContext();

                context.Includes.Add("Variants");

                var target = mapper.Map(source, new TargetProduct_IListVariants(), typeof(SourceProduct), typeof(TargetProduct_IListVariants), null, context) as TargetProduct_IListVariants;

                Assert.Equal(10, target.Id);
                Assert.NotNull(target.Variants);
                Assert.Equal(2, target.Variants.Count);

                Assert.Equal(1, target.Variants.First().Id);
                Assert.Equal("variant 1", target.Variants.First().Name);
                Assert.Equal(2, target.Variants.Skip(1).First().Id);
                Assert.Equal("variant 2", target.Variants.Skip(1).First().Name);
            }
            public void can_map_collections()
            {
                var source = new SourceProduct
                {
                    Id = 10,
                    Variants = new List<SourceProductVariant>
                    {
                        new SourceProductVariant { Id = 1, Name = "variant 1"},
                        new SourceProductVariant { Id = 2, Name = "variant 2"}
                    }
                };

                var mapper = new DefaultObjectMapper();
                var context = new MappingContext(new LocalApiContext(new ApiContext(), null));
                context.Includes.Add("Variants");

                var target = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(10, target.Id);
                Assert.NotNull(target.Variants);
                Assert.Equal(2, target.Variants.Count);

                Assert.Equal(1, target.Variants.First().Id);
                Assert.Equal("variant 1", target.Variants.First().Name);
                Assert.Equal(2, target.Variants.Skip(1).First().Id);
                Assert.Equal("variant 2", target.Variants.Skip(1).First().Name);
            }
            public void can_work_on_different_collection_type_array()
            {
                var source = new SourceProduct
                {
                    Id = 10,
                    Variants = new List<SourceProductVariant>
                    {
                        new SourceProductVariant { Id = 1, Name = "variant 1"},
                        new SourceProductVariant { Id = 2, Name = "variant 2"}
                    }
                };

                var mapper = new DefaultObjectMapper();
                var context = new MappingContext();
                context.Includes.Add("Variants");

                var target = mapper.Map(source, new TargetProduct_ArrayVariants(), typeof(SourceProduct), typeof(TargetProduct_ArrayVariants), null, context) as TargetProduct_ArrayVariants;

                Assert.Equal(10, target.Id);
                Assert.NotNull(target.Variants);
                Assert.Equal(2, target.Variants.Length);

                Assert.Equal(1, target.Variants.First().Id);
                Assert.Equal("variant 1", target.Variants.First().Name);
                Assert.Equal(2, target.Variants.Skip(1).First().Id);
                Assert.Equal("variant 2", target.Variants.Skip(1).First().Name);
            }
示例#4
0
            public void can_customize_nested_object_mapping()
            {
                var source = new SourceProduct
                {
                    Id    = 1,
                    Brand = new SourceBrand
                    {
                        Name = "Nike"
                    }
                };

                var mapper  = new DefaultObjectMapper();
                var context = new MappingContext();

                context.Includes.Add("Brand");

                mapper.GetMapper = (sourceType, targetType) =>
                {
                    if (sourceType == typeof(SourceBrand))
                    {
                        return(new BrandMapper());
                    }

                    return(mapper);
                };

                var target = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal("Nike (Customized)", target.Brand.Name);
            }
示例#5
0
            public void should_ignore_nested_objects_bydefault()
            {
                var source = new SourceProduct
                {
                    Id    = 5,
                    Name  = "Product 1",
                    Brand = new SourceBrand {
                        Id = 10, Name = "Nike"
                    }
                };

                var mapper  = new DefaultObjectMapper();
                var context = new MappingContext();
                var target  = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal("Product 1", target.Name);
                Assert.Null(target.Brand);
            }
示例#6
0
            public void work_on_simple_properties()
            {
                var mapper = new DefaultObjectMapper();
                var source = new SourceProduct
                {
                    Id      = 5,
                    Name    = "Product name",
                    Price   = 150m,
                    BrandId = 10,
                    Type    = SourceProductType.Type2
                };

                var context = new MappingContext();
                var target  = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(5, target.Id);
                Assert.Equal("Product name", target.Name);
                Assert.Equal(150m, target.Price);
                Assert.Equal(TargetProductType.Type2, target.Type);
            }
示例#7
0
            public void can_handle_null_nested_object()
            {
                var source = new SourceProduct
                {
                    Id   = 1024,
                    Name = "1024 Product"
                };

                var mapper   = new DefaultObjectMapper();
                var includes = new IncludeCollection();

                includes.Add("Brand");

                var context = new MappingContext(new LocalApiContext(new ApiContext(), null), includes);
                var target  = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(1024, target.Id);
                Assert.Equal("1024 Product", target.Name);
                Assert.Null(target.Brand);
            }
示例#8
0
            public void ignore_collections_bydefault()
            {
                var source = new SourceProduct
                {
                    Id       = 10,
                    Variants = new List <SourceProductVariant>
                    {
                        new SourceProductVariant {
                            Id = 1, Name = "variant 1"
                        },
                        new SourceProductVariant {
                            Id = 2, Name = "variant 2"
                        }
                    }
                };

                var mapper  = new DefaultObjectMapper();
                var context = new MappingContext();
                var target  = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(10, target.Id);
                Assert.Null(target.Variants);
            }
示例#9
0
            public void can_map_one_level_nested_object()
            {
                var source = new SourceProduct
                {
                    Id    = 5,
                    Name  = "Product name",
                    Brand = new SourceBrand {
                        Id = 10, Name = "Nike"
                    }
                };

                var mapper   = new DefaultObjectMapper();
                var includes = new IncludeCollection();

                includes.Add("Brand");

                var context = new MappingContext(includes);
                var target  = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.NotNull(target.Brand);
                Assert.Equal(10, target.Brand.Id);
                Assert.Equal("Nike", target.Brand.Name);
            }
示例#10
0
            public void should_replace_target_collections()
            {
                var source = new SourceProduct
                {
                    Id       = 10,
                    Variants = new List <SourceProductVariant>
                    {
                        new SourceProductVariant {
                            Id = 1, Name = "variant 1"
                        },
                        new SourceProductVariant {
                            Id = 2, Name = "variant 2"
                        }
                    }
                };

                var mapper  = new DefaultObjectMapper();
                var context = new MappingContext();

                context.Includes.Add("Variants");

                var target = new TargetProduct();

                target.Variants = new List <TargetProductVariant>();
                target.Variants.Add(new TargetProductVariant {
                    Id = 3, Name = "variant 3"
                });

                target = mapper.Map(source, target, typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(2, target.Variants.Count);
                Assert.Equal(1, target.Variants.First().Id);
                Assert.Equal("variant 1", target.Variants.First().Name);
                Assert.Equal(2, target.Variants.Skip(1).First().Id);
                Assert.Equal("variant 2", target.Variants.Skip(1).First().Name);
            }
            public void should_replace_target_collections()
            {
                var source = new SourceProduct
                {
                    Id = 10,
                    Variants = new List<SourceProductVariant>
                    {
                        new SourceProductVariant { Id = 1, Name = "variant 1"},
                        new SourceProductVariant { Id = 2, Name = "variant 2"}
                    }
                };

                var mapper = new DefaultObjectMapper();
                var context = new MappingContext();
                context.Includes.Add("Variants");

                var target = new TargetProduct();
                target.Variants = new List<TargetProductVariant>();
                target.Variants.Add(new TargetProductVariant { Id = 3, Name = "variant 3" });

                target = mapper.Map(source, target, typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(2, target.Variants.Count);
                Assert.Equal(1, target.Variants.First().Id);
                Assert.Equal("variant 1", target.Variants.First().Name);
                Assert.Equal(2, target.Variants.Skip(1).First().Id);
                Assert.Equal("variant 2", target.Variants.Skip(1).First().Name);
            }
            public void can_customize_nested_object_mapping()
            {
                var source = new SourceProduct
                {
                    Id = 1,
                    Brand = new SourceBrand
                    {
                        Name = "Nike"
                    }
                };

                var mapper = new DefaultObjectMapper();
                var context = new MappingContext();
                context.Includes.Add("Brand");

                mapper.GetMapper = (sourceType, targetType) =>
                {
                    if (sourceType == typeof(SourceBrand))
                    {
                        return new BrandMapper();
                    }

                    return mapper;
                };

                var target = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal("Nike (Customized)", target.Brand.Name);
            }
            public void should_ignore_nested_objects_bydefault()
            {
                var source = new SourceProduct
                {
                    Id = 5,
                    Name = "Product 1",
                    Brand = new SourceBrand { Id = 10, Name = "Nike" }
                };

                var mapper = new DefaultObjectMapper();
                var context = new MappingContext();
                var target = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal("Product 1", target.Name);
                Assert.Null(target.Brand);
            }
            public void can_map_one_level_nested_object()
            {
                var source = new SourceProduct
                {
                    Id = 5,
                    Name = "Product name",
                    Brand = new SourceBrand { Id = 10, Name = "Nike" }
                };

                var mapper = new DefaultObjectMapper();
                var includes = new IncludeCollection();
                includes.Add("Brand");

                var context = new MappingContext(includes);
                var target = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.NotNull(target.Brand);
                Assert.Equal(10, target.Brand.Id);
                Assert.Equal("Nike", target.Brand.Name);
            }
            public void can_handle_null_nested_object()
            {
                var source = new SourceProduct
                {
                    Id = 1024,
                    Name = "1024 Product"
                };

                var mapper = new DefaultObjectMapper();
                var includes = new IncludeCollection();
                includes.Add("Brand");

                var context = new MappingContext(new LocalApiContext(new ApiContext(), null), includes);
                var target = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(1024, target.Id);
                Assert.Equal("1024 Product", target.Name);
                Assert.Null(target.Brand);
            }
            public void work_on_simple_properties()
            {
                var mapper = new DefaultObjectMapper();
                var source = new SourceProduct
                {
                    Id = 5,
                    Name = "Product name",
                    Price = 150m,
                    BrandId = 10,
                    Type = SourceProductType.Type2
                };

                var context = new MappingContext();
                var target = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(5, target.Id);
                Assert.Equal("Product name", target.Name);
                Assert.Equal(150m, target.Price);
                Assert.Equal(TargetProductType.Type2, target.Type);
            }
            public void ignore_collections_bydefault()
            {
                var source = new SourceProduct
                {
                    Id = 10,
                    Variants = new List<SourceProductVariant>
                    {
                        new SourceProductVariant { Id = 1, Name = "variant 1"},
                        new SourceProductVariant { Id = 2, Name = "variant 2"}
                    }
                };

                var mapper = new DefaultObjectMapper();
                var context = new MappingContext();
                var target = mapper.Map(source, new TargetProduct(), typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(10, target.Id);
                Assert.Null(target.Variants);
            }