Example #1
0
 /// <summary>
 /// Fills the DTO from viewModel.
 /// </summary>
 /// <param name="toDTO">To dto.</param>
 /// <param name="fromViewModel">from view model.</param>
 public static void FillDTOFromViewModel(IDTO toDTO, ViewModelBase fromViewModel)
 {
     FillData(fromViewModel, toDTO, false);
 }
Example #2
0
 /// <summary>
 /// Fills the viewModel from DTO.
 /// </summary>
 /// <param name="toViewModel">
 /// The View Model to copy to.
 /// </param>
 /// <param name="fromDTO">
 /// The DTO to copy from.
 /// </param>
 public static void FillViewModelFromDTO(ViewModelBase toViewModel, IDTO fromDTO)
 {
     FillData(toViewModel, fromDTO, true);
 }
Example #3
0
        /// <summary>
        /// Fills the data.
        /// </summary>
        /// <param name="viewModel">The dto.</param>
        /// <param name="dto">The viewModel.</param>
        /// <param name="viewModelFromDto">if private set to <c>true</c> [viewModel from dto].</param>
        private static void FillData(ViewModelBase viewModel, IDTO dto, bool viewModelFromDto)
        {
            var dtoType = dto.GetType();
            var viewModelType = viewModel.GetType();
            MappingType mappingType;

            if (!VerifyForViewModelType(viewModelType, dtoType, out mappingType))
            {
                throw new DTOConversionException(string.Format(Thread.CurrentThread.CurrentCulture, "ViewModel type '{0}' must match with type specified in ViewModelMappingAttribute on type '{1}' !", viewModelType.ToString(), dtoType.ToString()));
            }

            var properties = dtoType.GetProperties();

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

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

                if (skipThisProperty)
                {
                    continue;
                }

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

                    if (entityProperty == null)
                    {
                        throw new DTOConversionException(entityPropertyName, dto);
                    }

                    var sourceProperty = viewModelFromDto ? property : entityProperty;
                    var destinationProperty = viewModelFromDto ? entityProperty : property;
                    var sourceObject = viewModelFromDto ? (object)dto : (object)viewModel;
                    var destinationObject = viewModelFromDto ? (viewModel 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);
                    }
                }
            }
        }