public bool SetProperty <TProp>( ImportResult result, string columnName, Expression <Func <T, TProp> > prop, TProp defaultValue = default(TProp), Func <object, CultureInfo, TProp> converter = null) { // TBD: (MC) do not check or validate for perf reason? //CheckInitialized(); var isPropertySet = false; var pi = prop.ExtractPropertyInfo(); var propName = pi.Name; var target = _entity; columnName = columnName ?? propName; try { object value; var mapping = _segmenter.ColumnMap.GetMapping(columnName); if (mapping.IgnoreProperty) { // explicitly ignore this property } else if (_row.TryGetValue(mapping.MappedName, out value) && value != null && value != DBNull.Value && !value.ToString().IsCaseInsensitiveEqual(ExplicitIgnore)) { // source contains field value. Set it. TProp converted; if (converter != null) { converted = converter(value, _segmenter.Culture); } else if (value.ToString().IsCaseInsensitiveEqual(ExplicitNull)) { // prop is "explicitly" set to null. Don't fallback to any default! converted = default(TProp); } else { converted = value.Convert <TProp>(_segmenter.Culture); } var fastProp = FastProperty.GetProperty(target.GetUnproxiedType(), propName, PropertyCachingStrategy.EagerCached); fastProp.SetValue(target, converted); isPropertySet = true; } else { // source field value does not exist or is null/empty if (IsNew) { // if entity is new and source field value is null, determine default value in this particular order: // 2.) Default value in field mapping table // 3.) passed default value argument defaultValue = GetDefaultValue(mapping, defaultValue, result); // source does not contain field data or is empty... if (defaultValue != null) { // ...but the entity is new. In this case set the default value if given. var fastProp = FastProperty.GetProperty(target.GetUnproxiedType(), propName, PropertyCachingStrategy.EagerCached); fastProp.SetValue(target, defaultValue); isPropertySet = true; } } } } catch (Exception exception) { result.AddWarning("Conversion failed: " + exception.Message, this.GetRowInfo(), propName); } if (isPropertySet && !_isDirty) { _isDirty = true; } return(isPropertySet); }
private TProp GetDefaultValue <TProp>(ColumnMappingItem mapping, TProp defaultValue, ImportResult result = null) { if (mapping != null && mapping.Default.HasValue()) { try { return(mapping.Default.Convert <TProp>(_segmenter.Culture)); } catch (Exception exception) { if (result != null) { var msg = "Failed to convert default value '{0}'. Please specify a convertable default value. Column: {1}"; result.AddWarning(msg.FormatInvariant(mapping.Default, exception.Message), this.GetRowInfo(), mapping.SoureName); } } } return(defaultValue); }
public bool SetProperty <TProp>( ImportResult result, T target, Expression <Func <T, TProp> > prop, TProp defaultValue = default(TProp), Func <object, CultureInfo, TProp> converter = null) { // TBD: (MC) do not check for perf reason? //CheckInitialized(); var isPropertySet = false; var pi = prop.ExtractPropertyInfo(); var propName = pi.Name; try { object value; var mapping = _segmenter.ColumnMap.GetMapping(propName); if (_row.TryGetValue(mapping.Property, out value)) { // source contains field value. Set it. TProp converted; if (converter != null) { converted = converter(value, _segmenter.Culture); } else if (value == DBNull.Value || value.ToString().IsCaseInsensitiveEqual("NULL")) { converted = GetDefaultValue(mapping, propName, defaultValue, result); } else { converted = value.Convert <TProp>(_segmenter.Culture); } var fastProp = FastProperty.GetProperty(target.GetUnproxiedType(), propName, PropertyCachingStrategy.EagerCached); fastProp.SetValue(target, converted); isPropertySet = true; } else { if (IsTransient) { defaultValue = GetDefaultValue(mapping, propName, defaultValue, result); // source does not contain field data or is empty... if (defaultValue != null) { // ...but the entity is new. In this case set the default value if given. var fastProp = FastProperty.GetProperty(target.GetUnproxiedType(), propName, PropertyCachingStrategy.EagerCached); fastProp.SetValue(target, defaultValue); isPropertySet = true; } } } } catch (Exception exception) { result.AddWarning("Conversion failed: " + exception.Message, this.GetRowInfo(), propName); } return(isPropertySet); }