Esempio n. 1
0
        private Dtobase Filter(Dtobase item, Type type, byte level)
        {
            try
            {
                if (null == item)
                {
                    return(null);
                }

                Dtobase        dto        = (Dtobase)Activator.CreateInstance(type);
                PropertyMeta[] properties = this.context.GetPropertyDefs(type);
                for (int i = 0; i < properties.Length; i++)
                {
                    PropertyMeta property = properties[i];
                    if (!property.Valid || !property.HasLevel(level))
                    {
                        continue;
                    }

                    Object value = property.Read(item);
                    if (PropertyMeta.ClassType.Value == property.PropertyClassType ||
                        PropertyMeta.ClassType.CustomType == property.PropertyClassType ||
                        PropertyMeta.ClassType.CustomTypeCollection == property.PropertyClassType)
                    {
                        property.Write(dto, value);
                    }
                    else if (PropertyMeta.ClassType.Class == property.PropertyClassType)
                    {
                        byte nestedLevel = property.DtoNestedLevel;
                        if (level != property.DtoLevel)
                        {
                            nestedLevel = (byte)(nestedLevel + level - property.DtoLevel);
                        }

                        property.Write(dto,
                                       this.Filter((Dtobase)value, property.PropertyClass, nestedLevel));
                    }
                    else if (PropertyMeta.ClassType.Collection == property.PropertyClassType)
                    {
                        byte nestedLevel = property.DtoNestedLevel;
                        if (level != property.DtoLevel)
                        {
                            nestedLevel = (byte)(nestedLevel + level - property.DtoLevel);
                        }

                        property.Write(dto,
                                       this.FilterList((IEnumerable)value, property.InnerGenericClass, nestedLevel));
                    }
                }

                return(dto);
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "Filtering failed. Type:'" + item.GetType().Name +
                          "'. Level:'" + level + "'", ex);
            }
        }
Esempio n. 2
0
        private void MapTo(Dtobase source, Dtobase dest, Type type)
        {
            if (null == source)
            {
                return;
            }

            try
            {
                PropertyMeta[] properties = this.context.GetPropertyDefs(type);
                for (int i = 0; i < properties.Length; i++)
                {
                    PropertyMeta property = properties[i];
                    if (!property.Valid || !property.IsAssigned(source))
                    {
                        continue;
                    }

                    Object sourceValue = property.Read(source);
                    Object destValue   = property.Read(dest);

                    if (null == sourceValue ||
                        null == destValue ||
                        PropertyMeta.ClassType.Value == property.PropertyClassType ||
                        PropertyMeta.ClassType.CustomType == property.PropertyClassType ||
                        PropertyMeta.ClassType.CustomTypeCollection == property.PropertyClassType)
                    {
                        property.Write(dest, sourceValue);
                    }
                    else if (PropertyMeta.ClassType.Class == property.PropertyClassType)
                    {
                        if (object.Equals(sourceValue, destValue))
                        {
                            this.MapTo((Dtobase)sourceValue, (Dtobase)destValue, sourceValue.GetType());
                        }
                        else
                        {
                            property.Write(dest, sourceValue);
                        }
                    }
                    else if (PropertyMeta.ClassType.Collection == property.PropertyClassType)
                    {
                        IEnumerable destList   = (IEnumerable)destValue;
                        IEnumerable sourceList = (IEnumerable)sourceValue;
                        this.MapToCollection(sourceList, destList, source);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(
                          "Mapping failed from '" + source.GetType().Name +
                          "' to '" + dest.GetType().Name + "'", ex);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Parses JSON data into domain object
        /// </summary>
        /// <param name="node">JSON tree</param>
        /// <param name="type">domain object type</param>
        /// <returns>parsed domain object instance</returns>
        public object Parse(JToken node, Type type)
        {
            try
            {
                Dtobase dto = (Dtobase)Activator.CreateInstance(type);
                if (!node.HasValues)
                {
                    return(dto);
                }

                PropertyMeta[] properties = this.context.GetPropertyDefs(type);
                for (int i = 0; i < properties.Length; i++)
                {
                    PropertyMeta propertyDef = properties[i];
                    if (!propertyDef.Valid)
                    {
                        continue;
                    }

                    JToken propertyValue = node[propertyDef.Name];
                    if (null != propertyValue)
                    {
                        try
                        {
                            if (PropertyMeta.ClassType.Value == propertyDef.PropertyClassType)
                            {
                                dto.AddAssignedField(propertyDef.Name);
                                propertyDef.Write(dto, this.ParseValue(propertyValue, propertyDef));
                            }
                            else if (PropertyMeta.ClassType.Class == propertyDef.PropertyClassType ||
                                     PropertyMeta.ClassType.CustomType == propertyDef.PropertyClassType)
                            {
                                dto.AddAssignedField(propertyDef.Name);
                                propertyDef.Write(dto, this.Parse(propertyValue, propertyDef.PropertyClass));
                            }
                            else if (PropertyMeta.ClassType.Collection == propertyDef.PropertyClassType ||
                                     PropertyMeta.ClassType.CustomTypeCollection == propertyDef.PropertyClassType)
                            {
                                IList list = (IList)this.context.BuildList(propertyDef.InnerGenericClass);
                                foreach (JToken item in propertyValue)
                                {
                                    list.Add(this.Parse(item, propertyDef.InnerGenericClass));
                                }

                                propertyDef.Write(dto, list);
                                dto.AddAssignedField(propertyDef.Name);
                            }
                        }
                        catch
                        {
                            dto.AddWrongField(propertyDef.Name);
                        }
                    }
                }

                return(dto);
            }
            catch (Exception ex)
            {
                throw new Exception("JSON parsing failed", ex);
            }
        }