Beispiel #1
0
        /// <summary>
        /// Discovers and invokes any Initialize methods according to the initializeMethodSelector passed to
        /// the constructor.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="request">The request.</param>
        protected virtual void CallInitializeMethods(object model, ResolvedNavigationRequest request)
        {
            if (model == null || initializeMethodSelector == null)
            {
                return;
            }

            var initializers = model.GetType().GetMethods() as IEnumerable <MethodInfo>;
            var initializer  = initializeMethodSelector(initializers);

            if (initializer == null)
            {
                return;
            }

            var arguments = new List <object>();

            foreach (var parameterInfo in initializer.GetParameters())
            {
                var bindingContext = new ModelBindingContext(parameterInfo.Name, initializer, parameterInfo.ParameterType, request.RouteValues);
                var binder         = modelBinders.GetBinder(parameterInfo.ParameterType);
                var argument       = binder.BindModel(request, bindingContext);
                arguments.Add(argument);
            }

            initializer.Invoke(model, arguments.ToArray());
        }
        public void ShouldResolveBindersViaInheritance()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
            dictionary.Add(typeof(IListSource), FakeBinder);

            var binder = dictionary.GetBinder(typeof(DataSet)); // DataSet implements IListSource

            Assert.AreSame(FakeBinder, binder);
        }
        public void ShouldResolveBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
            dictionary.Add(typeof(DataSet), FakeBinder);

            var binder = dictionary.GetBinder(typeof(DataSet));

            Assert.AreSame(FakeBinder, binder);
        }
        /// <summary>
        /// Executes the action on the controller using the parameters and model binders in the current request.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="modelBinders">The model binders.</param>
        /// <returns>
        /// The <see cref="ActionResult"/> returned by the controller action.
        /// </returns>
        public ActionResult Execute(ControllerContext controllerContext, ModelBinderDictionary modelBinders)
        {
            var arguments = new List<object>();
            foreach (var parameterInfo in method.GetParameters())
            {
                var bindingContext = new ModelBindingContext(parameterInfo.Name, Method, parameterInfo.ParameterType, controllerContext.Request.RouteValues);
                var binder = modelBinders.GetBinder(parameterInfo.ParameterType);
                var argument = binder.BindModel(controllerContext.Request, bindingContext);
                arguments.Add(argument);
            }

            try
            {
                var wrapper = DelegateInvoker.CreateInvoker(controller, method);
                return (ActionResult) wrapper.Call(arguments.ToArray());
            }
            catch (Exception ex)
            {
                TraceSources.MagellanSource.TraceError(ex, "The action '{0}' on controller '{1} threw an exception.", controllerContext.ActionName, controllerContext.ActionName);
                throw;
            }
        }
        /// <summary>
        /// Executes the action on the controller using the parameters and model binders in the current request.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="modelBinders">The model binders.</param>
        /// <returns>
        /// The <see cref="ActionResult"/> returned by the controller action.
        /// </returns>
        public ActionResult Execute(ControllerContext controllerContext, ModelBinderDictionary modelBinders)
        {
            var arguments = new List <object>();

            foreach (var parameterInfo in method.GetParameters())
            {
                var bindingContext = new ModelBindingContext(parameterInfo.Name, Method, parameterInfo.ParameterType, controllerContext.Request.RouteValues);
                var binder         = modelBinders.GetBinder(parameterInfo.ParameterType);
                var argument       = binder.BindModel(controllerContext.Request, bindingContext);
                arguments.Add(argument);
            }

            try
            {
                var wrapper = DelegateInvoker.CreateInvoker(controller, method);
                return((ActionResult)wrapper.Call(arguments.ToArray()));
            }
            catch (Exception ex)
            {
                TraceSources.MagellanSource.TraceError(ex, "The action '{0}' on controller '{1} threw an exception.", controllerContext.ActionName, controllerContext.ActionName);
                throw;
            }
        }
        public void ShouldRespectExplicitDefaultBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
            dictionary.DefaultModelBinder = FakeBinder;

            var binder = dictionary.GetBinder(typeof(int));

            Assert.AreSame(FakeBinder, binder);
        }
        public void ShouldFallBackToDefaultBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
            dictionary.Add(typeof(DataSet), FakeBinder);

            var binder = dictionary.GetBinder(typeof(int));

            Assert.IsInstanceOf<DefaultModelBinder>(binder);
        }