///-------------------------------------------------------------------------------- /// <summary>This method determines if the input enterprise data object is equal to /// (in key values) to the current instance.</summary> /// /// <param name="inputObject">The item to compare.</param> /// /// <returns>True if two instances compare as equal, false otherwise.</returns> ///-------------------------------------------------------------------------------- public override bool Equals(object inputObject) { if (inputObject == null) { return(false); } if (inputObject is IEnterpriseDataObject && GetType().Name == inputObject.GetType().Name) { IEnterpriseDataObject inputEnterpriseDataObject = (IEnterpriseDataObject)inputObject; // compare primary key values for equality if (PrimaryKeyValues != inputEnterpriseDataObject.PrimaryKeyValues) { return(false); } // compare all public property values for equality GenericEntity inputEntity = null; if (DataTransformHelper.EntityPropertyFieldInfoCache.GenericEntityCache[inputEnterpriseDataObject.GetType().FullName] is GenericEntity) { inputEntity = DataTransformHelper.EntityPropertyFieldInfoCache.GenericEntityCache[inputEnterpriseDataObject.GetType().FullName] as GenericEntity; } else { inputEntity = DataTransformHelper.EntityPropertyFieldInfoCache.AddNewEntityWithPropertyFieldInfo(inputEnterpriseDataObject); } foreach (GenericProperty loopProperty in inputEntity.PropertyList) { if (loopProperty.PropertyTypeCode == (int)DataTransformPropertyType.ElementProperty || loopProperty.PropertyTypeCode == (int)DataTransformPropertyType.ElementField) { if (inputEnterpriseDataObject.IsPropertyValueMatch(loopProperty.PropertyName, GetPropertyValue(loopProperty.PropertyName)) == false) { return(false); } } } return(true); } return(false); }
///-------------------------------------------------------------------------------- /// <summary>Transform data from the input object into the output object. Each /// element to be transformed must have the DataElement or DataArrayItem attribute /// associated with it.</summary> /// /// <param name="inputObject">The object with the input data.</param> /// <param name="outputObject">The output object to be populated with the input data.</param> /// <param name="filterElements">Data elements to filter out of the transformation.</param> /// <param name="includeCollections">Flag indicating whether or not to include collections in the transform.</param> /// <param name="getNonDefaultValuesOnly">If true, only return a value if not null and not a default value.</param> ///-------------------------------------------------------------------------------- public static void TransformDataFromObject(IDataObject inputObject, IDataObject outputObject, NameObjectCollection filterElements, bool includeCollections, bool getNonDefaultValuesOnly) { GenericEntity inputEntity = null; GenericEntity outputEntity = null; PropertyInfo inputPropertyInfo = null; PropertyInfo outputPropertyInfo = null; FieldInfo inputFieldInfo = null; FieldInfo outputFieldInfo = null; object inputArrayValue = null; object inputValue = null; Type inputType = null; string inputTypeName = String.Empty; string outputTypeName = String.Empty; try { if (inputObject != null && outputObject != null) { // get input and output entities that contain property and field info information // add new input and output objects encountered to the entity property/field info cache inputTypeName = inputObject.GetType().FullName; if (EntityPropertyFieldInfoCache.GenericEntityCache[inputTypeName] is GenericEntity) { inputEntity = EntityPropertyFieldInfoCache.GenericEntityCache[inputTypeName] as GenericEntity; } else { inputEntity = EntityPropertyFieldInfoCache.AddNewEntityWithPropertyFieldInfo(inputObject); } outputTypeName = outputObject.GetType().FullName; if (EntityPropertyFieldInfoCache.GenericEntityCache[outputTypeName] is GenericEntity) { outputEntity = EntityPropertyFieldInfoCache.GenericEntityCache[outputTypeName] as GenericEntity; } else { outputEntity = EntityPropertyFieldInfoCache.AddNewEntityWithPropertyFieldInfo(outputObject); } // go through transform elements and perform transform where applicable/allowed foreach (GenericProperty loopOutputProperty in outputEntity.PropertyList) { if (filterElements == null || filterElements[loopOutputProperty.PropertyName].GetString() == String.Empty) { bool transformPerformed = false; foreach (GenericProperty loopInputProperty in inputEntity.PropertyList) { if (loopOutputProperty.PropertyName == loopInputProperty.PropertyName) { switch (loopInputProperty.PropertyTypeCode) { case (int)DataTransformPropertyType.ElementProperty: switch (loopOutputProperty.PropertyTypeCode) { case (int)DataTransformPropertyType.ElementProperty: // transform property to property inputPropertyInfo = loopInputProperty.GenericValue as PropertyInfo; outputPropertyInfo = loopOutputProperty.GenericValue as PropertyInfo; inputValue = DataHelper.GetValueFromSystemType(inputObject.GetPropertyValueString(inputPropertyInfo.Name), outputPropertyInfo.PropertyType.UnderlyingSystemType.FullName, getNonDefaultValuesOnly); if (inputValue != null || getNonDefaultValuesOnly == false) { outputPropertyInfo.SetValue(outputObject, inputValue, null); } transformPerformed = true; break; case (int)DataTransformPropertyType.ElementField: // transform property to field inputPropertyInfo = loopInputProperty.GenericValue as PropertyInfo; outputFieldInfo = loopOutputProperty.GenericValue as FieldInfo; inputValue = DataHelper.GetValueFromSystemType(inputObject.GetPropertyValueString(inputPropertyInfo.Name), outputFieldInfo.FieldType.UnderlyingSystemType.FullName, getNonDefaultValuesOnly); if (inputValue != null || getNonDefaultValuesOnly == false) { outputFieldInfo.SetValue(outputObject, inputValue); } transformPerformed = true; break; default: break; } break; case (int)DataTransformPropertyType.ElementField: switch (loopOutputProperty.PropertyTypeCode) { case (int)DataTransformPropertyType.ElementProperty: // transform field to property inputFieldInfo = loopInputProperty.GenericValue as FieldInfo; outputPropertyInfo = loopOutputProperty.GenericValue as PropertyInfo; inputValue = DataHelper.GetValueFromSystemType(inputObject.GetFieldValueString(inputFieldInfo.Name), outputPropertyInfo.PropertyType.UnderlyingSystemType.FullName, getNonDefaultValuesOnly); if (inputValue != null || getNonDefaultValuesOnly == false) { outputPropertyInfo.SetValue(outputObject, inputValue, null); } transformPerformed = true; break; case (int)DataTransformPropertyType.ElementField: // transform field to field inputFieldInfo = loopInputProperty.GenericValue as FieldInfo; outputFieldInfo = loopOutputProperty.GenericValue as FieldInfo; inputValue = DataHelper.GetValueFromSystemType(inputObject.GetFieldValueString(inputFieldInfo.Name), outputFieldInfo.FieldType.UnderlyingSystemType.FullName, getNonDefaultValuesOnly); if (inputValue != null || getNonDefaultValuesOnly == false) { outputFieldInfo.SetValue(outputObject, inputValue); } transformPerformed = true; break; default: break; } break; case (int)DataTransformPropertyType.ArrayItemProperty: if (includeCollections == true) { inputType = null; inputPropertyInfo = loopInputProperty.GenericValue as PropertyInfo; inputArrayValue = inputPropertyInfo.GetValue(inputObject, null); if (inputArrayValue != null) { if (inputPropertyInfo.PropertyType.IsGenericType && inputPropertyInfo.PropertyType.GetProperty("Item") != null) { inputType = inputPropertyInfo.PropertyType.GetProperty("Item").PropertyType; } if (inputType != null) { switch (loopOutputProperty.PropertyTypeCode) { case (int)DataTransformPropertyType.ArrayItemProperty: // create array item and transfer property to property outputPropertyInfo = loopOutputProperty.GenericValue as PropertyInfo; outputPropertyInfo.SetValue(outputObject, Activator.CreateInstance(outputPropertyInfo.PropertyType, inputType, inputArrayValue, filterElements), null); transformPerformed = true; break; case (int)DataTransformPropertyType.ArrayItemField: // create array item and transfer property to field outputFieldInfo = loopOutputProperty.GenericValue as FieldInfo; outputFieldInfo.SetValue(outputObject, Activator.CreateInstance(outputFieldInfo.FieldType, inputType, inputArrayValue, filterElements)); transformPerformed = true; break; default: break; } } } } else { transformPerformed = true; } break; case (int)DataTransformPropertyType.ArrayItemField: if (includeCollections == true) { inputType = null; inputFieldInfo = loopInputProperty.GenericValue as FieldInfo; inputArrayValue = inputFieldInfo.GetValue(inputObject); if (inputArrayValue != null) { if (inputFieldInfo.FieldType.IsGenericType && inputFieldInfo.FieldType.GetProperty("Item") != null) { inputType = inputFieldInfo.FieldType.GetProperty("Item").PropertyType; } if (inputType != null) { switch (loopOutputProperty.PropertyTypeCode) { case (int)DataTransformPropertyType.ArrayItemProperty: // create array item and transfer field to property outputPropertyInfo = loopOutputProperty.GenericValue as PropertyInfo; outputPropertyInfo.SetValue(outputObject, Activator.CreateInstance(outputPropertyInfo.PropertyType, inputType, inputArrayValue, filterElements), null); transformPerformed = true; break; case (int)DataTransformPropertyType.ArrayItemField: // create array item and transfer field to field outputFieldInfo = loopOutputProperty.GenericValue as FieldInfo; outputFieldInfo.SetValue(outputObject, Activator.CreateInstance(outputFieldInfo.FieldType, inputType, inputArrayValue, filterElements)); transformPerformed = true; break; default: break; } } } } else { transformPerformed = true; } break; default: break; } } if (transformPerformed == true) { break; } } } } } } catch (Exception ex) { bool reThrow = ExceptionHandler.HandleException(ex); if (reThrow) { throw; } } finally { inputEntity = null; outputEntity = null; inputPropertyInfo = null; outputPropertyInfo = null; inputFieldInfo = null; outputFieldInfo = null; } }
///-------------------------------------------------------------------------------- /// <summary>Add a generic entity loaded with property and field info from reflection.</summary> /// /// <param name="inputObject">The object with the input data.</param> /// /// <returns>A GenericEntity with property/field info.</returns> ///-------------------------------------------------------------------------------- public GenericEntity AddNewEntityWithPropertyFieldInfo(IDataObject inputObject) { GenericEntity newEntity = new GenericEntity(); try { newEntity.EntityName = inputObject.GetType().FullName; newEntity.PropertyList = new SortableDataObjectList <GenericProperty>(); // add all public properties with the DataElement or DataArrayItem attribute to the entity foreach (PropertyInfo loopInfo in inputObject.GetType().GetProperties()) { foreach (object loopAttribute in loopInfo.GetCustomAttributes(true)) { if (loopAttribute is DataElementAttribute) { // get name of property to transform string transformName = (loopAttribute as DataElementAttribute).ElementName.GetString().Trim(); if (transformName == String.Empty) { transformName = loopInfo.Name.Trim(); } GenericProperty newProperty = new GenericProperty(); newProperty.PropertyName = transformName; newProperty.GenericValue = loopInfo; PropertyInfoCache[newEntity.EntityName + "." + transformName] = loopInfo; newProperty.PropertyTypeCode = (int)DataTransformPropertyType.ElementProperty; newEntity.PropertyList.Add(newProperty); } else if (loopAttribute is DataArrayItemAttribute) { // get name of property to transform string transformName = (loopAttribute as DataArrayItemAttribute).ElementName.GetString().Trim(); if (transformName == String.Empty) { transformName = loopInfo.Name.Trim(); } GenericProperty newProperty = new GenericProperty(); newProperty.PropertyName = transformName; newProperty.GenericValue = loopInfo; PropertyInfoCache[newEntity.EntityName + "." + transformName] = loopInfo; newProperty.PropertyTypeCode = (int)DataTransformPropertyType.ArrayItemProperty; newEntity.PropertyList.Add(newProperty); } } } // add all fields with the DataElement or DataArrayItem attribute to the entity foreach (FieldInfo loopInfo in inputObject.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance)) { foreach (object loopAttribute in loopInfo.GetCustomAttributes(true)) { if (loopAttribute is DataElementAttribute) { // get name of field to transform string transformName = (loopAttribute as DataElementAttribute).ElementName.GetString().Trim(); if (transformName == String.Empty) { transformName = loopInfo.Name.Trim(); } GenericProperty newProperty = new GenericProperty(); newProperty.PropertyName = transformName; newProperty.GenericValue = loopInfo; FieldInfoCache[newEntity.EntityName + "." + transformName] = loopInfo; newProperty.PropertyTypeCode = (int)DataTransformPropertyType.ElementField; newEntity.PropertyList.Add(newProperty); } else if (loopAttribute is DataArrayItemAttribute) { // get name of field to transform string transformName = (loopAttribute as DataArrayItemAttribute).ElementName.GetString().Trim(); if (transformName == String.Empty) { transformName = loopInfo.Name.Trim(); } GenericProperty newProperty = new GenericProperty(); newProperty.PropertyName = transformName; newProperty.GenericValue = loopInfo; FieldInfoCache[newEntity.EntityName + "." + transformName] = loopInfo; newProperty.PropertyTypeCode = (int)DataTransformPropertyType.ArrayItemField; newEntity.PropertyList.Add(newProperty); } } } GenericEntityCache[newEntity.EntityName] = newEntity; } catch (Exception ex) { bool reThrow = ExceptionHandler.HandleException(ex); if (reThrow) { throw; } } return(newEntity); }