Beispiel #1
0
        public void ObservesAdoptedObject()
        {
            var obj = new Body {
                LeftArm = new Arm {
                    Thumb = new Digit {
                        LengthMM = 50
                    }
                }
            };

            var path = PropertyPaths <Body> .Get <int>(t => t.LeftArm.Thumb.LengthMM).Single();

            var observer = new PropertyPathObserver <Body>(path, obj);

            obj.LeftArm.Thumb = new Digit();

            int changesObserved = 0;

            observer.Changed += (sender, e) =>
            {
                changesObserved++;
            };

            obj.LeftArm.Thumb.LengthMM = 51;

            Assert.Equal(1, changesObserved);
        }
Beispiel #2
0
        public void InterpretsPropertyPathsCorrectly()
        {
            var paths = PropertyPaths <Outer> .Get <int>(o => o.Inner != null?o.Inner.Qty : 0);

            Assert.Equal(2, paths.Count);
            Assert.Contains("Inner", paths.Select(p => p.ToString()));
            Assert.Contains("Inner.Qty", paths.Select(p => p.ToString()));
        }
Beispiel #3
0
        public void ReducesLikePaths()
        {
            var paths = PropertyPaths <Outer> .Get(o => o.Inner.Qty > 6 && o.Inner.Qty < 10);

            var reduced = PropertyPath.Reduce(paths);

            Assert.Equal(1, reduced.Count);
            Assert.Equal("Inner.Qty", reduced.Single().ToString());
        }
Beispiel #4
0
        public void ObservesIntermediatePath()
        {
            var obj  = new Body();
            var path = PropertyPaths <Body> .Get <int>(t => t.LeftArm.Thumb.LengthMM).Single();

            var observer = new PropertyPathObserver <Body>(path, obj);

            int changesObserved = 0;

            observer.Changed += (sender, e) =>
            {
                changesObserved++;
            };

            obj.LeftArm = new Arm();

            Assert.Equal(1, changesObserved);
        }
        public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
        {
            string errorMessage;

            if (!Validate(out errorMessage))
            {
                throw new ArgumentException(errorMessage);
            }

            //Component will not be executed if the property is set to true
            if (Disabled)
            {
                return(pInMsg);
            }

            IBaseMessageContext context = pInMsg.Context;

            string[] PropertyPathList = PropertyPaths.Split(';');
            string[] HeaderNamesList  = HeaderNames.Split(';');

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < PropertyPathList.Length; i++)
            {
                sb.Append(HeaderNamesList[i] + ":" + (string)context.Read(new ContextProperty(PropertyPathList[i])));

                if (i < PropertyPathList.Length - 1)
                {
                    sb.Append(" ");
                }
            }
            string HttpHeaders = sb.ToString();

            if (PromoteProperty)
            {
                pInMsg.Context.Promote(new ContextProperty(DestinationPath), HttpHeaders);
            }
            else
            {
                pInMsg.Context.Write(new ContextProperty(DestinationPath), HttpHeaders);
            }
            return(pInMsg);
        }
Beispiel #6
0
        /// <summary>
        /// Determines whether or not a rule should execute.
        /// </summary>
        /// <param name="rule">The rule</param>
        /// <param name="propertyPath">Property path (eg Customer.Address.Line1)</param>
        /// <param name="context">Contextual information</param>
        /// <returns>Whether or not the validator can execute.</returns>
        public bool CanExecute(IValidationRule rule, string propertyPath, IValidationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (propertyPath == null)
            {
                throw new ArgumentNullException(nameof(propertyPath));
            }

            var dependencies = context.GetDependencies(propertyPath);

            if (rule != null)
            {
                dependencies = dependencies.Concat(rule.GetDependencies(context));
            }

            if (HasIndexers)
            {
                propertyPath = IndexerMatcher.Value.Replace(propertyPath, "[]");
                dependencies = dependencies.Select(propertyPath => IndexerMatcher.Value.Replace(propertyPath, "[]"));
            }

            // Validator selector only applies to the top level.
            // If we're running in a child context then this means that the child validator has already been selected
            // Because of this, we assume that the rule should continue (i.e. if the parent rule is valid, all children are valid)
            var isChildContext = context.IsChildContext;
            var cascadeEnabled = !context.RootContextData.ContainsKey(DisableCascadeKey);

            return(isChildContext && cascadeEnabled && !PropertyPaths.Any(x => x.Contains('.')) ||
                   rule is IIncludeRule ||
                   Matches(propertyPath) ||
                   dependencies.Any(Matches));

            bool Matches(string otherPath) => PropertyPaths.Any(path => path == otherPath ||
                                                                IsSubPath(otherPath, path) ||
                                                                IsSubPath(path, otherPath));
        }
Beispiel #7
0
 private string GetLine(T item)
 {
     return(string.Join(",", PropertyPaths.Select(x => GetPropertyValue(item, x).ToString()).ToArray()));
 }