public static void DoMemberLevelValidation(this IDictionary <string, object> dict, string fn, ChoRecordFieldConfiguration fieldConfig, ChoObjectValidationMode vm) { if (!fieldConfig.Validators.IsNullOrEmpty() && (vm & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { ChoValidator.ValidateFor(dict[fn], fn, fieldConfig.Validators); } }
public static void DoMemberLevelValidation(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, ChoObjectValidationMode vm) { if (!((vm & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel)) { return; } if (rec is IDictionary <string, object> ) { ((IDictionary <string, object>)rec).DoMemberLevelValidation(fn, fieldConfig, vm); } else { if (fieldConfig.PI != null) { if (fieldConfig.Validators.IsNullOrEmpty()) { ChoValidator.ValidateFor(rec, fieldConfig.PI); } else { ChoValidator.ValidateFor(ChoType.GetPropertyValue(rec, fieldConfig.PI), fn, fieldConfig.Validators); } } } }
public static void DoObjectLevelValidatation(this object record, ChoRecordFieldConfiguration[] fieldConfigs) { bool hasConfigValidators = (from fc in fieldConfigs where !fc.Validators.IsNullOrEmpty() select fc).Any(); if (hasConfigValidators) { Dictionary <string, ValidationAttribute[]> valDict = (from fc in fieldConfigs select new KeyValuePair <string, ValidationAttribute[]>(fc.Name, fc.Validators)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); IDictionary <string, Object> dict = null; if (record is ExpandoObject) { dict = record as IDictionary <string, Object>; } else { dict = record.ToDictionary(); } ChoValidator.Validate(dict, valDict); } else { if (!(record is ExpandoObject)) { ChoValidator.Validate(record); } } }
public static void DoObjectLevelValidation(this object recObject, ChoRecordConfiguration configuration, IEnumerable <ChoRecordFieldConfiguration> fieldConfigurations) { if ((configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { if (configuration.HasConfigValidators) { IDictionary <string, Object> dict = null; if (recObject is ExpandoObject) { dict = recObject as IDictionary <string, Object>; } else { dict = new Dictionary <string, object>(); foreach (var pd in configuration.PIDict.Values) { dict.Add(pd.Name, ChoType.GetPropertyValue(recObject, pd)); } } ChoValidator.Validate(dict, configuration.ValDict); } else { if (!configuration.IsDynamicObject) { ChoValidator.Validate(recObject); } } } }
public static void DoMemberLevelValidation(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, ChoObjectValidationMode vm, object fieldValue) { if ((vm & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { if (fieldConfig.Validators.IsNullOrEmpty()) { ChoValidator.ValidateFor(fieldValue, fn, fieldConfig.Validators); } } }
public static void DoObjectLevelValidation(this object recObject, ChoRecordConfiguration configuration, IEnumerable <ChoRecordFieldConfiguration> fieldConfigurations) { if (recObject == null) { return; } if ((configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { if (configuration.Validator == null) { if (configuration.HasConfigValidators) { IDictionary <string, Object> dict = null; if (recObject is IDictionary <string, object> ) { dict = recObject as IDictionary <string, Object>; } else { dict = new Dictionary <string, object>(); foreach (var pd in configuration.PIDict.Values) { dict.Add(pd.Name, ChoType.GetPropertyValue(recObject, pd)); } } ChoValidator.Validate(dict, configuration.ValDict); } else { if (!configuration.IsDynamicObject) { ChoValidator.Validate(recObject); } } } else { if (recObject != null && configuration.Validator(recObject)) { throw new ValidationException("Failed to validate '{0}' object. {1}".FormatString(recObject.GetType().FullName, Environment.NewLine)); } } } }
public static void DoMemberLevelValidation(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, ChoObjectValidationMode vm, object fieldValue) { if ((vm & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { if (fieldConfig.Validators.IsNullOrEmpty()) { if (!(rec is ExpandoObject)) { ChoValidator.ValidateFor(fieldValue, fn, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(rec.GetType(), fn)).ToArray()); } } else { ChoValidator.ValidateFor(fieldValue, fn, fieldConfig.Validators); } } }
public static void DoMemberLevelValidation(this IDictionary <string, object> dict, string fn, ChoRecordFieldConfiguration fieldConfig, ChoObjectValidationMode vm) { if (!fieldConfig.Validators.IsNullOrEmpty() && (vm & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { if (fieldConfig.Validator == null) { ChoValidator.ValidateFor(dict[fn], fn, fieldConfig.Validators); } else { if (!fieldConfig.Validator(dict[fn])) { throw new ValidationException("Failed to validate '{0}' member. {1}".FormatString(fn, Environment.NewLine)); } } } }
public static void DoMemberLevelValidation(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, ChoObjectValidationMode vm) { if (rec is ExpandoObject) { ((IDictionary <string, object>)rec).DoMemberLevelValidation(fn, fieldConfig, vm); } else { if ((vm & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { if (fieldConfig.Validators.IsNullOrEmpty()) { ChoValidator.ValidateFor(rec, fn); } else { ChoValidator.ValidateFor(ChoType.GetMemberValue(rec, fn), fn, fieldConfig.Validators); } } } }
public static void DoMemberLevelValidation(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, ChoObjectValidationMode vm) { if (!((vm & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel)) { return; } if (rec is IDictionary <string, object> ) { ((IDictionary <string, object>)rec).DoMemberLevelValidation(fn, fieldConfig, vm); } else { if (fieldConfig.Validator == null) { if (fieldConfig.PD == null) { fieldConfig.PD = fieldConfig.PropertyDescriptor; } if (fieldConfig.PD != null) { if (fieldConfig.Validators.IsNullOrEmpty()) { ChoValidator.ValidateFor(rec, fieldConfig.PD); } else { ChoValidator.ValidateFor(fieldConfig.PD.GetValue(rec), fn, fieldConfig.Validators); } } } else { if (!fieldConfig.Validator(fieldConfig.PD.GetValue(rec))) { throw new ValidationException("Failed to validate '{0}' member. {1}".FormatString(fn, Environment.NewLine)); } } } }
public static object ChangeType <T>(object value, Type conversionType) where T : Attribute { if (value == null) { return(ChoActivator.CreateInstanceAndInit(conversionType)); } if (conversionType.IsAssignableFrom(value.GetType())) { return(value); } object dest = null; if (value is IConvertible) { dest = Convert.ChangeType(value, conversionType); } else { dest = ChoActivator.CreateInstanceAndInit(conversionType); value.CloneTo <T>(dest); } if (dest != null && !dest.GetType().IsSimple()) { ChoObjectValidationMode m = GetValidationMode(value); if (m == ChoObjectValidationMode.MemberLevel) { ChoValidator.Validate(dest); } else if (m == ChoObjectValidationMode.ObjectLevel) { ChoValidator.Validate(dest); } } return(dest); }
private void Initialize() { try { if (!Monitor.TryEnter(_padLock, 1 * 1000)) { return; } IDictionary <string, object> kvpDict = null; if (_func != null) { kvpDict = _func(); } else { kvpDict = Seed(); } if (kvpDict == null) { return; } IDictionary <string, object> mkvpDict = _kvpDict; bool hasDiff = mkvpDict == null || kvpDict.Except(mkvpDict).Concat(mkvpDict.Except(kvpDict)).Any(); if (!hasDiff) { return; } _kvpDict = kvpDict; ChoPropertyAttribute attr = null; object memberValue = null; string propName = null; //scan through members and load them foreach (var prop in ChoType.GetMembers(GetType()).Where(m => m.GetCustomAttribute <ChoIgnoreMemberAttribute>() != null && !ChoType.IsReadOnlyMember(m))) { attr = ChoType.GetMemberAttribute <ChoPropertyAttribute>(prop); try { SetDefaultValue(prop, true); propName = attr != null && !attr.Name.IsNullOrWhiteSpace() ? attr.Name : prop.Name; if (kvpDict.ContainsKey(propName)) { memberValue = AfterKVPLoaded(prop.Name, kvpDict[propName]); } else { memberValue = AfterKVPLoaded(prop.Name, null); } if (memberValue != null && memberValue is string) { string mv = memberValue as string; if (attr != null) { switch (attr.TrimOption) { case ChoPropertyValueTrimOption.Trim: mv = mv.Trim(); break; case ChoPropertyValueTrimOption.TrimEnd: mv = mv.TrimEnd(); break; case ChoPropertyValueTrimOption.TrimStart: mv = mv.TrimStart(); break; } } memberValue = mv; } if (ChoType.GetMemberType(prop) == typeof(string)) { if (!((string)memberValue).IsNullOrEmpty()) { ChoType.ConvertNSetMemberValue(this, prop, memberValue); } } else { if (memberValue != null) { ChoType.ConvertNSetMemberValue(this, prop, memberValue); } } ChoValidator.ValidateFor(this, prop); } catch (Exception ex) { //ChoLog.Error("{0}: Error loading '{1}' property. {2}".FormatString(NName, prop.Name, ex.Message)); SetDefaultValue(prop, false); } } } catch (Exception outerEx) { //ChoLog.Error("{0}: Error loading options. {1}".FormatString(NName, outerEx.Message)); } finally { Monitor.Exit(_padLock); } }
private bool FillRecord(object rec, Tuple <int, string> pair) { int lineNo; string line; lineNo = pair.Item1; line = pair.Item2; object fieldValue = null; string[] fieldValues = (from x in line.Split(Configuration.Delimiter, Configuration.StringSplitOptions, Configuration.QuoteChar) select x).ToArray(); if (Configuration.ColumnCountStrict) { if (fieldValues.Length != Configuration.RecordFieldConfigurations.Count) { throw new ChoParserException("Incorrect number of field values found at line [{2}]. Expected [{0}] field values. Found [{1}] field values.".FormatString(Configuration.RecordFieldConfigurations.Count, fieldValues.Length, pair.Item1)); } } Dictionary <string, string> _fieldNameValues = ToFieldNameValues(fieldValues); ValidateLine(pair.Item1, fieldValues); ChoCSVRecordFieldConfiguration fieldConfig = null; foreach (KeyValuePair <string, ChoCSVRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict) { fieldConfig = kvp.Value; if (Configuration.CSVFileHeaderConfiguration.HasHeaderRecord) { if (_fieldNameValues.ContainsKey(fieldConfig.FieldName)) { fieldValue = _fieldNameValues[fieldConfig.FieldName]; } else if (Configuration.ColumnCountStrict) { throw new ChoParserException("No matching '{0}' field header found.".FormatString(fieldConfig.FieldName)); } } else { if (fieldConfig.FieldPosition - 1 < fieldValues.Length) { fieldValue = fieldValues[fieldConfig.FieldPosition - 1]; } else if (Configuration.ColumnCountStrict) { throw new ChoParserException("Missing field value for {0} [Position: {1}] field.".FormatString(fieldConfig.FieldName, fieldConfig.FieldPosition)); } } fieldValue = CleanFieldValue(fieldConfig, fieldValue as string); if (!RaiseBeforeRecordFieldLoad(rec, pair.Item1, kvp.Key, ref fieldValue)) { continue; } try { bool ignoreFieldValue = fieldConfig.IgnoreFieldValue(fieldValue); if (!ignoreFieldValue) { if (rec is ExpandoObject) { if (fieldConfig.FieldType != typeof(string)) { fieldValue = ChoConvert.ConvertTo(fieldValue, fieldConfig.FieldType, Configuration.Culture); } var x = rec as IDictionary <string, Object>; x.Add(kvp.Key, fieldValue); } else { if (ChoType.HasProperty(rec.GetType(), kvp.Key)) { ChoType.ConvertNSetMemberValue(rec, kvp.Key, fieldValue); fieldValue = ChoType.GetMemberValue(rec, kvp.Key); if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { ChoValidator.ValididateFor(rec, kvp.Key); } } else { throw new ChoMissingRecordFieldException("Missing '{0}' property in {1} type.".FormatString(kvp.Key, ChoType.GetTypeName(rec))); } } } if (!RaiseAfterRecordFieldLoad(rec, pair.Item1, kvp.Key, fieldValue)) { return(false); } } catch (ChoParserException) { throw; } catch (ChoMissingRecordFieldException) { if (Configuration.ThrowAndStopOnMissingField) { throw; } } catch (Exception ex) { ChoETLFramework.HandleException(ex); if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop) { throw; } try { ChoFallbackValueAttribute fbAttr = ChoTypeDescriptor.GetPropetyAttribute <ChoFallbackValueAttribute>(rec.GetType(), kvp.Key); if (fbAttr != null) { if (!fbAttr.Value.IsNullOrDbNull()) { ChoType.ConvertNSetMemberValue(rec, kvp.Key, fbAttr.Value); } } else { throw; } } catch { if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue) { continue; } else if (fieldConfig.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordFieldLoadError(rec, pair.Item1, kvp.Key, fieldValue, ex)) { throw; } } else { throw; } } } } return(true); }
private bool LoadLine(Tuple <int, string> pair, ref object rec) { try { if (!RaiseBeforeRecordLoad(rec, ref pair)) { return(false); } if (pair == null || pair.Item2 == null) { return(false); } if (!pair.Item2.IsNullOrWhiteSpace()) { if (!FillRecord(rec, pair)) { return(false); } if (!(rec is ExpandoObject) && (Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { ChoValidator.Validate(rec); } } if (!RaiseAfterRecordLoad(rec, pair)) { return(false); } } catch (ChoParserException) { throw; } catch (ChoMissingRecordFieldException) { throw; } catch (Exception ex) { ChoETLFramework.HandleException(ex); if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue) { rec = null; } else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordLoadError(rec, pair, ex)) { throw; } } else { throw; } return(true); } return(true); }