private static void FillData(IDTO dto, EntityBase entity, bool populateEntityFromDTO) { var dtoType = dto.GetType(); var entityType = entity.GetType(); if (!VerifyValidEntityType(entityType, dtoType)) { throw new EntityConversionException(); } var properties = dtoType.GetProperties(); var sourceObject = populateEntityFromDTO ? (dto as object) : (entity as object); var destinationObject = populateEntityFromDTO ? (entity as object) : (dto as object); foreach (PropertyInfo property in properties) { var attributes = property.GetCustomAttributes(typeof(EntityPropertyMappingAttribute), false); if (attributes.Length == 0) { continue; } bool skipThisProperty = false; foreach (object customAttribute in attributes) { EntityPropertyMappingAttribute entityPropertyMappingAttribute = (EntityPropertyMappingAttribute)customAttribute; if (entityPropertyMappingAttribute.MappingDirection == MappingDirectionType.None || (populateEntityFromDTO && entityPropertyMappingAttribute.MappingDirection == MappingDirectionType.DTOFromEntity) || (!populateEntityFromDTO && entityPropertyMappingAttribute.MappingDirection == MappingDirectionType.EntityFromDTO)) { skipThisProperty = true; break; } } if (skipThisProperty) { continue; } var entityProperty = entityType.GetProperty(property.Name); var sourceProperty = populateEntityFromDTO ? property : entityProperty; var destinationProperty = populateEntityFromDTO ? entityProperty : property; var sourceValue = sourceProperty.GetValue(sourceObject, null); if (destinationProperty.CanWrite) { destinationProperty.SetValue(destinationObject, sourceValue, null); } } }
public static void FillEntityFromDTO(IDTO dto, Object entity) { Type sourceType = dto.GetType(); Type destinationType = entity.GetType(); foreach (PropertyInfo pSource in sourceType.GetProperties()) { var valueToBeSet = pSource.GetValue(dto); var nameOfEntityPropertyToBeMapped = pSource.GetCustomAttribute <EntityPropertyMappingAttribute>().MappedPropertyname; foreach (PropertyInfo pDestination in destinationType.GetProperties()) { if (pDestination.Name.Equals(nameOfEntityPropertyToBeMapped)) { pDestination.SetValue(entity, valueToBeSet); break; } } } }
/// <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(object 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); } } } }
/// <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); } } } }
/// <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, object 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); bool isComplexProperty = false; DTOType DTOTypeOfComplexProperty = DTOType.Undefined; //var allTypes = Assembly.GetExecutingAssembly(); //var aa = allTypes.GetTypes(); object[] complexPropertyMappingAttributes = property.GetCustomAttributes(typeof(ComplexPropertyMappingAttribute), false); if (complexPropertyMappingAttributes.Length != 0) { if (complexPropertyMappingAttributes.Length > 1) { throw new EntityConversionException(string.Format("More than one {0} is not allowed at {1}.", "ComplexPropertyMappingAttribute", property.ToString())); } ComplexPropertyMappingAttribute complexPropertyMappingAttribute = (ComplexPropertyMappingAttribute)complexPropertyMappingAttributes[0]; isComplexProperty = true; DTOTypeOfComplexProperty = complexPropertyMappingAttribute.DTOType; } if (isComplexProperty) { object sourcePropertyObject = sourceProperty.GetValue(sourceObject); if (sourcePropertyObject != null) { object destinationPropertyObject = destinationProperty.GetValue(destinationObject); if (destinationPropertyObject == null) { object obj = null; if (entityFromDto) { // create the entity object obj = Activator.CreateInstance(destinationProperty.PropertyType); } else { // create the DTO object obj = DTOFactory.Instance.Create(DTOTypeOfComplexProperty, null); } destinationProperty.SetValue(destinationObject, obj, null); destinationPropertyObject = destinationProperty.GetValue(destinationObject); } if (entityFromDto) { FillData((IDTO)sourcePropertyObject, destinationPropertyObject, true); } else { FillData((IDTO)destinationPropertyObject, sourcePropertyObject, false); } } } else { 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); } } } } }