/// <summary> /// Creates the specified DTO type. /// </summary> /// <param name="type">The type.</param> /// <param name="args">The args.</param> /// <returns></returns> public IDTO Create(DTOType type, params object[] args) { try { // get type info QualifiedTypeNameAttribute QualifiedNameAttr = EnumAttributeUtility<DTOType, QualifiedTypeNameAttribute>.GetEnumAttribute(type.ToString()); // Initialize instance IDTO instance = null; // create instance instance = (IDTO)this.CreateObjectInstance(QualifiedNameAttr.AssemblyFileName, QualifiedNameAttr.QualifiedTypeName, args); // return return instance; } catch (FactoryException fex) { throw fex; } }
/// <summary> /// Initializes a new instance of the <see cref="DTOBase"/> class. /// </summary> /// <param name="dtoType">Type of the dto.</param> protected DTOBase(DTOType dtoType) { this.DTOType = dtoType; this._uniqueID = Guid.NewGuid(); this.ObjectState = ObjectStateType.None; }
/// <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); } } } } }
public ComplexPropertyMappingAttribute(DTOType DTOType) { this.DTOType = DTOType; }