///-------------------------------------------------------------------------------- /// <summary>This method executes the command and returns the first column of the /// first row of the results set. All other columns and rows are ignored.</summary> /// /// <param name="sprocName">Name of the stored procedure.</param> /// <param name="sprocParams">The input stored procedure parameter values.</param> /// /// <returns>The results of the operation.</returns> ///-------------------------------------------------------------------------------- public object ExecuteScalar(string sprocName, NameObjectCollection sprocParams) { DbCommand command = null; object scalar = null; try { // build the command and execute scalar for simple results command = SetupSprocCommand(sprocName, sprocParams); scalar = _database.ExecuteScalar(command); // put output/return values into sproc params foreach (DbParameter loopParameter in command.Parameters) { sprocParams[SqlHelper.GetParameterNameFromSqlParameterName(loopParameter.ParameterName)] = loopParameter.Value; } } catch (Exception ex) { bool reThrow = ExceptionHandler.HandleException(ex); if (reThrow) { throw; } } return(scalar); }
///-------------------------------------------------------------------------------- /// <summary>This method executes a stored procedure command using a data reader /// (to be used for get operations. The connection should be closed by the user /// and ouput parameters read after the connection is closed.</summary> /// /// <param name="sprocName">Name of the stored procedure.</param> /// <param name="sprocParams">The input stored procedure parameter values.</param> /// <param name="command">The output command (to get output parameters).</param> /// /// <returns>A IDataReader with query results.</returns> ///-------------------------------------------------------------------------------- public DbDataReader ExecuteReader(string sprocName, NameObjectCollection sprocParams, out DbCommand command) { command = null; DbDataReader reader = null; try { // build the command and execute the reader for get operations command = SetupSprocCommand(sprocName, sprocParams); reader = (DbDataReader)_database.ExecuteReader(command); // put output/return values into sproc params foreach (DbParameter loopParameter in command.Parameters) { sprocParams[SqlHelper.GetParameterNameFromSqlParameterName(loopParameter.ParameterName)] = loopParameter.Value; } } catch (Exception ex) { bool reThrow = ExceptionHandler.HandleException(ex); if (reThrow) { throw; } } return(reader); }
///-------------------------------------------------------------------------------- /// <summary>This method executes a non query (for database update operations). /// The user is expect to commit or roll back the transaction after one or more of /// these operations.</summary> /// /// <param name="sprocName">Name of the stored procedure.</param> /// <param name="sprocParams">The input stored procedure parameter values.</param> /// /// <returns>The result code of the operation.</returns> ///-------------------------------------------------------------------------------- public int ExecuteNonQuery(string sprocName, NameObjectCollection sprocParams) { DbCommand command = null; int results = DefaultValue.Int; try { // build the command and execute the non query for update operations command = SetupSprocCommand(sprocName, sprocParams); if (command.Connection.State == ConnectionState.Closed) { command.Connection.Open(); } results = _database.ExecuteNonQuery(command, Transaction); // put output/return values into sproc params foreach (DbParameter loopParameter in command.Parameters) { sprocParams[SqlHelper.GetParameterNameFromSqlParameterName(loopParameter.ParameterName)] = loopParameter.Value; } } catch (Exception ex) { bool reThrow = ExceptionHandler.HandleException(ex); if (reThrow) { throw; } } return(results); }
///-------------------------------------------------------------------------------- /// <summary>This method checks for a sql error in the output and throws an exception /// if one is found.</summary> /// /// <param name="sprocParams">The output stored procedure parameter values.</param> /// <param name="sqlErrorName">The parameter name to get a sql error number.</param> /// <param name="sqlErrorMessageName">The parameter name to get a sql error message.</param> ///-------------------------------------------------------------------------------- public static void ThrowSqlErrorIfFound(NameObjectCollection sprocParams, string sqlErrorName, string sqlErrorMessageName) { // check for a sql error and throw an exception if one is found if (sprocParams[sqlErrorName] != null && sprocParams[sqlErrorName].GetInt() != 0) { throw new ApplicationException("SQL Error " + sprocParams[sqlErrorName] + " has been encountered: " + sprocParams[sqlErrorMessageName]); } }
///-------------------------------------------------------------------------------- /// <summary>This method sets up a stored procedure command, loaded with values from /// the input data access object where appropriate.</summary> /// /// <param name="sprocName">Name of the stored procedure.</param> /// <param name="sprocParams">The input stored procedure parameter values.</param> /// /// <returns>A DbCommand for the stored procedure.</returns> ///-------------------------------------------------------------------------------- public DbCommand SetupSprocCommand(string sprocName, NameObjectCollection sprocParams) { GenericMethod sprocMethod = null; DbCommand command = null; try { // get sproc method from cache or db sprocMethod = DbProcs.MethodList.Find("MethodName", sprocName); if (sprocMethod == null) { // get sproc from db an add to cache sprocMethod = GetSprocFromDB(sprocName, true); } // set up the base command information command = _database.GetStoredProcCommand(sprocName); command.Connection = Connection; command.CommandTimeout = DBOptions.CommandTimeout; // load the sproc parameters with input data if found foreach (GenericParameter loopParameter in sprocMethod.MethodParameterList) { bool foundValue = false; if (sprocParams[loopParameter.ParameterName] != null && sprocParams[loopParameter.ParameterName].GetString() != String.Empty) { foundValue = true; } if (foundValue == false && loopParameter.IsNullable == false && loopParameter.ParameterTypeCode == 1) { // required parameter not found, throw exception throw new System.Exception("MoPlus.Data.BuildSprocParamsWithInputValues - required input value parameter " + loopParameter.ParameterName + " for stored procedure " + sprocName + " not found."); } _database.AddParameter(command, loopParameter.ParameterName, SqlHelper.GetDbTypeFromSqlDataType(loopParameter.DataTypeCode), loopParameter.Size, SqlHelper.GetParameterDirectionFromColumnType(loopParameter.ParameterTypeCode), loopParameter.IsNullable, (byte)loopParameter.Precision, (byte)4, loopParameter.ParameterName, DataRowVersion.Default, sprocParams[loopParameter.ParameterName]); } } catch (Exception ex) { bool reThrow = ExceptionHandler.HandleException(ex); if (reThrow) { throw; } } return(command); }
///-------------------------------------------------------------------------------- /// <summary>This method closes the connection and populates the output parameters /// in the collection provided.</summary> /// /// <remarks>This version of the Microsoft Enterprise library requires the connection /// to be closed in order to get output parameters after using ExecuteReader.</remarks> /// /// <param name="command">The output command (to get output parameters).</param> /// <param name="sprocParams">The associated stored procedure parameter values.</param> /// /// <returns>The result code of the operation.</returns> ///-------------------------------------------------------------------------------- public void CloseConnectionAndPopulateOutputParameters(DbCommand command, NameObjectCollection sprocParams) { try { if (command.Connection.State == ConnectionState.Open) { command.Connection.Close(); } // put output/return values into sproc params foreach (DbParameter loopParameter in command.Parameters) { sprocParams[SqlHelper.GetParameterNameFromSqlParameterName(loopParameter.ParameterName)] = loopParameter.Value; } } catch (Exception ex) { bool reThrow = ExceptionHandler.HandleException(ex); if (reThrow) { throw; } } }
///-------------------------------------------------------------------------------- /// <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> ///-------------------------------------------------------------------------------- public static void TransformDataFromObject(IDataObject inputObject, IDataObject outputObject, NameObjectCollection filterElements) { TransformDataFromObject(inputObject, outputObject, filterElements, true, false); }
///-------------------------------------------------------------------------------- /// <summary>This method executes a stored procedure command using a data reader /// (to be used for get operations. The connection should be closed by the user.</summary> /// /// <param name="sprocName">Name of the stored procedure.</param> /// <param name="sprocParams">The input stored procedure parameter values.</param> /// /// <returns>A IDataReader with query results.</returns> ///-------------------------------------------------------------------------------- public DbDataReader ExecuteReader(string sprocName, NameObjectCollection sprocParams) { DbCommand command = null; return(ExecuteReader(sprocName, sprocParams, out command)); }
///-------------------------------------------------------------------------------- /// <summary>This constructor takes in a sortable (or enterprise) data object list of another /// type (as an object) and transforms it to this list.</summary> /// /// <param name="inputListItemType">The type for the items to be found in the input list.</param> /// <param name="inputEnterpriseDataObjectList">The input list (as an object) to transform to this list.</param> /// <param name="filterElements">Field and property values to exclude from transform.</param> ///-------------------------------------------------------------------------------- public SortableDataObjectList(Type inputListItemType, object inputSortableDataObjectList, NameObjectCollection filterElements) { if (inputSortableDataObjectList != null) { if (inputSortableDataObjectList.GetType().Name.Contains("SortableDataObjectList") == true) { // handle sortable data object list Type inputListType = typeof(ISortableDataObjectList <>); Type genericListType = inputListType.MakeGenericType(inputListItemType); IList items = (IList)Activator.CreateInstance(genericListType, inputSortableDataObjectList, filterElements); foreach (IDataObject loopItem in items) { T newItem = new T(); newItem.TransformDataFromObject(loopItem, filterElements); Add(newItem); } } else if (inputSortableDataObjectList.GetType().Name.Contains("EnterpriseDataObjectList") == true) { // special handling of input enterprise data object list Type inputListType = typeof(EnterpriseDataObjectList <>); Type genericListType = inputListType.MakeGenericType(inputListItemType); IList items = (IList)Activator.CreateInstance(genericListType, inputSortableDataObjectList, filterElements); foreach (IEnterpriseDataObject loopItem in items) { T newItem = new T(); newItem.TransformDataFromObject(loopItem, filterElements); Add(newItem); } } else { // unsupported input list type throw new ApplicationException("SortableDataObjectList constructor given input list of the wrong type: " + inputSortableDataObjectList.GetType().Name); } } }
///-------------------------------------------------------------------------------- /// <summary>This constructor takes in a source list from which data is copied from. /// Output properties can be filtered out.</summary> /// /// <param name="sourceDataObjectList">The source sortable list to copy to this list.</param> /// <param name="filterElements">Field and property values to exclude from transform.</param> ///-------------------------------------------------------------------------------- public SortableDataObjectList(ISortableDataObjectList <T> sourceDataObjectList, NameObjectCollection filterElements) { if (sourceDataObjectList != null) { foreach (T loopItem in sourceDataObjectList) { T newItem = new T(); newItem.TransformDataFromObject(loopItem, filterElements); this.Add(newItem); } } }
///-------------------------------------------------------------------------------- /// <summary>This method retrieves formatted data of all serializable public /// property elements (non complex or array types). Header and maximum characters /// per column is input.</summary> /// /// <param name="includeHeader">Specifies whether or not to include the header in /// the output.</param> /// <param name="includeData">Specifies whether or not to include the data in /// the output.</param> /// <param name="maxWidthPerColumn">The maximum number of characters per column.</param> /// /// <remarks>In this version, the output only looks good for fixed width character /// fonts such as courier.</remarks> /// /// <returns>Formatted data of the list, with options as specified by the user.</returns> ///-------------------------------------------------------------------------------- public string GetFormattedData(bool includeHeader, bool includeData, int maxWidthPerColumn) { StringBuilder output = new StringBuilder(); NameObjectCollection outputCollection = new NameObjectCollection(); NameObjectCollection maxColumnWidths = new NameObjectCollection(); if (Count > 0) { int row = 0; if (includeHeader == true) { // add titles into collection foreach (PropertyInfo loopInfo in this[0].GetType().GetProperties()) { string title = loopInfo.Name; if (title.Length > maxWidthPerColumn) { title = title.Substring(0, maxWidthPerColumn); } if (loopInfo.GetCustomAttributes(typeof(XmlElementAttribute), true).Length > 0) { outputCollection["title, " + loopInfo.Name] = title; maxColumnWidths[loopInfo.Name] = Math.Min(title.Length, maxWidthPerColumn); } } } if (includeData == true) { // add data into collection row = 0; foreach (T item in this) { foreach (PropertyInfo loopInfo in item.GetType().GetProperties()) { string value = item.GetPropertyValueString(loopInfo.Name).GetString(); if (value.Length > maxWidthPerColumn) { value = value.Substring(0, maxWidthPerColumn); } if (loopInfo.GetCustomAttributes(typeof(XmlElementAttribute), true).Length > 0) { // output serializable element data into collection outputCollection[row.ToString() + ", " + loopInfo.Name] = value; maxColumnWidths[loopInfo.Name] = Math.Max((int)maxColumnWidths[loopInfo.Name], value.Length); maxColumnWidths[loopInfo.Name] = Math.Min((int)maxColumnWidths[loopInfo.Name], maxWidthPerColumn); } } row++; } } if (includeHeader == true) { // add padded titles to output foreach (PropertyInfo loopInfo in this[0].GetType().GetProperties()) { if (loopInfo.GetCustomAttributes(typeof(XmlElementAttribute), true).Length > 0) { output.Append(DataHelper.PadStringToLength(outputCollection["title, " + loopInfo.Name].ToString(), (int)maxColumnWidths[loopInfo.Name], DataHelper.IsLeftJustifiedSystemType(loopInfo.PropertyType.UnderlyingSystemType.Name))); output.Append(" "); } } } if (includeData == true) { // add padded data to output row = 0; foreach (T item in this) { output.Append("\r\n"); foreach (PropertyInfo loopInfo in item.GetType().GetProperties()) { if (loopInfo.GetCustomAttributes(typeof(XmlElementAttribute), true).Length > 0) { output.Append(DataHelper.PadStringToLength(outputCollection[row.ToString() + ", " + loopInfo.Name].ToString(), (int)maxColumnWidths[loopInfo.Name], DataHelper.IsLeftJustifiedSystemType(loopInfo.PropertyType.UnderlyingSystemType.Name))); output.Append(" "); } } row++; } } } return(output.ToString()); }
///-------------------------------------------------------------------------------- /// <summary>This method transforms data from the input object into this instance, /// with the option to include collections in the transformation.</summary> /// /// <param name="inputObject">The input object to get data from.</param> /// <param name="filterElements">Properties and fields whose values should be omitted from the transform.</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 void TransformDataFromObject(IDataObject inputObject, NameObjectCollection filterElements, bool includeCollections, bool getNonDefaultValuesOnly) { DataTransformHelper.TransformDataFromObject(inputObject, this, filterElements, includeCollections, getNonDefaultValuesOnly); }
///-------------------------------------------------------------------------------- /// <summary>This method transforms data from the input object into this instance.</summary> /// /// <param name="inputObject">The input object to get data from.</param> /// <param name="filterElements">Properties and fields whose values should be omitted from the transform.</param> /// <param name="includeCollections">Flag indicating whether or not to include collections in the transform.</param> ///-------------------------------------------------------------------------------- public void TransformDataFromObject(IDataObject inputObject, NameObjectCollection filterElements, bool includeCollections) { TransformDataFromObject(inputObject, filterElements, includeCollections, false); }
///-------------------------------------------------------------------------------- /// <summary>This constructor takes in an enterprise data object list of the same /// type and transforms it to this list.</summary> /// /// <param name="inputEnterpriseDataObjectList">The input list to transform to this list.</param> /// <param name="filterElements">Field and property values to exclude from transform.</param> /// <param name="copyItems">Flag indicating whether items in the list should be copied (or retained).</param> ///-------------------------------------------------------------------------------- public EnterpriseDataObjectList(EnterpriseDataObjectList <T> inputEnterpriseDataObjectList, NameObjectCollection filterElements, bool copyItems = true) { if (inputEnterpriseDataObjectList != null) { foreach (T loopItem in inputEnterpriseDataObjectList) { if (copyItems == true) { T newItem = new T(); newItem.TransformDataFromObject(loopItem, filterElements); Add(newItem); } else { Add(loopItem); } } } }
///-------------------------------------------------------------------------------- /// <summary>This method checks for a sql error in the output and throws an exception /// if one is found.</summary> /// /// <param name="sprocParams">The output stored procedure parameter values.</param> ///-------------------------------------------------------------------------------- public static void ThrowSqlErrorIfFound(NameObjectCollection sprocParams) { // check for a sql error and throw an exception if one is found ThrowSqlErrorIfFound(sprocParams, "SqlErrorNumber", "SqlErrorMessage"); }
///-------------------------------------------------------------------------------- /// <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>Transform data from the input data reader into the output object. Each /// element to be transformed must have the DataElement attribute associated with /// it.</summary> /// /// <param name="inputReader">The database reader 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> ///-------------------------------------------------------------------------------- public static void TransformDataFromDbReader(DbDataReader inputReader, IDataObject outputObject, NameObjectCollection filterElements) { try { // add all public properties with the DataElement attribute to the output object foreach (PropertyInfo loopInfo in outputObject.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().ToLower(); if (transformName == String.Empty) { transformName = loopInfo.Name.Trim().ToLower(); } // do transform if not in filter field list if (filterElements == null || filterElements[transformName].GetString() == String.Empty) { for (int i = 0; i < inputReader.FieldCount; i++) { if (inputReader.GetName(i).Trim().ToLower() == transformName) { // set value, based on system type loopInfo.SetValue(outputObject, DataHelper.GetValueFromSystemType(inputReader[i], loopInfo.PropertyType.UnderlyingSystemType.FullName, false), null); break; } } } } } } // add all fields with the DataElement attribute to the output object foreach (FieldInfo loopInfo in outputObject.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().ToLower(); if (transformName == String.Empty) { transformName = loopInfo.Name.Trim().ToLower(); } // do transform if not in filter field list if (filterElements == null || filterElements[transformName].GetString() == String.Empty) { for (int i = 0; i < inputReader.FieldCount; i++) { if (inputReader.GetName(i).Trim().ToLower() == transformName) { // set value, based on system type loopInfo.SetValue(outputObject, DataHelper.GetValueFromSystemType(inputReader[i], loopInfo.FieldType.UnderlyingSystemType.FullName, false)); break; } } } } } } } catch (Exception ex) { bool reThrow = ExceptionHandler.HandleException(ex); if (reThrow) { throw; } } }
///-------------------------------------------------------------------------------- /// <summary>This method takes in an enterprise data object list of an input type /// and transforms it to a list of the output type.</summary> /// /// <param name="inputEnterpriseDataObjectList">The input list to transform to this list.</param> /// <param name="filterElements">Field and property values to exclude from transform.</param> /// /// <returns>An enterprise data object list of type TOutput.</returns> ///-------------------------------------------------------------------------------- public static EnterpriseDataObjectList <TOutput> CreateList <TInput, TOutput>(EnterpriseDataObjectList <TInput> inputEnterpriseDataObjectList, NameObjectCollection filterElements) where TInput : IEnterpriseDataObject, new() where TOutput : IEnterpriseDataObject, new() { if (inputEnterpriseDataObjectList != null) { EnterpriseDataObjectList <TOutput> outputEnterpriseDataObjectList = new EnterpriseDataObjectList <TOutput>(); foreach (TInput loopItem in inputEnterpriseDataObjectList) { TOutput newItem = new TOutput(); newItem.TransformDataFromObject(loopItem, filterElements); outputEnterpriseDataObjectList.Add(newItem); } return(outputEnterpriseDataObjectList); } return(null); }