Exemplo n.º 1
0
        ///<summary>
        ///  Uses the action pipeline to invoke the method.
        ///</summary>
        ///<param name="target"> The object instance to invoke the method on. </param>
        ///<param name="methodName"> The name of the method to invoke. </param>
        ///<param name="view"> The view. </param>
        ///<param name="source"> The source of the invocation. </param>
        ///<param name="eventArgs"> The event args. </param>
        ///<param name="parameters"> The method parameters. </param>
        public static void Invoke(object target, string methodName, DependencyObject view = null,
                                  FrameworkElement source = null, object eventArgs = null, object[] parameters = null)
        {
            var context = new ActionExecutionContext
            {
                Target = target,
                Method = target.GetType().GetMethod(methodName),
#if WINFORMS || WEBGUI
                Message = new ActionMessage(source, null, methodName),
#else
                Message = new ActionMessage {
                    MethodName = methodName
                },
#endif
                View      = view,
                Source    = source,
                EventArgs = eventArgs
            };

            if (parameters != null)
            {
                parameters.Apply(x => context.Message.Parameters.Add(x as Parameter ?? new Parameter {
                    Value = x
                }));
            }

            ActionMessage.InvokeAction(context);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Called by the bootstrapper's constructor at runtime to start the framework.
        /// </summary>
        protected virtual void StartRuntime()
        {
#if WEBGUI
            Execute.ResetWithoutDispatcher();
#else
            Execute.InitializeWithDispatcher();
#endif
            EventAggregator.DefaultPublicationThreadMarshaller = Execute.OnUIThread;

            EventAggregator.HandlerResultProcessing = (target, result) =>
            {
                var coroutine = result as IEnumerable <IResult>;
                if (coroutine != null)
                {
                    var viewAware = target as IViewAware;
                    var view      = viewAware != null?viewAware.GetView() : null;

                    var context = new ActionExecutionContext {
                        Target = target, View = (DependencyObject)view
                    };

                    Coroutine.BeginExecute(coroutine.GetEnumerator(), context);
                }
            };

#if WEBGUI
            var selectedAssemblies = SelectAssemblies();
            foreach (var selectedAssembly in selectedAssemblies)
            {
                if (!AssemblySource.Instance.Contains(selectedAssembly))
                {
                    AssemblySource.Instance.AddRange(new[] { selectedAssembly });
                }
            }
#else
            AssemblySource.Instance.AddRange(SelectAssemblies());
#endif

            if (useApplication)
            {
#if WINFORMS || WEBGUI
                //Application = System.Windows.Forms.Application;
#else
                Application = Application.Current;
#endif
                PrepareApplication();
            }

            Configure();
            IoC.GetInstance     = GetInstance;
            IoC.GetAllInstances = GetAllInstances;
            IoC.BuildUp         = BuildUp;
        }
Exemplo n.º 3
0
        private void UpdateContext()
        {
            if (context != null)
            {
                context.Dispose();
            }

            context = new ActionExecutionContext
            {
                Message = this,
                Source  = AssociatedObject
            };

            PrepareContext(context);
            UpdateAvailabilityCore();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Executes a coroutine.
        /// </summary>
        /// <param name="coroutine">The coroutine to execute.</param>
        /// <param name="context">The context to execute the coroutine within.</param>
        /// /// <param name="callback">The completion callback for the coroutine.</param>
        public static void BeginExecute(IEnumerator <IResult> coroutine, ActionExecutionContext context = null,
                                        EventHandler <ResultCompletionEventArgs> callback = null)
        {
            Log.Info("Executing coroutine.");

            var enumerator = CreateParentEnumerator(coroutine);

            IoC.BuildUp(enumerator);

            if (callback != null)
            {
                ExecuteOnCompleted(enumerator, callback);
            }

            ExecuteOnCompleted(enumerator, Completed);
            enumerator.Execute(context ?? new ActionExecutionContext());
        }
Exemplo n.º 5
0
        public void Execute(ActionExecutionContext context)
        {
            var view = context.View.Object as Control;
            while (view != null)
            {
                var indicator = view as IHaveBusyIndicator;
                if (indicator != null)
                {
                    indicator.Indicator.IsBusy = !_hide;
                    indicator.Indicator.BusyContent = _message;
                    break;
                }
                view = view.Parent;
            }

            if (view == null)
            {
                var queue = new Queue<Control>();
#if WINFORMS
                queue.Enqueue(Form.ActiveForm);
#else
                queue.Enqueue(ApplicationContext.WebGUIActiveForm);
#endif
                while (queue.Count > 0)
                {
                    var current = queue.Dequeue();
                    if (current == null)
                        continue;

                    var indicator = current as BusyIndicator;
                    if (indicator != null)
                    {
                        indicator.IsBusy = !_hide;
                        break;
                    }

                    var count = current.Controls.Count;
                    for (var i = 0; i < count; i++)
                    {
                        queue.Enqueue(current.Controls[i]);
                    }
                }
            }

            Completed(this, new ResultCompletionEventArgs());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Determines the parameters that a method should be invoked with.
        /// </summary>
        /// <param name="context">The action execution context.</param>
        /// <param name="requiredParameters">The parameters required to complete the invocation.</param>
        /// <returns>The actual parameter values.</returns>
        public static object[] DetermineParameters(ActionExecutionContext context, ParameterInfo[] requiredParameters)
        {
            var providedValues = context.Message.Parameters.Select(x => x.Value).ToArray();
            var finalValues = new object[requiredParameters.Length];

            for (int i = 0; i < requiredParameters.Length; i++)
            {
                var parameterType = requiredParameters[i].ParameterType;
                var parameterValue = providedValues[i];
                var parameterAsString = parameterValue as string;

                if (parameterAsString != null)
                    finalValues[i] = CoerceValue(parameterType,
                        EvaluateParameter(parameterAsString, parameterType, context), context);
                else finalValues[i] = CoerceValue(parameterType, parameterValue, context);
            }

            return finalValues;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Try to find a candidate for guard function, having:
        ///		- a name in the form "CanXXX"
        ///		- no generic parameters
        ///		- a bool return type
        ///		- no parameters or a set of parameters corresponding to the action method
        /// </summary>
        /// <param name="context">The execution context</param>
        /// <returns>A MethodInfo, if found; null otherwise</returns>
        private static MethodInfo TryFindGuardMethod(ActionExecutionContext context)
        {
            var guardName  = "Can" + context.Method.Name;
            var targetType = context.Target.GetType();
            var guard      = targetType.GetMethod(guardName);

            if (guard == null)
            {
                return(null);
            }
            if (guard.ContainsGenericParameters)
            {
                return(null);
            }
            if (typeof(bool) != guard.ReturnType)
            {
                return(null);
            }

            var guardPars  = guard.GetParameters();
            var actionPars = context.Method.GetParameters();

            if (guardPars.Length == 0)
            {
                return(guard);
            }
            if (guardPars.Length != actionPars.Length)
            {
                return(null);
            }

            var comparisons = guardPars.Zip(
                context.Method.GetParameters(),
                (x, y) => x.ParameterType == y.ParameterType
                );

            if (comparisons.Any(x => !x))
            {
                return(null);
            }

            return(guard);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Determines the parameters that a method should be invoked with.
        /// </summary>
        /// <param name="context">The action execution context.</param>
        /// <param name="requiredParameters">The parameters required to complete the invocation.</param>
        /// <returns>The actual parameter values.</returns>
        public static object[] DetermineParameters(ActionExecutionContext context, ParameterInfo[] requiredParameters)
        {
            var providedParameters = context.Message.Parameters.Select(x => x).ToArray();
            var finalValues = new object[requiredParameters.Length];

            for (int i = 0; i < requiredParameters.Length; i++)
            {
                var parameterType = requiredParameters[i].ParameterType;
                var parameterValue = providedParameters[i].Value;
                var parameterAsString = parameterValue as string;

                if (parameterAsString != null)
                {
                    if (providedParameters[i].MustEvaluate)
                    {
                        var parameterMembers = Parser.SplitParameter(parameterAsString);
                        var sourceControl = EvaluateParameter(parameterMembers[0], parameterType, context);
                        if (parameterMembers[1] == null)
                        {
                            finalValues[i] = CoerceValue(parameterType, sourceControl, context);
                        }
                        else
                        {
                            var sourceProperty = sourceControl.GetType().GetProperty(parameterMembers[1]);
                            var propertyValue = sourceProperty.GetValue(sourceControl, null);
                            finalValues[i] = CoerceValue(parameterType, propertyValue, context);
                        }
                    }
                    else
                    {
                        finalValues[i] = CoerceValue(parameterType,
                            EvaluateParameter(parameterAsString, parameterType, context), context);
                    }
                }
                else
                {
                    finalValues[i] = CoerceValue(parameterType, parameterValue, context);
                }
            }

            return finalValues;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Determines the parameters that a method should be invoked with.
        /// </summary>
        /// <param name="context">The action execution context.</param>
        /// <param name="requiredParameters">The parameters required to complete the invocation.</param>
        /// <returns>The actual parameter values.</returns>
        public static object[] DetermineParameters(ActionExecutionContext context, ParameterInfo[] requiredParameters)
        {
            var providedParameters = context.Message.Parameters.Select(x => x).ToArray();
            var finalValues        = new object[requiredParameters.Length];

            for (int i = 0; i < requiredParameters.Length; i++)
            {
                var parameterType     = requiredParameters[i].ParameterType;
                var parameterValue    = providedParameters[i].Value;
                var parameterAsString = parameterValue as string;

                if (parameterAsString != null)
                {
                    if (providedParameters[i].MustEvaluate)
                    {
                        var parameterMembers = Parser.SplitParameter(parameterAsString);
                        var sourceControl    = EvaluateParameter(parameterMembers[0], parameterType, context);
                        if (parameterMembers[1] == null)
                        {
                            finalValues[i] = CoerceValue(parameterType, sourceControl, context);
                        }
                        else
                        {
                            var sourceProperty = sourceControl.GetType().GetProperty(parameterMembers[1]);
                            var propertyValue  = sourceProperty.GetValue(sourceControl, null);
                            finalValues[i] = CoerceValue(parameterType, propertyValue, context);
                        }
                    }
                    else
                    {
                        finalValues[i] = CoerceValue(parameterType,
                                                     EvaluateParameter(parameterAsString, parameterType, context), context);
                    }
                }
                else
                {
                    finalValues[i] = CoerceValue(parameterType, parameterValue, context);
                }
            }

            return(finalValues);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Determines the parameters that a method should be invoked with.
        /// </summary>
        /// <param name="context">The action execution context.</param>
        /// <param name="requiredParameters">The parameters required to complete the invocation.</param>
        /// <returns>The actual parameter values.</returns>
        public static object[] DetermineParameters(ActionExecutionContext context, ParameterInfo[] requiredParameters)
        {
            var providedValues = context.Message.Parameters.Select(x => x.Value).ToArray();
            var finalValues    = new object[requiredParameters.Length];

            for (int i = 0; i < requiredParameters.Length; i++)
            {
                var parameterType     = requiredParameters[i].ParameterType;
                var parameterValue    = providedValues[i];
                var parameterAsString = parameterValue as string;

                if (parameterAsString != null)
                {
                    finalValues[i] = CoerceValue(parameterType,
                                                 EvaluateParameter(parameterAsString, parameterType, context), context);
                }
                else
                {
                    finalValues[i] = CoerceValue(parameterType, parameterValue, context);
                }
            }

            return(finalValues);
        }
Exemplo n.º 11
0
 /// <summary>
 ///   Executes the result using the specified context.
 /// </summary>
 /// <param name = "context">The context.</param>
 public void Execute(ActionExecutionContext context)
 {
     this.context = context;
     ChildCompleted(null, new ResultCompletionEventArgs());
 }
Exemplo n.º 12
0
        ///<summary>
        ///  Uses the action pipeline to invoke the method.
        ///</summary>
        ///<param name="target"> The object instance to invoke the method on. </param>
        ///<param name="methodName"> The name of the method to invoke. </param>
        ///<param name="view"> The view. </param>
        ///<param name="source"> The source of the invocation. </param>
        ///<param name="eventArgs"> The event args. </param>
        ///<param name="parameters"> The method parameters. </param>
        public static void Invoke(object target, string methodName, DependencyObject view = null,
            FrameworkElement source = null, object eventArgs = null, object[] parameters = null)
        {
            var context = new ActionExecutionContext
            {
                Target = target,
                Method = target.GetType().GetMethod(methodName),
#if WINFORMS || WEBGUI
                Message = new ActionMessage(source, null, methodName),
#else
                Message = new ActionMessage {
                    MethodName = methodName
                },
#endif
                View = view,
                Source = source,
                EventArgs = eventArgs
            };

            if (parameters != null)
            {
                parameters.Apply(x => context.Message.Parameters.Add(x as Parameter ?? new Parameter {Value = x}));
            }

            ActionMessage.InvokeAction(context);
        }
Exemplo n.º 13
0
 /// <summary>
 ///   Executes the result using the specified context.
 /// </summary>
 /// <param name = "context">The context.</param>
 public void Execute(ActionExecutionContext context)
 {
     this.context = context;
     ChildCompleted(null, new ResultCompletionEventArgs());
 }
Exemplo n.º 14
0
        /// <summary>
        /// Try to find a candidate for guard function, having:
        ///		- a name in the form "CanXXX"
        ///		- no generic parameters
        ///		- a bool return type
        ///		- no parameters or a set of parameters corresponding to the action method
        /// </summary>
        /// <param name="context">The execution context</param>
        /// <returns>A MethodInfo, if found; null otherwise</returns>
        private static MethodInfo TryFindGuardMethod(ActionExecutionContext context)
        {
            var guardName = "Can" + context.Method.Name;
            var targetType = context.Target.GetType();
            var guard = targetType.GetMethod(guardName);

            if (guard == null) return null;
            if (guard.ContainsGenericParameters) return null;
            if (typeof (bool) != guard.ReturnType) return null;

            var guardPars = guard.GetParameters();
            var actionPars = context.Method.GetParameters();
            if (guardPars.Length == 0) return guard;
            if (guardPars.Length != actionPars.Length) return null;

            var comparisons = guardPars.Zip(
                context.Method.GetParameters(),
                (x, y) => x.ParameterType == y.ParameterType
                );

            if (comparisons.Any(x => !x))
            {
                return null;
            }

            return guard;
        }
Exemplo n.º 15
0
        private void UpdateContext()
        {
            if (context != null)
                context.Dispose();

            context = new ActionExecutionContext
            {
                Message = this,
                Source = AssociatedObject
            };

            PrepareContext(context);
            UpdateAvailabilityCore();
        }
Exemplo n.º 16
0
        public void Execute(ActionExecutionContext context)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(state =>
            {
                System.Threading.Thread.Sleep(2000);

                Response = Guid.NewGuid();
                Completed(this, new ResultCompletionEventArgs());
            });
        }