/// <summary> /// This code gets called on the first time this Object type is constructed /// </summary> void AddSharedBusinessRules() { lock (_LockObject) { if (!SharedValidationRules.RulesExistFor(this.GetType())) { ValidationRulesManager mgrValidation = SharedValidationRules.GetManager(this.GetType()); CharacterFormattingRulesManager mgrCharacterCasing = SharedCharacterFormattingRules.GetManager(this.GetType()); foreach (var propInfo in PclReflection.GetPropertiesInfo(this)) { foreach (var atr in propInfo.GetCustomAttributes <BaseValidatorAttribute>(false)) { mgrValidation.AddRule(atr.Create(propInfo.Name), propInfo.Name); } foreach (var atr in propInfo.GetCustomAttributes <CharacterFormattingAttribute>(false)) { mgrCharacterCasing.AddRule(propInfo.Name, atr.CharacterCasing, atr.RemoveSpace); } } AddSharedBusinessValidationRules(mgrValidation); AddSharedCharacterCasingFormattingRules(mgrCharacterCasing); } } }
/// <summary> /// Builds up a string containing each property and value in the class. The string displays the property name, property friendly name and property value. /// </summary> /// <typeparam name="T">Class Type</typeparam> /// <param name="instance">Instance of the class</param> /// <param name="delimiter">What delimiter do you want between each property? Defaults to comma; can pass others like Environment.NewLine, etc.</param> /// <param name="sortByPropertyName">If set to <c>SortByPropertyName.Yes</c> then output will be sorted by AuditAttribute.AuditSequence and then property name; otherwise no additional sorting is performed and properties; will be left in ordinal order.</param> /// <returns>A string containing each property name, friendly name and value, separated by the delimiter and optionally sorted by property name.</returns> public static String ClassToString <T>(T instance, String delimiter = GlobalConstants.DefaultDelimiter, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { var sb = new StringBuilder(4096); var list = new List <SortablePropertyBasket>(); foreach (var propInfo in PclReflection.GetPropertiesInfo(instance)) { var auditAttributes = propInfo.GetCustomAttributes <AuditAttribute>(false); var auditAttributeList = new List <AuditAttribute>(auditAttributes); AuditAttribute auditAttribute = null; if (auditAttributeList.Count == 1) { auditAttribute = auditAttributeList[0]; } var auditSequence = 1; if (auditAttribute != null) { auditSequence = auditAttribute.AuditSequence; } if (propInfo.Name != "Item" && propInfo.Name != "ValidationErrors") { list.Add(propInfo.GetValue(instance, null) != null ? new SortablePropertyBasket(auditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), propInfo.GetValue(instance, null).ToString()) : new SortablePropertyBasket(auditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), Null)); } } if (list.Count > 0) { if (sortByPropertyName == SortByPropertyName.Yes) { list.Sort(); } foreach (var propertyBasket in list) { sb.Append(propertyBasket); sb.Append(delimiter); } if (sb.Length > delimiter.Length) { sb.Length -= delimiter.Length; } } else { sb.Append("Class has no properties"); } return(sb.ToString()); }
/// <summary> /// Builds up a string containing each property and value in the class decorated with the AuditAttribute. The string displays the property name, property friendly name and property value. /// </summary> /// <typeparam name="T">Class Type</typeparam> /// <param name="instance">Instance of the class</param> /// <param name="defaultValue">If no class properties are decorated with the <see cref="AuditAttribute"/> then a single entry will be added to the dictionary that is named 'DefaultValue' and will have the value of defaultValue.</param> /// <param name="delimiter">What delimiter do you want between each property? Defaults to comma; can pass others like Environment.NewLine, etc.</param> /// <param name="includeAllProperties">if set to <c>true</c> [include all attributes].</param> /// <returns>A string containing each property name, friendly name and value, separated by the delimiter and sorted by AuditAttribute.AuditSequence and then property name.</returns> public static String AuditToString <T>(T instance, String defaultValue, String delimiter = GlobalConstants.DefaultDelimiter, Boolean includeAllProperties = false) { var sb = new StringBuilder(2048); var list = new List <SortablePropertyBasket>(); var nonAttributedPropertyIndex = 0; foreach (var propInfo in PclReflection.GetPropertiesInfo(instance)) { var auditAttributes = propInfo.GetCustomAttributes <AuditAttribute>(false); var auditAttributeList = new List <AuditAttribute>(auditAttributes); AuditAttribute auditAttribute = null; if (auditAttributeList.Count == 1) { auditAttribute = auditAttributeList[0]; } if (auditAttribute != null) { list.Add(propInfo.GetValue(instance, null) != null ? new SortablePropertyBasket(auditAttribute.AuditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), propInfo.GetValue(instance, null).ToString()) : new SortablePropertyBasket(auditAttribute.AuditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), Null)); } else if (includeAllProperties && propInfo.Name != "Item") { nonAttributedPropertyIndex += 1; list.Add(propInfo.GetValue(instance, null) != null ? new SortablePropertyBasket(nonAttributedPropertyIndex, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), propInfo.GetValue(instance, null).ToString()) : new SortablePropertyBasket(nonAttributedPropertyIndex, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), Null)); } } if (list.Count > 0) { list.Sort(); foreach (SortablePropertyBasket propertyBasket in list) { sb.Append(propertyBasket); sb.Append(delimiter); } if (sb.Length > delimiter.Length) { sb.Length -= delimiter.Length; } } else { sb.Append(defaultValue); } return(sb.ToString()); }
/// <summary> /// Populates the passed in IDictionary with property's name and value in the class for properties decorated with the <see cref="AuditAttribute"/>. /// </summary> /// <typeparam name="T">Class type.</typeparam> /// <param name="instance">The instance.</param> /// <param name="defaultValue">If no class properties are decorated with the <see cref="AuditAttribute"/> then a single entry will be added to the dictionary that is named 'DefaultValue' and will have the value of defaultValue.</param> /// <param name="dictionary">Pass an IDictionary object that needs to be populated. This could be the Data property of an exception object that you want to populate, etc.</param> /// <param name="sortByPropertyName">If set to <c>SortByPropertyName.Yes</c> then output will be sorted by AuditAttribute.AuditSequence and then property name; otherwise no additional sorting is performed and properties; will be left in ordinal order.</param> /// <returns>The dictionary passed in populated with properties and values.</returns> public static IDictionary <String, String> ClassToIDictionary <T>(T instance, String defaultValue, IDictionary <String, String> dictionary, SortByPropertyName sortByPropertyName = SortByPropertyName.Yes) { var list = new List <SortablePropertyBasket>(); foreach (var propInfo in PclReflection.GetPropertiesInfo(instance)) { var auditAttributes = propInfo.GetCustomAttributes <AuditAttribute>(false); var auditAttributeList = new List <AuditAttribute>(auditAttributes); AuditAttribute auditAttribute = null; if (auditAttributeList.Count == 1) { auditAttribute = auditAttributeList[0]; } if (auditAttribute != null) { list.Add(new SortablePropertyBasket(One, propInfo.Name, String.Empty, propInfo.GetValue(instance, null).ToString())); } } if (list.Count > 0) { if (sortByPropertyName == SortByPropertyName.Yes) { list.Sort(); } foreach (SortablePropertyBasket propertyBasket in list) { dictionary.Add(propertyBasket.PropertyName, propertyBasket.Value); } } else { dictionary.Add(DefaultValue, defaultValue); } return(dictionary); }
/// <summary> /// Populates the dictionary with property's name and value in the class for properties decorated with the <see cref="AuditAttribute"/>. /// </summary> /// <typeparam name="T">Class type.</typeparam> /// <param name="instance">The instance.</param> /// <param name="defaultValue">If no class properties are decorated with the <see cref="AuditAttribute"/> then a single entry will be added to the dictionary that is named 'DefaultValue' and will have the value of defaultValue.</param>. /// <param name="dictionary">Pass an IDictionary object that needs to be populated. This could be the Data property of an exception object that you want to populate, etc.</param> /// <returns>IDictionary populated with properties and values.</returns> public static IDictionary <String, String> AuditToIDictionary <T>(T instance, String defaultValue, IDictionary <String, String> dictionary) { var list = new List <SortablePropertyBasket>(); foreach (var propInfo in PclReflection.GetPropertiesInfo(instance)) { var auditAttributes = propInfo.GetCustomAttributes <AuditAttribute>(false); var auditAttributeList = new List <AuditAttribute>(auditAttributes); AuditAttribute auditAttribute = null; if (auditAttributeList.Count == 1) { auditAttribute = auditAttributeList[0]; } if (auditAttribute != null) { list.Add(new SortablePropertyBasket(auditAttribute.AuditSequence, propInfo.Name, CamelCaseString.GetWords(propInfo.Name), propInfo.GetValue(instance, null).ToString())); } } if (list.Count > 0) { list.Sort(); foreach (var propertyBasket in list) { dictionary.Add(propertyBasket.PropertyName, propertyBasket.Value); } } else { dictionary.Add(DefaultValue, defaultValue); } return(dictionary); }