private string GetLineFromModel(T row)
        {
            var line = string.Empty;

            foreach (var field in layout.GetOrderedFields())
            {
                var modelProperty = typeof(T).GetProperty(field.PropertyInfo.Name);

                if (modelProperty != null)
                {
                    var modelValue = modelProperty.GetValue(row).ToString();

                    // Throw exception on truncation. Early failure preferable to incorrect data.
                    // e.g. 1.234567890E+100 would truncate to 1.234 @ 5 char field length.
                    // TODO: Accept a type converter here for writes
                    if (field.Length < modelValue.Length)
                    {
                        throw new Exception($"Field is too short for value. '{field.PropertyInfo.Name}' field length of {field.Length} " +
                                            $"is less than required length of {modelValue.Length} for value {modelValue}");
                    }

                    var valueForFile = modelValue
                                       .Substring(0, Math.Min(field.Length, modelValue.Length))
                                       .PadRight(field.Length);

                    line += valueForFile;
                }
                else
                {
                    throw new Exception($"Model property with name {field.PropertyInfo.Name} was not found.");
                }
            }

            return(line);
        }
        public FixedWidthFileWriter(IFlatFileLayoutDescriptor <T> layout, string filePath)
        {
            this.layout   = layout;
            this.filePath = filePath;

            if (layout.GetOrderedFields()
                .Any(x => x.TypeConverter == null))
            {
                throw new ArgumentException("Missing TypeConverter for one or more fields", nameof(layout));
            }
        }
Beispiel #3
0
        /// <summary>
        ///     For each field in layout, the field is extracted from row and added to model (TEntity)
        /// </summary>
        /// <exception cref="T:Exception">Property name is null, not unique, or not found in TEntity.</exception>
        /// <param name="row"></param>
        /// <returns></returns>
        private T GetModelFromLine(string row)
        {
            var model = new T();

            foreach (var field in layout.GetOrderedFields())
            {
                if (field.ShouldSkip)
                {
                    continue;
                }

                // This could throw ambigous match exception if inheritance is used on the model incorrectly (e.g. new
                // keyword missing, and hiding a parent property)
                // This could throw argument null exception if Field or PropertyInfo or Name are null
                // Should check for these conditions eventually.
                var modelProperty = typeof(T).GetProperty(field.PropertyInfo.Name);

                if (modelProperty != null)
                {
                    var stringToConvert = row.Substring(field.StartPosition, field.Length);
                    var typeConverter   = field.TypeConverter;
                    var convertedValue  = typeConverter.GetConvertedValue(stringToConvert);

                    modelProperty.SetValue(
                        model,
                        convertedValue);
                }
                else
                {
                    /* Model type (TEntity) of LayoutDescriptor and FixedWidthFileParser must match, so
                     * short of user monkeying around with the PropertyInfo, the layout descriptor's
                     * field.PropertyInfo.Name should always be found in the model */
                    throw new Exception($"Model property with name {field.PropertyInfo.Name} was not found.");
                }
            }

            return(model);
        }