Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        private void Map(Dtobase source, Type type)
        {
            PropertyMeta property = null;

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

                    object sourceValue = property.Read(source);
                    if (null != sourceValue)
                    {
                        if (PropertyMeta.ClassType.Class == property.PropertyClassType)
                        {
                            this.Map((Dtobase)sourceValue, property.PropertyClass);
                        }
                        else if (PropertyMeta.ClassType.Collection == property.PropertyClassType)
                        {
                            IEnumerable sourceSet = (IEnumerable)sourceValue;
                            foreach (object item in sourceSet)
                            {
                                this.Map((Dtobase)item, property.InnerGenericClass);
                                if (item is IChild)
                                {
                                    (item as IChild).ConnectToParent(source);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Mapping failed. Type:'" + source.GetType().Name + "'. Name:'" + property.Name + "'", ex);
            }
        }