表示一个可以用在条件表达式里的参数。
Пример #1
0
        public bool Apply(RuleParameter param, object paramValue, object inputValue)
        {
            Require.NotNull(param, "param");
            Require.NotNull(paramValue, "paramValue");
            Require.NotNull(inputValue, "inputValue");

            return paramValue.ToString().Contains(inputValue.ToString(), StringComparison.OrdinalIgnoreCase);
        }
Пример #2
0
        public override object ResolveValue(RuleParameter param, object dataContext)
        {
            var referenceResolver = TypeActivator.CreateInstance(_referenceResolverType) as IReferenceResolver;
            if (referenceResolver == null)
                throw new InvalidOperationException("Cannot resolve reference resolver type: " + _referenceResolverType + ".");

            return referenceResolver.Resolve(_referencingType, dataContext);
        }
        public override object ResolveValue(RuleParameter param, object dataContext)
        {
            if (dataContext == null)
            {
                return null;
            }

            return _property.GetValue(dataContext, null);
        }
Пример #4
0
        public bool Apply(RuleParameter param, object paramValue, object inputValue)
        {
            if (paramValue is String && inputValue is String)
            {
                return (paramValue as String).Equals((inputValue as String), StringComparison.OrdinalIgnoreCase);
            }

            return paramValue.Equals(inputValue);
        }
Пример #5
0
        public override object ResolveValue(RuleParameter param, object dataContext)
        {
            if (dataContext == null)
            {
                return(null);
            }

            return(_property.GetValue(dataContext, null));
        }
Пример #6
0
        public bool Apply(RuleParameter param, object paramValue, object inputValue)
        {
            Require.NotNull(param, "param");
            Require.NotNull(paramValue, "paramValue");
            Require.That(paramValue is IComparable, "paramValue", "Require comparable parameter value.");
            Require.NotNull(inputValue, "inputValue");
            Require.That(inputValue is IComparable, "inputValue", "Require comparable input value.");

            return ((IComparable)paramValue).CompareTo((IComparable)inputValue) > 0;
        }
        public override object ResolveValue(RuleParameter param, object dataContext)
        {
            var referenceResolver = TypeActivator.CreateInstance(_referenceResolverType) as IReferenceResolver;

            if (referenceResolver == null)
            {
                throw new InvalidOperationException("Cannot resolve reference resolver type: " + _referenceResolverType + ".");
            }

            return(referenceResolver.Resolve(_referencingType, dataContext));
        }
        // 此方法用于获取自定义条件参数的值
        private string GetCustomerCompnay(RuleParameter parameter, object dataContext)
        {
            var order = (Order)dataContext;

            // 返回其 "Company" 这个 CustomField 的值
            var field = order.Customer.CustomFields.FirstOrDefault(f => f.Name == "Company");
            if (field == null)
            {
                return null;
            }

            return field.Value;
        }
        public override object ResolveValue(RuleParameter param, object dataContext)
        {
            var value = dataContext;
            foreach (var resolver in _resolvers)
            {
                value = resolver.ResolveValue(param, value);
                if (value == null)
                {
                    break;
                }
            }

            return value;
        }
        public override object ResolveValue(RuleParameter param, object dataContext)
        {
            var value = dataContext;

            foreach (var resolver in _resolvers)
            {
                value = resolver.ResolveValue(param, value);
                if (value == null)
                {
                    break;
                }
            }

            return(value);
        }
Пример #11
0
        public RuleParameterModel(RuleParameter param)
            : this()
        {
            Name = param.Name;
            DisplayName = param.Name;
            ValueType = param.ValueType.FullName;
            IsNumberValue = param.ValueType.IsNumericType();

            foreach (var @operator in param.SupportedOperators)
            {
                SupportedOperators.Add(new ComparisonOperatorModel(@operator));
            }

            if (param.ValueSource != null)
            {
                Values = param.ValueSource.GetValues(param).Select(x => new SelectListItem
                {
                    Text = x.Key,
                    Value = x.Value
                })
                .ToList();
            }
        }
 public IDictionary <string, string> GetValues(RuleParameter param)
 {
     return(_values);
 }
Пример #13
0
 public override object ResolveValue(RuleParameter param, object dataContext)
 {
     return _resolveValue(param, dataContext);
 }
Пример #14
0
 /// <summary>
 /// Resolve the parameter value of the specified parameter from the context object.
 /// </summary>
 /// <param name="param">The parameter to resolve value.</param>
 /// <param name="dataContext">The context object.</param>
 /// <returns>The value of the parameter.</returns>
 public abstract object ResolveValue(RuleParameter param, object dataContext);
 public override object ResolveValue(RuleParameter param, object dataContext)
 {
     return(_resolveValue(param, dataContext));
 }
 /// <summary>
 /// Resolve the parameter value of the specified parameter from the context object.
 /// </summary>
 /// <param name="param">The parameter to resolve value.</param>
 /// <param name="dataContext">The context object.</param>
 /// <returns>The value of the parameter.</returns>
 public abstract object ResolveValue(RuleParameter param, object dataContext);
 public IDictionary<string, string> GetValues(RuleParameter param)
 {
     return _values;
 }
Пример #18
0
 public bool Apply(RuleParameter param, object paramValue, object inputValue)
 {
     return !ComparisonOperators.GreaterThan.Apply(param, paramValue, inputValue);
 }
        private List<RuleParameter> FindConditionParameters(Type containerType, RuleParameterValueResolver containerResolver, string prefix)
        {
            var parameters = new List<RuleParameter>();

            foreach (var property in containerType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                var refAttr = property.GetCustomAttribute<ReferenceAttribute>(false);
                if (refAttr != null)
                {
                    var resolver = new ChainedRuleParameterValueResolver().Chain(containerResolver, new PropertyBackedRuleParameterValueResolver(property));

                    var referencingType = refAttr.ReferencingType ?? property.PropertyType;
                    // Indirect reference
                    if (referencingType != property.PropertyType)
                    {
                        if (refAttr.ReferenceResolver == null)
                            throw new InvalidOperationException("Indirect reference must speicify a reference resolver. Property: " + property.ReflectedType.FullName + "." + property.Name + ".");

                        resolver.Chain(new IndirectReferenceAdapter(referencingType, refAttr.ReferenceResolver));
                    }

                    var newPrefix = prefix;

                    // refAttr.Prefix == null: Generate a default prefix
                    // refAttr.Prefix == String.Empty: Do not use prefix
                    // otherwise, use the specified prefix
                    if (refAttr.Prefix != null)
                    {
                        if (refAttr.Prefix.Length > 0)
                        {
                            newPrefix = prefix + (refAttr.Prefix.EndsWith(".") ? refAttr.Prefix : refAttr.Prefix + ".");
                        }
                    }
                    else
                    {
                        // Try to get a smart default preifx if developer doesn't specify one.
                        // If the property name is like BrandId,
                        // then we use 'Brand' instead of 'BrandId'.
                        // So when we are investigating Brand.Name property, we can get a better parameter name 'BrandName' instead of 'BrandIdName'
                        if (property.Name.EndsWith("Id"))
                        {
                            newPrefix = prefix + property.Name.Substring(0, property.Name.Length - 2);
                        }
                        else
                        {
                            newPrefix = prefix + property.Name;
                        }

                        newPrefix = newPrefix + ".";
                    }

                    parameters.AddRange(FindConditionParameters(referencingType, resolver, newPrefix));

                    // Add also extended parameters
                    foreach (var provider in Providers)
                    {
                        if (provider.GetType() != typeof(DefaultRuleParameterProvider))
                        {
                            var additionalParams = provider.GetParameters(referencingType);
                            foreach (var param in additionalParams)
                            {
                                var valueResolver = new ChainedRuleParameterValueResolver();
                                valueResolver.Chain(resolver);
                                valueResolver.Chain(param.ValueResolver);

                                var adaptedParam = new RuleParameter(newPrefix + param.Name, param.ValueType, valueResolver, param.SupportedOperators)
                                {
                                    ValueSource = param.ValueSource
                                };

                                parameters.Add(adaptedParam);
                            }
                        }
                    }
                }
                else
                {
                    var paramAttr = property.GetCustomAttribute<ParamAttribute>(false);
                    if (paramAttr != null)
                    {
                        parameters.Add(CreateConditionParameter(containerType, containerResolver, prefix, property, paramAttr));
                    }
                }
            }

            return parameters;
        }
        private List <RuleParameter> FindConditionParameters(Type containerType, RuleParameterValueResolver containerResolver, string prefix)
        {
            var parameters = new List <RuleParameter>();

            foreach (var property in containerType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                var refAttr = property.GetCustomAttribute <ReferenceAttribute>(false);
                if (refAttr != null)
                {
                    var resolver = new ChainedRuleParameterValueResolver().Chain(containerResolver, new PropertyBackedRuleParameterValueResolver(property));

                    var referencingType = refAttr.ReferencingType ?? property.PropertyType;
                    // Indirect reference
                    if (referencingType != property.PropertyType)
                    {
                        if (refAttr.ReferenceResolver == null)
                        {
                            throw new InvalidOperationException("Indirect reference must speicify a reference resolver. Property: " + property.ReflectedType.FullName + "." + property.Name + ".");
                        }

                        resolver.Chain(new IndirectReferenceAdapter(referencingType, refAttr.ReferenceResolver));
                    }

                    var newPrefix = prefix;

                    // refAttr.Prefix == null: Generate a default prefix
                    // refAttr.Prefix == String.Empty: Do not use prefix
                    // otherwise, use the specified prefix
                    if (refAttr.Prefix != null)
                    {
                        if (refAttr.Prefix.Length > 0)
                        {
                            newPrefix = prefix + (refAttr.Prefix.EndsWith(".") ? refAttr.Prefix : refAttr.Prefix + ".");
                        }
                    }
                    else
                    {
                        // Try to get a smart default preifx if developer doesn't specify one.
                        // If the property name is like BrandId,
                        // then we use 'Brand' instead of 'BrandId'.
                        // So when we are investigating Brand.Name property, we can get a better parameter name 'BrandName' instead of 'BrandIdName'
                        if (property.Name.EndsWith("Id"))
                        {
                            newPrefix = prefix + property.Name.Substring(0, property.Name.Length - 2);
                        }
                        else
                        {
                            newPrefix = prefix + property.Name;
                        }

                        newPrefix = newPrefix + ".";
                    }

                    parameters.AddRange(FindConditionParameters(referencingType, resolver, newPrefix));

                    // Add also extended parameters
                    foreach (var provider in Providers)
                    {
                        if (provider.GetType() != typeof(DefaultRuleParameterProvider))
                        {
                            var additionalParams = provider.GetParameters(referencingType);
                            foreach (var param in additionalParams)
                            {
                                var valueResolver = new ChainedRuleParameterValueResolver();
                                valueResolver.Chain(resolver);
                                valueResolver.Chain(param.ValueResolver);

                                var adaptedParam = new RuleParameter(newPrefix + param.Name, param.ValueType, valueResolver, param.SupportedOperators)
                                {
                                    ValueSource = param.ValueSource
                                };

                                parameters.Add(adaptedParam);
                            }
                        }
                    }
                }
                else
                {
                    var paramAttr = property.GetCustomAttribute <ParamAttribute>(false);
                    if (paramAttr != null)
                    {
                        parameters.Add(CreateConditionParameter(containerType, containerResolver, prefix, property, paramAttr));
                    }
                }
            }

            return(parameters);
        }
Пример #21
0
 public SwitchCaseRule(RuleParameter parameter)
 {
     Parameter = parameter;
 }