示例#1
0
 /// <summary>
 /// Fills the DTO from entity.
 /// </summary>
 /// <param name="fromEntity">From entity.</param>
 /// <param name="toDTO">To DTO.</param>
 public static void FillDTOFromEntity(EntityObject fromEntity, IDTO toDTO)
 {
     FillData(toDTO, fromEntity, false);
 }
示例#2
0
 /// <summary>
 /// Fills the entity from DTO.
 /// </summary>
 /// <param name="fromDTO">From DTO.</param>
 /// <param name="toEntity">To entity.</param>
 public static void FillEntityFromDTO(IDTO fromDTO, EntityObject toEntity)
 {
     FillData(fromDTO, toEntity, true);
 }
示例#3
0
        /// <summary>
        /// Fills the data.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="entityFromDto">if private set to <c>true</c> [entity from dto].</param>
        private static void FillData(IDTO dto, EntityObject entity, bool entityFromDto)
        {
            var dtoType = dto.GetType();
            var entityType = entity.GetType();
            MappingType mappingType;

            if (!VerifyForEntityType(entityType, dtoType, out mappingType))
            {
                throw new EntityConversionException(string.Format(Thread.CurrentThread.CurrentCulture, "Entity type '{0}' must match with type specified in EntityMappingAttribute on type '{1}' !", entityType.ToString(), dtoType.ToString()));
            }

            var properties = dtoType.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                bool skipThisProperty = false;
                object[] customAttributes = property.GetCustomAttributes(typeof(EntityPropertyMappingAttribute), false);
                if (mappingType == MappingType.TotalExplicit && customAttributes.Length == 0)
                {
                    continue;
                }

                foreach (object customAttribute in customAttributes)
                {
                    EntityPropertyMappingAttribute entityPropertyMappingAttribute = (EntityPropertyMappingAttribute)customAttribute;
                    if (entityPropertyMappingAttribute.MappingDirection == MappingDirectionType.None)
                    {
                        skipThisProperty = true;
                        break;
                    }
                }

                if (skipThisProperty)
                {
                    continue;
                }

                var entityPropertyName = GetEntityPropertyName(property, mappingType, entityFromDto);
                if (!string.IsNullOrEmpty(entityPropertyName))
                {
                    var entityProperty = entityType.GetProperty(entityPropertyName);

                    if (entityProperty == null)
                    {
                        throw new EntityConversionException(entityPropertyName, entity);
                    }

                    var sourceProperty = entityFromDto ? property : entityProperty;
                    var destinationProperty = entityFromDto ? entityProperty : property;
                    var sourceObject = entityFromDto ? (dto as object) : (entity as object);
                    var destinationObject = entityFromDto ? (entity as object) : (dto as object);
                    var sourceValue = sourceProperty.GetValue(sourceObject, null);

                    if (destinationProperty.CanWrite)
                    {
                        if (sourceProperty.PropertyType.IsEnum && destinationProperty.PropertyType == typeof(byte))
                        {
                            sourceValue = (byte)(int)sourceValue;
                        }

                        destinationProperty.SetValue(destinationObject, sourceValue, null);
                    }
                }
            }
        }