Ejemplo n.º 1
0
        /// <summary>
        /// This code gets called on the first time this Object type is constructed
        /// </summary>
        void AddSharedBusinessRules()
        {
            lock (_LockObject) {
                var entityType = this.GetType();
                if (!SharedValidationRules.RulesExistFor(entityType))
                {
                    ValidationRulesManager mgrValidation = SharedValidationRules.GetManager(entityType);
                    if (mgrValidation.RulesLoaded)
                    {
                        return;
                    }
                    mgrValidation.SetRulesLoaded();
                    CharacterFormattingRulesManager mgrCharacterCasing = SharedCharacterFormattingRules.GetManager(entityType);

                    foreach (PropertyInfo prop in entityType.GetProperties())
                    {
                        foreach (BaseValidatorAttribute atr in prop.GetCustomAttributes(typeof(BaseValidatorAttribute), false))
                        {
                            mgrValidation.AddRule(atr, prop.Name);
                        }

                        foreach (CharacterFormattingAttribute atr in prop.GetCustomAttributes(typeof(CharacterFormattingAttribute), false))
                        {
                            mgrCharacterCasing.AddRule(prop.Name, atr.CharacterCasing, atr.RemoveSpace, atr.PhoneExtension);
                        }
                    }

                    AddSharedBusinessValidationRules(mgrValidation);
                    AddSharedCharacterCasingFormattingRules(mgrCharacterCasing);
                }
            }
        }
        /// <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);
                }
            }
        }
Ejemplo n.º 3
0
        static CharacterCasesFixture()
        {
            Type type = typeof(CustomerCC);
            var  mgr  = SharedCharacterFormattingRules.GetManager(type);

            foreach (PropertyInfo prop in type.GetProperties())
            {
                foreach (CharacterFormattingAttribute atr in prop.GetCustomAttributes(typeof(CharacterFormattingAttribute), false))
                {
                    mgr.AddRule(prop.Name, atr.CharacterCasing, atr.RemoveSpace, atr.PhoneExtension);
                }
            }
        }
        /// <summary>
        /// Called by in business entity sub-classes in their property setters to set the value of the property.
        /// If the business Object is not in a loading state, this method performs validation on the property.
        /// This method also runs the String compression to remove extra spaces if the property is attributed with the CharacterFormattingAttribute.
        ///
        /// <example>Example:
        /// <code>
        ///   Set(ByVal Value As String)
        ///       MyBase.SetPropertyValue(ref _strSL_DatabaseConnection, Value)
        ///   End Set
        /// </code>
        /// </example>
        /// </summary>
        /// <param name="currentValue">Current property value.</param>
        /// <param name="newValue">Value argument from the property Setter Set.</param>
        /// <param name="propertyName">Property Name.</param>
        protected void SetPropertyValue(ref String currentValue, String newValue, [CallerMemberName] String propertyName = null)
        {
            if (currentValue == null)
            {
                if (newValue == null)
                {
                    return;
                }
            }
            else if (newValue != null && currentValue.Equals(newValue))
            {
                return;
            }

            if (!this.IsLoading)
            {
                this.IsDirty = true;
                this.BeforePropertyChanged(propertyName);

                //only apply character casing rules after the Object is loaded.
                var characterFormat = SharedCharacterFormattingRules.GetManager(this.GetType()).GetRuleForProperty(propertyName);

                if (characterFormat != null)
                {
                    currentValue = characterFormat.CharacterCasing != CharacterCasing.None ? FormatText.ApplyCharacterCasing(newValue, characterFormat.CharacterCasing) : newValue;
                    if (!String.IsNullOrWhiteSpace(currentValue) && characterFormat.RemoveSpace == RemoveSpace.MultipleSpaces)
                    {
                        currentValue = Regex.Replace(currentValue, @"\s+", " ");
                    }
                    else if (!String.IsNullOrWhiteSpace(currentValue) && characterFormat.RemoveSpace == RemoveSpace.AllSpaces)
                    {
                        currentValue = currentValue.Replace(" ", String.Empty).Trim();
                    }
                }
                else
                {
                    currentValue = newValue;
                }

                CheckRulesForProperty(propertyName);
                InternalRaisePropertyChanged(propertyName);

                this.AfterPropertyChanged(propertyName);
            }
            else
            {
                //since we are loading, just set the value
                currentValue = newValue;
            }
        }
Ejemplo n.º 5
0
        CharacterFormattingRulesManager GetCharacterFormattingRulesManagerForTarget <T>(T target) where T : class
        {
            CharacterFormattingRulesManager characterFormattingRulesManager = SharedCharacterFormattingRules.GetManager(this.GetType());

            if (!characterFormattingRulesManager.RulesLoaded)
            {
                lock (LockObject) {
                    foreach (PropertyInfo prop in target.GetType().GetProperties())
                    {
                        if (prop.GetCustomAttribute(typeof(CharacterFormattingAttribute), false) is CharacterFormattingAttribute characterFormattingAttribute)
                        {
                            characterFormattingRulesManager.AddRule(characterFormattingAttribute, prop.Name);
                        }
                    }
                }
                characterFormattingRulesManager.SetRulesLoaded();
            }

            return(characterFormattingRulesManager);
        }