예제 #1
0
        public object MapTo(object mapped, CommerceEntity entity)
        {
            MappingType mappingType = MappingInfo.Mapping;
            object      value       = entity.GetPropertyValue(FieldName);

            if (MappingInfo.FromConverter != null)
            {
                value = ConverterLazyRegistry.Instance.Resolve(MappingInfo.FromConverter).Convert(value);
            }
            else if (value != null)
            {
                if (mappingType == MappingType.Property)
                {
                    if (value.GetType() != Info.PropertyType)
                    {
                        value = Convert.ChangeType(value, Info.PropertyType);
                    }
                }
                else if (mappingType == MappingType.Identity)
                {
                    if (MappingInfo.FromConverter == null && !String.IsNullOrEmpty(value.ToString()))
                    {
                        value = new Guid(value.ToString());
                    }
                    else
                    {
                        value = null;
                    }
                }
                else
                {
                    var mapper = new Mapper();

                    var relationshipList = value as CommerceRelationshipList;

                    if (relationshipList != null)
                    {
                        value = Activator.CreateInstance(GeneratedListType);

                        foreach (var relation in relationshipList)
                        {
                            object element = mapper.Map(relation.Target, ListElementType);

                            AddMethod.Invoke(value, new[] { element });
                        }
                    }
                    else
                    {
                        var relationship = value as CommerceRelationship;
                        if (relationship != null)
                        {
                            value = mapper.Map(relationship.Target, Info.PropertyType);
                        }
                    }
                }
            }


            if (value != null || (ReflectionHelper.IsNullableType(Info.PropertyType) || !Info.PropertyType.IsValueType))
            {
                Info.SetValue(mapped, value, null);
            }

            return(mapped);
        }