Пример #1
0
        /// <summary>
        /// Gets the row value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="row">The row.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="parseErrors">The parse errors.</param>
        /// <param name="parseErrorMessage">The parse error message.</param>
        /// <param name="isNullable">if set to <c>true</c> [is nullable].</param>
        /// <returns>System.Nullable&lt;T&gt;.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// row
        /// or
        /// propertyName
        /// or
        /// parseErrors
        /// </exception>
        public virtual T?GetRowValue <T>(dynamic row, string propertyName, ref List <string> parseErrors, string parseErrorMessage = null, bool isNullable = false) where T : struct
        {
            if (row == null)
            {
                throw new ArgumentNullException("row");
            }
            if (String.IsNullOrWhiteSpace(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }
            if (parseErrors == null)
            {
                throw new ArgumentNullException("parseErrors");
            }

            string input      = null;
            bool   valueFound = false;

            if (FileIOHelpers.IsDynamicType(row))
            {
                IDictionary <string, object> dict = row as IDictionary <string, object>;
                if (dict.ContainsKey(propertyName))
                {
                    input      = Convert.ToString(row[propertyName]);
                    valueFound = true;
                }
            }
            else
            {
                //the object is not a dynamic object, so get the value using reflection
                var prop = row.GetType().GetProperty(propertyName);
                if (prop != null)
                {
                    input      = Convert.ToString(prop.GetValue(row, null));
                    valueFound = true;
                }
            }

            if (valueFound)
            {
                return(GetRowValue <T>(input, ref parseErrors, parseErrorMessage, isNullable, propertyName));
            }
            else
            {
                return(default(T?));
            }
        }
Пример #2
0
        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.</returns>
        public IEnumerator <SqlDataRecord> GetEnumerator()
        {
            if (_data == null || !_data.Any())
            {
                yield break;
            }

            PropertyInfo[] properties = _importType.GetProperties();
            StringComparer comparer   = StringComparer.InvariantCultureIgnoreCase;

            this._validator = this._validator ?? new ObjectValidator();
            bool?isDynamicType = null;

            int errorColumnOrdinal = -1;
            var sqlMetaArray       = _sqlMetadata.ToArray();

            if (_sqlMetadata.Any(x => comparer.Equals(x.Name, _errorColumn)))
            {
                SqlDataRecord tempRecord = new SqlDataRecord(sqlMetaArray);
                errorColumnOrdinal = tempRecord.GetOrdinal(_errorColumn);                 //will cause an exception if it does not exist, hence the any check
                tempRecord         = null;
            }

            foreach (dynamic row in _data)
            {
                _rowIndex++;
                SqlDataRecord record = new SqlDataRecord(sqlMetaArray);
                List <string> errors = new List <string>();

                //check the first object to see if it is a dynamic type as we dont need to run it throught the object mapper in that case
                if (!isDynamicType.HasValue)
                {
                    isDynamicType = FileIOHelpers.IsDynamicType(row);
                }

                T rowObj = default(T);

                if (isDynamicType.Value)
                {
                    try
                    {
                        rowObj = FileIOUtilities.MapObject <T>(row, _rowIndex, _validator, _fileValuesMapper, ref errors);
                    }
                    catch (Exception ex)
                    {
                        errors.Add(ex.ToString());
                    }
                }
                else
                {
                    rowObj = row;
                }

                try
                {
                    //built in data annotation validation
                    this._validator.TryValidate(rowObj, ref errors);
                    //custom validation
                    if (_customValidator != null)
                    {
                        _customValidator.Invoke(rowObj, ref errors);
                    }
                }
                catch (Exception ex)
                {
                    errors.Add(ex.ToString());
                }

                ISqlRecordMapper mapperObj = null;
                //if they provide a custom mapper use that one over the interface.
                if (this._customSqlMapper != null)
                {
                    this._customSqlMapper.Invoke(rowObj, record, _rowIndex, errors);
                }
                else if ((mapperObj = rowObj as ISqlRecordMapper) != null)
                {
                    mapperObj.MapSqlRecord(record, _rowIndex, errors);
                }
                else                 //last ditch effort, hopefully they don't rely on this
                {
                    object val;
                    //try to set the rows from the metadata, and the properties
                    foreach (SqlMetaData metaData in _sqlMetadata)
                    {
                        string name = metaData.Name;
                        val = null;
                        if (!comparer.Equals(name, _errorColumn))
                        {
                            var prop = properties.FirstOrDefault(x => comparer.Equals(x.Name, name));
                            if (prop != null && (val = prop.GetValue(rowObj, null)) != null)
                            {
                                record.SetValue(record.GetOrdinal(name), val);
                            }
                        }
                    }
                    //if an error column is defined, set the import errors
                    if (errorColumnOrdinal != -1 && errors.Count != 0)
                    {
                        string errorMessage = FileIOHelpers.ErrorsToXml(errors, _rowIndex);
                        record.SetString(errorColumnOrdinal, errorMessage);
                    }
                }
                yield return(record);
            }
        }