public static TResult CreateElseNode <TResult>(FluentScope fluentScope)
        {
            var tmpScope = fluentScope.CreateChild();

            tmpScope.Set(RegistryKeys.FluentScopeAction, new FluentScopeAction(BuilderHelper.SwitchToElse));
            return(CreateFluentNode <TResult>(tmpScope));
        }
        public static TEndIf CreateEndIf <TEndIf>(FluentScope fluentScope)
        {
            var tmpScope = fluentScope.CreateChild(fluentScope.Get(RegistryKeys.IfScope));

            //TODO: Make all IFluentNodes have a new() constructor and an Initialize(FluentScope2) method
            return(CreateFluentNode <TEndIf>(tmpScope));
        }
        public static void RegisterMessage(FluentScope fluentScope, FluentBuilderToken token)
        {
            var sourceType   = fluentScope.Get(RegistryKeys.SourceType);
            var messageEntry = new MessageEntry(
                sourceType,
                fluentScope.Get(RegistryKeys.SetupExpression, Utilities.ReturnSelf(sourceType)),
                fluentScope.Get(RegistryKeys.Rule, null),
                fluentScope.Get(RegistryKeys.Message));

            token.ErrorResolver.AddEntry(messageEntry);
        }
        public static ForClassElseEndIf <T, ENDIF> CreateIf <T, ENDIF>(FluentScope fluentScope, LambdaExpression conditionalExpression)
        {
            var tmpScope = fluentScope.CreateChild();

            tmpScope.Set(RegistryKeys.FluentScopeAction, new FluentScopeAction(BuilderHelper.RegisterConditional));
            tmpScope.Set(RegistryKeys.ConditionalExpression, conditionalExpression);
            tmpScope.Set(RegistryKeys.IfScope, fluentScope);

            var result = new ForClassElseEndIf <T, ENDIF>(tmpScope);

            return(result);
        }
        public static void RegisterEnumerableComposition(FluentScope fluentScope, FluentBuilderToken token)
        {
            var usingEngine           = fluentScope.Get(RegistryKeys.UsingEngine, token.RootEngine);
            var compositionExpression = fluentScope.Get(RegistryKeys.SetupExpression);
            var sourceType            = compositionExpression.Parameters[0].Type;
            var resultType            = compositionExpression.ReturnType;

            var result = (IRuleInvoker)Utilities.CreateType(typeof(EnumerableCompositionInvoker <,>), sourceType, resultType)
                         .CreateInstance(usingEngine, compositionExpression);

            token.CurrentEngine.InvokerRegistry.RegisterInvoker(result);
        }
        public static void RegisterConditional(FluentScope fluentScope, FluentBuilderToken token)
        {
            var conditionalExpression = fluentScope.Get(RegistryKeys.ConditionalExpression);
            var sourceType            = conditionalExpression.Parameters[0].Type;
            var engine           = token.CurrentEngine;
            var conditionalToken = token.Condition();

            var invoker = (IRuleInvoker)Utilities.CreateType(typeof(ConditionalInvoker <>), sourceType)
                          .CreateInstance(conditionalExpression, conditionalToken.IfTrueEngine, conditionalToken.IfFalseEngine);

            engine.InvokerRegistry.RegisterInvoker(invoker);
        }
        public ForClass <T> For <T>()
        {
            var tmpScope = new FluentScope();

            tmpScope.Set(RegistryKeys.SourceType, typeof(T));


            var result = new ForClass <T>(tmpScope);

            _forScopes.Add(tmpScope);
            return(result);
        }
Example #8
0
        /// <summary>
        /// Creates a child node
        /// </summary>
        /// <param name="getInheritedValuesFrom">Node to get inherited values from</param>
        /// <returns></returns>
        public FluentScope CreateChild(FluentScope getInheritedValuesFrom)
        {
            if (getInheritedValuesFrom == null)
            {
                throw new ArgumentNullException("getValuesFrom");
            }
            if (_child != null)
            {
                throw new InvalidOperationException("Cannot call CreateChild when Child already exists.");
            }

            var values = getInheritedValuesFrom._values.ToDictionary(kp => kp.Key, kp => kp.Value is IFluentScopeValue ? ((IFluentScopeValue)kp.Value).Copy() : kp.Value);
            var result = new FluentScope(this, values);

            _child = result;
            return(result);
        }
        public static void RegisterRule(FluentScope fluentScope, FluentBuilderToken token)
        {
            var rule                            = fluentScope.Get(RegistryKeys.Rule);
            var sourceType                      = fluentScope.Get(RegistryKeys.SourceType);
            var setupExpression                 = fluentScope.Get(RegistryKeys.SetupExpression, Utilities.ReturnSelf(sourceType));
            var valueResolverFactory            = token.ValueResolverFactory;
            var isCrossField                    = fluentScope.Get(RegistryKeys.IsCrossField, false);
            var expressionToInvokeForValidation = isCrossField ? Utilities.ReturnSelf(sourceType) : fluentScope.Get(RegistryKeys.SetupExpression);
            var resultType                      = expressionToInvokeForValidation.ReturnType;
            var valueToValidateResolver         = valueResolverFactory.CreateResolver(expressionToInvokeForValidation);

            var culpritResolver = fluentScope.Get(RegistryKeys.CulpritResolver, token.CulpritResolverFactory.Create(null, setupExpression));
            var result          = (IRuleInvoker)Utilities.CreateType(typeof(RuleInvoker <,>), sourceType, resultType)
                                  .CreateInstance(rule
                                                  , valueToValidateResolver
                                                  , new EquatableExpression(setupExpression)
                                                  , culpritResolver
                                                  );

            token.CurrentEngine.InvokerRegistry.RegisterInvoker(result);
        }
Example #10
0
 internal SetupClassEndIf(FluentScope fluentScope)
 {
     _fluentScope = fluentScope;
 }
Example #11
0
 object IFluentNode.GetSelf(FluentScope fluentScope)
 {
     return(new SetupClassEndIf <T, R, ENDIF>(fluentScope));
 }
 object IFluentNode.GetSelf(FluentScope fluentScope)
 {
     return(new SetupClass <T, R>(fluentScope));
 }
 internal ForClassElseEndIf(FluentScope fluentScope)
 {
     _fluentScope = fluentScope;
 }
 object IFluentNode.GetSelf(FluentScope fluentScope)
 {
     return(new ForClassElseEndIf <T, ENDIF>(fluentScope));
 }
        private static object CreateFluentNode(Type type, FluentScope scope)
        {
            var binding = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.CreateInstance;

            return(type.GetConstructors(binding).First(c => c.GetParameters().Any(p => p.ParameterType == typeof(FluentScope))).Invoke(new[] { scope }));
        }
Example #16
0
 internal ForClass(FluentScope fluentScope)
 {
     _fluentScope = fluentScope;
 }
 public static T CreateFluentNode <T>(FluentScope scope)
 {
     return((T)CreateFluentNode(typeof(T), scope));
 }
        public static TResult CreateMustPassRule <TRule, TResult>(IRule <TRule> rule, FluentScope fluentScope, bool isCrossField)
        {
            var tmpScope = fluentScope.CreateChild();

            tmpScope.Set(RegistryKeys.FluentScopeAction, new FluentScopeAction(BuilderHelper.RegisterRule));
            tmpScope.Set(RegistryKeys.Rule, rule);
            tmpScope.Set(RegistryKeys.IsCrossField, isCrossField);

            return(CreateFluentNode <TResult>(tmpScope));
        }
Example #19
0
 private FluentScope(FluentScope parent, Dictionary <string, object> values)
 {
     _parent = parent;
     _values = values;
 }
Example #20
0
 object IFluentNode.GetSelf(FluentScope fluentScope)
 {
     return(new ForClass <T>(fluentScope));
 }
 public static void SwitchToElse(FluentScope fluentScope, FluentBuilderToken token)
 {
     token.Else();
 }
 public void Execute(FluentScope scope, FluentBuilderToken token)
 {
     _action(scope, token);
 }