protected override void MapProperty(System.Reflection.PropertyInfo targetProperty, object source, object target, Type sourceType, Type targetType, string propertyPath, MappingContext context)
        {
            var model = target as ProductVariant;
            var variant = source as Kooboo.Commerce.Products.ProductVariant;

            if (targetProperty.Name == "VariantFields" && context.Includes.Includes(propertyPath))
            {
                var product = context.ApiContext.Database.Repository<Kooboo.Commerce.Products.Product>().Find(variant.ProductId);
                var productType = product.ProductType;

                var controls = FormControls.Controls().ToList();

                foreach (var definition in productType.VariantFieldDefinitions)
                {
                    var field = variant.VariantFields.FirstOrDefault(f => f.FieldName == definition.Name);
                    var fieldModel = new CustomField
                    {
                        FieldName = definition.Name,
                        FieldLabel = productType.GetText("VariantFieldDefinitions[" + definition.Name + "].Label", context.ApiContext.Culture),
                        FieldValue = field == null ? null : field.FieldValue
                    };

                    if (String.IsNullOrEmpty(fieldModel.FieldLabel))
                    {
                        fieldModel.FieldLabel = definition.Label;
                    }

                    if (field != null)
                    {
                        var control = controls.Find(c => c.Name == definition.ControlType);
                        if (!control.IsSelectionList && !control.IsValuesPredefined)
                        {
                            fieldModel.FieldText = variant.GetText("VariantFields[" + definition.Name + "]", context.ApiContext.Culture);
                        }
                        else
                        {
                            if (control.IsSelectionList)
                            {
                                fieldModel.FieldText = productType.GetText("VariantFieldDefinitions[" + definition.Name + "].SelectionItems[" + fieldModel.FieldValue + "]", context.ApiContext.Culture);
                            }
                            else
                            {
                                fieldModel.FieldText = productType.GetText("VariantFieldDefinitions[" + definition.Name + "].DefaultValue", context.ApiContext.Culture);
                            }
                        }
                    }

                    if (String.IsNullOrEmpty(fieldModel.FieldText))
                    {
                        fieldModel.FieldText = fieldModel.FieldValue;
                    }

                    model.VariantFields.Add(fieldModel);
                }
            }
            else
            {
                base.MapProperty(targetProperty, source, target, sourceType, targetType, propertyPath, context);
            }
        }
        public override object Map(object source, object target, Type sourceType, Type targetType, string prefix, MappingContext context)
        {
            var model = base.Map(source, target, sourceType, targetType, prefix, context) as ProductVariant;
            var variant = source as Kooboo.Commerce.Products.ProductVariant;

            // Final price
            var shoppingContext = new Kooboo.Commerce.Carts.ShoppingContext
            {
                Culture = context.ApiContext.Culture.Name
            };

            if (!context.ApiContext.Customer.IsGuest())
            {
                var service = new Kooboo.Commerce.Customers.CustomerService(context.ApiContext.Instance);
                var customer = service.FindByEmail(context.ApiContext.Customer.Email);
                if (customer != null)
                {
                    shoppingContext.CustomerId = customer.Id;
                }
            }

            model.FinalPrice = variant.GetFinalPrice(shoppingContext);

            return model;
        }
            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);
            }
        public override object Map(object source, object target, Type sourceType, Type targetType, string prefix, MappingContext context)
        {
            var model = base.Map(source, target, sourceType, targetType, prefix, context) as ShoppingCart;
            var cart = source as Core.ShoppingCart;

            // Update cart prices
            var priceContext = new Core.ShoppingCartService(context.ApiContext.Instance).CalculatePrice(cart, null);
            if (model.Items != null && model.Items.Count > 0)
            {
                foreach (var priceItem in priceContext.Items)
                {
                    var cartItem = model.Items.FirstOrDefault(it => it.Id == priceItem.ItemId);
                    cartItem.Subtotal = priceItem.Subtotal;
                    cartItem.Discount = priceItem.Discount;
                    cartItem.Total = priceItem.Subtotal - priceItem.Discount;
                }
            }

            model.ShippingCost = priceContext.ShippingCost;
            model.PaymentMethodCost = priceContext.PaymentMethodCost;
            model.Tax = priceContext.Tax;

            model.Subtotal = priceContext.Subtotal;
            model.TotalDiscount = priceContext.TotalDiscount;
            model.Total = priceContext.Total;

            return model;
        }
Exemple #6
0
        protected override bool IsComplexPropertyIncluded(System.Reflection.PropertyInfo property, string propertyPath, MappingContext context)
        {
            if (property.Name == "Children" && context.Includes.Includes("Subtrees"))
            {
                return true;
            }

            return base.IsComplexPropertyIncluded(property, propertyPath, context);
        }
Exemple #7
0
        public override object Map(object source, object target, Type sourceType, Type targetType, string prefix, MappingContext context)
        {
            var model = base.Map(source, target, sourceType, targetType, prefix, context) as Product;
            var product = source as Kooboo.Commerce.Products.Product;

            model.SkuAlias = product.ProductType.SkuAlias;
            model.Prices = new PriceRange(product.LowestPrice, product.HighestPrice);

            return model;
        }
        public virtual object Map(object source, object target, Type sourceType, Type targetType, string prefix, MappingContext context)
        {
            foreach (var targetProp in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                var propPath = prefix + targetProp.Name;
                MapProperty(targetProp, source, target, sourceType, targetType, propPath, context);
            }

            return target;
        }
Exemple #9
0
        public override object Map(object source, object target, Type sourceType, Type targetType, string prefix, MappingContext context)
        {
            // Parent ID
            var model = base.Map(source, target, sourceType, targetType, prefix, context) as Category;
            var category = source as Core.CategoryTreeNode;
            if (category.Parent != null)
            {
                model.ParentId = category.Parent.Id;
            }

            // Localization
            var texts = Localizer.GetText(new EntityKey(typeof(Core.Category), model.Id), new[] { "Name", "Description" }, context.ApiContext.Culture);
            model.Name = texts["Name"] ?? model.Name;
            model.Description = texts["Description"] ?? model.Description;

            return model;
        }
        public override object Map(object source, object target, Type sourceType, Type targetType, string prefix, MappingContext context)
        {
            var model = base.Map(source, target, sourceType, targetType, prefix, context) as ShoppingCartItem;
            var cartItem = source as Kooboo.Commerce.Carts.ShoppingCartItem;

            // Product
            var mapper = GetMapperOrDefault(typeof(Kooboo.Commerce.Products.Product), typeof(Product));
            var propPath = prefix + "Product";
            var includes = new IncludeCollection();
            foreach (var each in context.Includes)
            {
                if (each.StartsWith(propPath) && each.Length > propPath.Length && each[propPath.Length] == '.')
                {
                    includes.Add(each.Substring(propPath.Length + 1));
                }
            }

            model.Product = mapper.Map(cartItem.ProductVariant.Product, new Product(), null, new MappingContext(context.ApiContext, includes)) as Product;

            return model;
        }
        public override object Resolve(PropertyInfo property, object container, MappingContext context)
        {
            var value = base.Resolve(property, container, context);

            if (property.PropertyType != typeof(String))
            {
                return value;
            }

            var localizable = container as ILocalizable;

            if (localizable != null && property.IsDefined(typeof(LocalizableAttribute), false))
            {
                var localizedValue = localizable.GetText(property.Name, context.ApiContext.Culture);
                if (!String.IsNullOrEmpty(localizedValue))
                {
                    return localizedValue;
                }
            }

            return value;
        }
            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_multi_level_nested_objects()
            {
                var source = new SourceOrderItem
                {
                    Id = 1,
                    Product = new SourceProduct
                    {
                        Id = 11,
                        Name = "Product 1",
                        Brand = new SourceBrand { Id = 21, Name = "Brand 1" }
                    }
                };

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

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

                Assert.Equal(1, target.Id);
                Assert.NotNull(target.Product);
                Assert.Equal(11, target.Product.Id);
                Assert.NotNull(target.Product.Brand);
                Assert.Equal(21, target.Product.Brand.Id);
                Assert.Equal("Brand 1", target.Product.Brand.Name);
            }
            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 virtual object Resolve(PropertyInfo property, object container, MappingContext context)
 {
     return property.GetValue(container, null);
 }
 private object ResolveSourcePropertyValue(PropertyInfo property, object source, MappingContext context)
 {
     return SourcePropertyValueResolver.Resolve(property, source, context);
 }
            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 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);
            }
Exemple #19
0
 protected virtual bool IsComplexPropertyIncluded(PropertyInfo property, string propertyPath, MappingContext context)
 {
     return(context.Includes.Includes(propertyPath));
 }
Exemple #20
0
        protected virtual void MapProperty(PropertyInfo targetProperty, object source, object target, Type sourceType, Type targetType, string propertyPath, MappingContext context)
        {
            var sourceProperty = sourceType.GetProperty(targetProperty.Name, BindingFlags.Public | BindingFlags.Instance);

            if (sourceProperty == null)
            {
                return;
            }

            var sourcePropValue = ResolveSourcePropertyValue(sourceProperty, source, context);

            if (sourcePropValue == null)
            {
                if (targetProperty.CanWrite)
                {
                    targetProperty.SetValue(target, null, null);
                }

                return;
            }

            if (IsComplexType(targetProperty.PropertyType))
            {
                if (!IsComplexPropertyIncluded(targetProperty, propertyPath, context))
                {
                    return;
                }

                MapComplexProperty(targetProperty, sourceProperty, sourcePropValue, source, target, sourceType, targetType, propertyPath, context);
            }
            else
            {
                MapSimpleProperty(targetProperty, sourceProperty, sourcePropValue, source, target, sourceType, targetType, propertyPath, context);
            }
        }
Exemple #21
0
        protected virtual void MapComplexProperty(PropertyInfo targetProperty, PropertyInfo sourceProperty, object sourcePropValue, object source, object target, Type sourceType, Type targetType, string propertyPath, MappingContext context)
        {
            object targetPropValue = targetProperty.GetValue(target, null);

            if (context.VisitedObjects.Contains(targetPropValue))
            {
                return;
            }

            var targetPropTypeInfo = ModelTypeInfo.GetTypeInfo(targetProperty.PropertyType);

            if (targetPropTypeInfo.IsCollection)
            {
                var sourcePropTypeInfo = ModelTypeInfo.GetTypeInfo(sourceProperty.PropertyType);

                if (targetPropTypeInfo.IsDictionary)
                {
                    if (sourcePropTypeInfo.IsDictionary)
                    {
                        // Map between dictionaries
                        var targetDic = Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(targetPropTypeInfo.DictionaryKeyType, targetPropTypeInfo.DictionaryValueType)) as IDictionary;
                        targetProperty.SetValue(target, targetDic, null);

                        var sourceDic = sourcePropValue as IDictionary;
                        foreach (var key in sourceDic.Keys)
                        {
                            targetDic.Add(key, sourceDic[key]);
                        }
                    }
                    else if (sourcePropTypeInfo.IsCollection)
                    {
                        // source collection element: Name or Key field -> dictionary key, Value field -> dictionary value
                        var keyProp = sourcePropTypeInfo.ElementType.GetProperty("Key", BindingFlags.Public | BindingFlags.Instance);
                        if (keyProp == null)
                        {
                            keyProp = sourcePropTypeInfo.ElementType.GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
                        }

                        if (keyProp != null)
                        {
                            var valueProp = sourcePropTypeInfo.ElementType.GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
                            if (valueProp != null)
                            {
                                var targetDic = Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(targetPropTypeInfo.DictionaryKeyType, targetPropTypeInfo.DictionaryValueType)) as IDictionary;
                                foreach (var item in sourcePropValue as IEnumerable)
                                {
                                    var key   = keyProp.GetValue(item, null);
                                    var value = valueProp.GetValue(item, null);
                                    targetDic.Add(key, value);
                                }

                                targetProperty.SetValue(target, targetDic, null);
                            }
                        }
                    }
                }
                else // List or Array
                {
                    var sourceElementType = sourcePropTypeInfo.ElementType;

                    var elementMapper = GetMapperOrDefault(sourceElementType, targetPropTypeInfo.ElementType);
                    if (elementMapper == null)
                    {
                        return;
                    }

                    var targetList = Activator.CreateInstance(typeof(List <>).MakeGenericType(targetPropTypeInfo.ElementType));
                    var addMethod  = targetList.GetType().GetMethod("Add", BindingFlags.Public | BindingFlags.Instance, null, new[] { targetPropTypeInfo.ElementType }, null);

                    var sourceList    = sourcePropValue as IEnumerable;
                    var elementPrefix = propertyPath + ".";
                    var totalElements = 0;

                    foreach (var sourceElement in sourceList)
                    {
                        var targetElement = elementMapper.Map(sourceElement, Activator.CreateInstance(targetPropTypeInfo.ElementType), sourceElementType, targetPropTypeInfo.ElementType, elementPrefix, context);
                        addMethod.Invoke(targetList, new[] { targetElement });
                        totalElements++;
                    }

                    if (!Object.ReferenceEquals(targetList, targetPropValue))
                    {
                        if (targetProperty.PropertyType.IsArray)
                        {
                            var array = Array.CreateInstance(targetPropTypeInfo.ElementType, totalElements);
                            var i     = 0;
                            foreach (var item in targetList as IEnumerable)
                            {
                                array.SetValue(item, i);
                                i++;
                            }

                            targetProperty.SetValue(target, array, null);
                        }
                        else
                        {
                            targetProperty.SetValue(target, targetList, null);
                        }
                    }
                }
            }
            else
            {
                var mapper = GetMapperOrDefault(sourceProperty.PropertyType, targetProperty.PropertyType);
                if (mapper != null)
                {
                    if (targetPropValue == null)
                    {
                        targetPropValue = Activator.CreateInstance(targetProperty.PropertyType);
                    }

                    targetPropValue = mapper.Map(sourcePropValue, targetPropValue, sourceProperty.PropertyType, targetProperty.PropertyType, propertyPath + ".", context);

                    targetProperty.SetValue(target, targetPropValue, null);
                }
            }
        }
Exemple #22
0
 public virtual object Resolve(PropertyInfo property, object container, MappingContext context)
 {
     return(property.GetValue(container, null));
 }
        protected virtual void MapComplexProperty(PropertyInfo targetProperty, PropertyInfo sourceProperty, object sourcePropValue, object source, object target, Type sourceType, Type targetType, string propertyPath, MappingContext context)
        {
            object targetPropValue = targetProperty.GetValue(target, null);

            if (context.VisitedObjects.Contains(targetPropValue))
            {
                return;
            }

            var targetPropTypeInfo = ModelTypeInfo.GetTypeInfo(targetProperty.PropertyType);

            if (targetPropTypeInfo.IsCollection)
            {
                var sourcePropTypeInfo = ModelTypeInfo.GetTypeInfo(sourceProperty.PropertyType);

                if (targetPropTypeInfo.IsDictionary)
                {
                    if (sourcePropTypeInfo.IsDictionary)
                    {
                        // Map between dictionaries
                        var targetDic = Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(targetPropTypeInfo.DictionaryKeyType, targetPropTypeInfo.DictionaryValueType)) as IDictionary;
                        targetProperty.SetValue(target, targetDic, null);

                        var sourceDic = sourcePropValue as IDictionary;
                        foreach (var key in sourceDic.Keys)
                        {
                            targetDic.Add(key, sourceDic[key]);
                        }
                    }
                    else if (sourcePropTypeInfo.IsCollection)
                    {
                        // source collection element: Name or Key field -> dictionary key, Value field -> dictionary value
                        var keyProp = sourcePropTypeInfo.ElementType.GetProperty("Key", BindingFlags.Public | BindingFlags.Instance);
                        if (keyProp == null)
                        {
                            keyProp = sourcePropTypeInfo.ElementType.GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
                        }

                        if (keyProp != null)
                        {
                            var valueProp = sourcePropTypeInfo.ElementType.GetProperty("Value", BindingFlags.Public | BindingFlags.Instance);
                            if (valueProp != null)
                            {
                                var targetDic = Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(targetPropTypeInfo.DictionaryKeyType, targetPropTypeInfo.DictionaryValueType)) as IDictionary;
                                foreach (var item in sourcePropValue as IEnumerable)
                                {
                                    var key = keyProp.GetValue(item, null);
                                    var value = valueProp.GetValue(item, null);
                                    targetDic.Add(key, value);
                                }

                                targetProperty.SetValue(target, targetDic, null);
                            }
                        }
                    }
                }
                else // List or Array
                {
                    var sourceElementType = sourcePropTypeInfo.ElementType;

                    var elementMapper = GetMapperOrDefault(sourceElementType, targetPropTypeInfo.ElementType);
                    if (elementMapper == null)
                    {
                        return;
                    }

                    var targetList = Activator.CreateInstance(typeof(List<>).MakeGenericType(targetPropTypeInfo.ElementType));
                    var addMethod = targetList.GetType().GetMethod("Add", BindingFlags.Public | BindingFlags.Instance, null, new[] { targetPropTypeInfo.ElementType }, null);

                    var sourceList = sourcePropValue as IEnumerable;
                    var elementPrefix = propertyPath + ".";
                    var totalElements = 0;

                    foreach (var sourceElement in sourceList)
                    {
                        var targetElement = elementMapper.Map(sourceElement, Activator.CreateInstance(targetPropTypeInfo.ElementType), sourceElementType, targetPropTypeInfo.ElementType, elementPrefix, context);
                        addMethod.Invoke(targetList, new[] { targetElement });
                        totalElements++;
                    }

                    if (!Object.ReferenceEquals(targetList, targetPropValue))
                    {
                        if (targetProperty.PropertyType.IsArray)
                        {
                            var array = Array.CreateInstance(targetPropTypeInfo.ElementType, totalElements);
                            var i = 0;
                            foreach (var item in targetList as IEnumerable)
                            {
                                array.SetValue(item, i);
                                i++;
                            }

                            targetProperty.SetValue(target, array, null);
                        }
                        else
                        {
                            targetProperty.SetValue(target, targetList, null);
                        }
                    }
                }
            }
            else
            {
                var mapper = GetMapperOrDefault(sourceProperty.PropertyType, targetProperty.PropertyType);
                if (mapper != null)
                {
                    if (targetPropValue == null)
                    {
                        targetPropValue = Activator.CreateInstance(targetProperty.PropertyType);
                    }

                    targetPropValue = mapper.Map(sourcePropValue, targetPropValue, sourceProperty.PropertyType, targetProperty.PropertyType, propertyPath + ".", context);

                    targetProperty.SetValue(target, targetPropValue, null);
                }
            }
        }
 protected virtual bool IsComplexPropertyIncluded(PropertyInfo property, string propertyPath, MappingContext context)
 {
     return context.Includes.Includes(propertyPath);
 }
        protected virtual void MapProperty(PropertyInfo targetProperty, object source, object target, Type sourceType, Type targetType, string propertyPath, MappingContext context)
        {
            var sourceProperty = sourceType.GetProperty(targetProperty.Name, BindingFlags.Public | BindingFlags.Instance);
            if (sourceProperty == null)
            {
                return;
            }

            var sourcePropValue = ResolveSourcePropertyValue(sourceProperty, source, context);
            if (sourcePropValue == null)
            {
                if (targetProperty.CanWrite)
                {
                    targetProperty.SetValue(target, null, null);
                }

                return;
            }

            if (IsComplexType(targetProperty.PropertyType))
            {
                if (!IsComplexPropertyIncluded(targetProperty, propertyPath, context))
                {
                    return;
                }

                MapComplexProperty(targetProperty, sourceProperty, sourcePropValue, source, target, sourceType, targetType, propertyPath, context);
            }
            else
            {
                MapSimpleProperty(targetProperty, sourceProperty, sourcePropValue, source, target, sourceType, targetType, propertyPath, context);
            }
        }
            public void can_customize_collection_element_mapping()
            {
                var source = new SourceBrandCollection
                {
                    Brands = new List<SourceBrand> {
                        new SourceBrand { Name = "Nike" },
                        new SourceBrand { Name = "Puma" }
                    }
                };

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

                    return mapper;
                };

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

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

                Assert.Equal(2, target.Brands.Count);
                Assert.Equal("Nike (Customized)", target.Brands.First().Name);
                Assert.Equal("Puma (Customized)", target.Brands.Skip(1).First().Name);
            }
            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);
            }
Exemple #28
0
 private object ResolveSourcePropertyValue(PropertyInfo property, object source, MappingContext context)
 {
     return(SourcePropertyValueResolver.Resolve(property, source, context));
 }
            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 override object Map(object source, object target, Type sourceType, Type targetType, string prefix, MappingContext context)
 {
     var brand = base.Map(source, target, sourceType, targetType, prefix, context) as TargetBrand;
     brand.Name += " (Customized)";
     return brand;
 }
Exemple #31
0
        public virtual object Map(object source, object target, Type sourceType, Type targetType, string prefix, MappingContext context)
        {
            foreach (var targetProp in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                var propPath = prefix + targetProp.Name;
                MapProperty(targetProp, source, target, sourceType, targetType, propPath, context);
            }

            return(target);
        }
Exemple #32
0
        protected virtual void MapSimpleProperty(PropertyInfo targetProperty, PropertyInfo sourceProperty, object sourcePropValue, object source, object target, Type sourceType, Type targetType, string propertyPath, MappingContext context)
        {
            if (!targetProperty.CanWrite)
            {
                return;
            }

            var targetPropValue = sourcePropValue;

            if (targetProperty.PropertyType.IsEnum)
            {
                targetPropValue = Enum.Parse(targetProperty.PropertyType, sourcePropValue.ToString(), true);
            }

            targetProperty.SetValue(target, sourcePropValue, null);
        }
            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);
            }
Exemple #34
0
 public static object Map(this IObjectMapper mapper, object source, object target, string prefix, MappingContext context)
 {
     return mapper.Map(source, target, TypeHelper.GetType(source), target.GetType(), prefix, context);
 }
        protected virtual void MapSimpleProperty(PropertyInfo targetProperty, PropertyInfo sourceProperty, object sourcePropValue, object source, object target, Type sourceType, Type targetType, string propertyPath, MappingContext context)
        {
            if (!targetProperty.CanWrite)
            {
                return;
            }

            var targetPropValue = sourcePropValue;
            if (targetProperty.PropertyType.IsEnum)
            {
                targetPropValue = Enum.Parse(targetProperty.PropertyType, sourcePropValue.ToString(), true);
            }

            targetProperty.SetValue(target, sourcePropValue, null);
        }