Exemplo n.º 1
0
        public static IFluentContinuation <TViewModel, TPropertyValue> MaxLength <TViewModel, TPropertyValue>(
            this IFluentOperator <TViewModel, TPropertyValue> instance, int maxLength)
            where TViewModel : ViewModelBase
            where TPropertyValue : IComparable <string>, IEquatable <string>
        {
            var implementerInstance = (FluentImplementer <TViewModel, TPropertyValue>)instance;

            implementerInstance.EnsureContextCurrentPropertyIsNotNull();

            if (maxLength <= 0)
            {
                throw new InvalidOperationException("maxLength must be greater than 0");
            }

            var evaluatedValueProvider = new Func <object>(() =>
            {
                var value = (string)implementerInstance.Context.CurrentProperty.GetValue();
                return(value == null ? 0 : value.Length);
            });
            var valueProvider = new Func <object>(() => maxLength);

            implementerInstance.AddToken(ExpressionNode.OpenParenthesis());
            implementerInstance.AddToken(ExpressionNode.LessThanGeneric(evaluatedValueProvider, valueProvider));
            implementerInstance.AddToken(ExpressionNode.Or());
            implementerInstance.AddToken(ExpressionNode.EqualToGeneric(evaluatedValueProvider, valueProvider));
            implementerInstance.AddToken(ExpressionNode.CloseParenthesis());
            return((IFluentContinuation <TViewModel, TPropertyValue>)instance);
        }