/// <summary>
 /// Initializes a new instance of the <see cref="DefaultViewInitializer"/> class.
 /// </summary>
 /// <param name="initializeMethodSelector">A filter used to decide whether a method can be used to
 /// select the Initialize method on a ViewModel.</param>
 /// <param name="modelBinders">The model binders.</param>
 public DefaultViewInitializer(Func<IEnumerable<MethodInfo>, MethodInfo> initializeMethodSelector, ModelBinderDictionary modelBinders)
 {
     Guard.ArgumentNotNull(initializeMethodSelector, "initializeMethodSelector");
     
     this.initializeMethodSelector = initializeMethodSelector;
     this.modelBinders = modelBinders ?? new ModelBinderDictionary(new DefaultModelBinder());
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ControllerRouteCatalog"/> class.
 /// </summary>
 /// <param name="controllerFactory">The controller factory.</param>
 /// <param name="viewActivator">The view activator that is used to instantiate views.</param>
 public ControllerRouteCatalog(IControllerFactory controllerFactory, IViewActivator viewActivator)
 {
     Guard.ArgumentNotNull(controllerFactory, "controllerFactory");
     this.controllerFactory = controllerFactory;
     viewEngines = Framework.ViewEngines.CreateDefaults(viewActivator);
     modelBinders = Framework.ModelBinders.CreateDefaults();
     handler = new ControllerRouteHandler(this.controllerFactory, viewEngines, modelBinders);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ControllerContext"/> class.
 /// </summary>
 /// <param name="controller">The controller.</param>
 /// <param name="request">The request.</param>
 /// <param name="viewEngines">The view engines.</param>
 /// <param name="modelBinders"></param>
 /// <param name="releaseCallback">A callback that is invoked when the current request needs to be 
 /// cleaned up.</param>
 public ControllerContext(IController controller, ResolvedNavigationRequest request, ViewEngineCollection viewEngines, ModelBinderDictionary modelBinders, Action releaseCallback)
 {
     this.controller = controller;
     this.request = request;
     this.viewEngines = viewEngines;
     this.modelBinders = modelBinders;
     this.releaseCallback = releaseCallback;
 }
 public void ClearCanBeUsedToRemoveAllRegistrations()
 {
     var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
     dictionary.Add(typeof(DataSet), FakeBinder);
     Assert.IsTrue(dictionary.Contains(FakeBinder));
     dictionary.Clear();
     Assert.IsFalse(dictionary.Contains(FakeBinder));
 }
        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 RemoveCanBeUsedToUnregister()
 {
     var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
     dictionary.Add(typeof(DataSet), FakeBinder);
     dictionary.Remove(FakeBinder);
     dictionary.Add(typeof(DataSet), FakeBinder);
     dictionary.Remove(FakeBinder);
     dictionary.Add(typeof(DataSet), FakeBinder);
 }
        public void ShouldResolveBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
            dictionary.Add(typeof(DataSet), FakeBinder);

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

            Assert.AreSame(FakeBinder, binder);
        }
        public void ContainsCanBeUsedToCheckForPriorRegistration()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
            Assert.IsFalse(dictionary.Contains(FakeBinder));

            dictionary.Add(typeof(DataSet), FakeBinder);
            Assert.IsTrue(dictionary.Contains(FakeBinder));

            dictionary.Remove(FakeBinder);
            Assert.IsFalse(dictionary.Contains(FakeBinder));
        }
Exemplo n.º 9
0
 /// <summary>
 /// Executes the action on the specified controller.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="actionName">Name of the action.</param>
 /// <param name="modelBinders">The model binders.</param>
 public override void ExecuteAction(ControllerContext controllerContext, string actionName, ModelBinderDictionary modelBinders)
 {
     var dispatcher = controllerContext.Request.Navigator.Dispatcher;
     ThreadPool.QueueUserWorkItem(
         delegate
             {
                 Thread.CurrentThread.Name = string.Format("Navigation request: {0}", controllerContext.Request);
                 try
                 {
                     ExecuteBase(controllerContext, actionName, modelBinders);
                 }
                 catch (Exception ex)
                 {
                     var rethrower = new Rethrower(
                         string.Format("An exception occurred when attempting to asynchronously execute the request '{0}'. {1}", controllerContext.Request, ex.Message), 
                         ex);
                     dispatcher.Dispatch(rethrower.RethrowOnDispatchThread);
                 }
             });
 }
        /// <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;
            }
        }
Exemplo n.º 12
0
        private ActionResult ExecuteAction(ControllerContext controllerContext, ModelBinderDictionary modelBinders, string actionName, IActionDescriptor actionDescriptor)
        {
            var filters = FindActionFilters(actionDescriptor, controllerContext, actionName);
            var result = ExecutePreActionFilters(controllerContext, modelBinders, filters);

            // If one of the filters gave us a result, we can short-circuit calling the action and skip ahead
            if (result != null)
                return result;

            controllerContext.Request.ReportProgress(new ExecutingActionNavigationEvent());

            try
            {
                // Invoke the action on the controller
                result = actionDescriptor.Execute(controllerContext, modelBinders);
            }
            catch (Exception ex)
            {
                // Give post-filters a chance to suppress or handle the exception
                var errorFilterResult = ExecutePostActionFilters(controllerContext, result, ex, filters);
                
                if (errorFilterResult.ExceptionHandled || errorFilterResult.Exception == null)
                    return errorFilterResult.Result;

                if (ex == errorFilterResult.Exception)
                    throw;

                throw errorFilterResult.Exception;
            }

            // Give post-filters a chance rewrite the result
            var filterResult = ExecutePostActionFilters(controllerContext, result, null, filters);
            if (filterResult.ExceptionHandled || filterResult.Exception == null)
                return filterResult.Result;

            throw filterResult.Exception;
        }
Exemplo n.º 13
0
        /// <summary>
        /// Executes the action on the specified controller.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="actionName">Name of the action.</param>
        /// <param name="modelBinders">The model binders.</param>
        public virtual void ExecuteAction(ControllerContext controllerContext, string actionName, ModelBinderDictionary modelBinders)
        {
            try
            {
                Guard.ArgumentNotNull(controllerContext, "controllerContext");
                Guard.ArgumentNotNull(modelBinders, "modelBinders");
                Guard.ArgumentNotNullOrEmpty(actionName, "actionName");

                controllerContext.Request.ReportProgress(new ResolvingActionNavigationEvent());
                var actionDescriptor = FindAction(controllerContext, actionName);
                if (actionDescriptor == null)
                {
                    throw new ActionNotFoundException(string.Format("An action by the name '{0}' could not be found on the controller '{1}'. In general, actions should be public instance methods, and should return ActionResult or a derived type. Please ensure the action exists, has a valid signature and the name is spelled correctly.", actionName, controllerContext.Controller.GetType()));
                }

                var actionResult = ExecuteAction(controllerContext, modelBinders, actionName, actionDescriptor);
                ExecuteResult(actionDescriptor, controllerContext, actionName, actionResult);
            }
            finally
            {
                controllerContext.Dispose();
                TraceSources.MagellanSource.TraceInformation("Request completed: '{0}'.", controllerContext.Request);
            }
        }
 public void MultipleBindersCannotBeRegisteredForSameType()
 {
     var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
     dictionary.Add(typeof(DataSet), FakeBinder);
     Assert.Throws<ArgumentException>(() => dictionary.Add(typeof(DataSet), FakeBinder));
 }
 public void ShouldAllowModelBindersToBeRegistered()
 {
     var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
     dictionary.Add(typeof(DataSet), FakeBinder);
 }
Exemplo n.º 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ActionExecutingContext"/> class.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="modelBinders">The model binders.</param>
 public ActionExecutingContext(ControllerContext controllerContext, ModelBinderDictionary modelBinders)
 {
     this.controllerContext = controllerContext;
     this.modelBinders      = modelBinders;
 }
Exemplo n.º 17
0
        /// <summary>
        /// Invokes all pre-request filters, before the controller has been invoked.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="modelBinders">The model binders.</param>
        /// <param name="filters">The filters.</param>
        /// <returns></returns>
        protected virtual ActionResult ExecutePreActionFilters(ControllerContext controllerContext, ModelBinderDictionary modelBinders, IEnumerable<IActionFilter> filters)
        {
            controllerContext.Request.ReportProgress(new PreActionFiltersNavigationEvent());

            var context = new ActionExecutingContext(controllerContext, modelBinders);
            foreach (var filter in filters)
            {
                filter.OnActionExecuting(context);
                if (context.OverrideResult != null)
                {
                    return context.OverrideResult;
                }
            }
            return null;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ActionExecutingContext"/> class.
 /// </summary>
 /// <param name="controllerContext">The controller context.</param>
 /// <param name="modelBinders">The model binders.</param>
 public ActionExecutingContext(ControllerContext controllerContext, ModelBinderDictionary modelBinders)
 {
     this.controllerContext = controllerContext;
     this.modelBinders = modelBinders;
 }
        /// <summary>
        /// Executes the action on the specified controller.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="actionName">Name of the action.</param>
        /// <param name="modelBinders">The model binders.</param>
        public override void ExecuteAction(ControllerContext controllerContext, string actionName, ModelBinderDictionary modelBinders)
        {
            var dispatcher = controllerContext.Request.Navigator.Dispatcher;

            ThreadPool.QueueUserWorkItem(
                delegate
            {
                Thread.CurrentThread.Name = string.Format("Navigation request: {0}", controllerContext.Request);
                try
                {
                    ExecuteBase(controllerContext, actionName, modelBinders);
                }
                catch (Exception ex)
                {
                    var rethrower = new Rethrower(
                        string.Format("An exception occurred when attempting to asynchronously execute the request '{0}'. {1}", controllerContext.Request, ex.Message),
                        ex);
                    dispatcher.Dispatch(rethrower.RethrowOnDispatchThread);
                }
            });
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ControllerRouteHandler"/> class.
 /// </summary>
 /// <param name="controllerFactory">The controller factory.</param>
 /// <param name="viewEngines">The view engines.</param>
 /// <param name="modelBinders">The model binders.</param>
 public ControllerRouteHandler(IControllerFactory controllerFactory, ViewEngineCollection viewEngines, ModelBinderDictionary modelBinders)
 {
     this.controllerFactory = controllerFactory;
     this.modelBinders = modelBinders;
     this.viewEngines = viewEngines ?? ViewEngines.CreateDefaults();
 }
Exemplo n.º 21
0
 private void ExecuteBase(ControllerContext controllerContext, string actionName, ModelBinderDictionary modelBinders)
 {
     base.ExecuteAction(controllerContext, actionName, modelBinders);
 }
Exemplo n.º 22
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultViewInitializer"/> class.
        /// </summary>
        /// <param name="initializeMethodSelector">A filter used to decide whether a method can be used to
        /// select the Initialize method on a ViewModel.</param>
        /// <param name="modelBinders">The model binders.</param>
        public DefaultViewInitializer(Func <IEnumerable <MethodInfo>, MethodInfo> initializeMethodSelector, ModelBinderDictionary modelBinders)
        {
            Guard.ArgumentNotNull(initializeMethodSelector, "initializeMethodSelector");

            this.initializeMethodSelector = initializeMethodSelector;
            this.modelBinders             = modelBinders ?? new ModelBinderDictionary(new DefaultModelBinder());
        }
Exemplo n.º 23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultViewInitializer"/> class.
 /// </summary>
 /// <param name="modelBinders">The model binders.</param>
 public DefaultViewInitializer(ModelBinderDictionary modelBinders)
     : this(DefaultMethodSelector, modelBinders)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultViewInitializer"/> class.
 /// </summary>
 /// <param name="modelBinders">The model binders.</param>
 public DefaultViewInitializer(ModelBinderDictionary modelBinders)
     : this(DefaultMethodSelector, modelBinders)
 {
 }
Exemplo n.º 25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ControllerContext"/> class.
 /// </summary>
 /// <param name="controller">The controller.</param>
 /// <param name="request">The request.</param>
 /// <param name="viewEngines">The view engines.</param>
 /// <param name="modelBinders"></param>
 /// <param name="releaseCallback">A callback that is invoked when the current request needs to be
 /// cleaned up.</param>
 public ControllerContext(IController controller, ResolvedNavigationRequest request, ViewEngineCollection viewEngines, ModelBinderDictionary modelBinders, Action releaseCallback)
 {
     this.controller      = controller;
     this.request         = request;
     this.viewEngines     = viewEngines;
     this.modelBinders    = modelBinders;
     this.releaseCallback = releaseCallback;
 }
Exemplo n.º 26
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ViewModelRouteCatalog"/> class.
 /// </summary>
 /// <param name="factory">The factory.</param>
 public ViewModelRouteCatalog(IViewModelFactory factory)
 {
     Guard.ArgumentNotNull(factory, "factory");
     this.factory = factory;
     modelBinders = new ModelBinderDictionary(new DefaultModelBinder());
 }
        public void ShouldRespectExplicitDefaultBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
            dictionary.DefaultModelBinder = FakeBinder;

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

            Assert.AreSame(FakeBinder, binder);
        }
Exemplo n.º 28
0
        /// <summary>
        /// Executes the action on the specified controller.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="actionName">Name of the action.</param>
        /// <param name="modelBinders">The model binders.</param>
        public virtual void ExecuteAction(ControllerContext controllerContext, string actionName, ModelBinderDictionary modelBinders)
        {
            try
            {
                Guard.ArgumentNotNull(controllerContext, "controllerContext");
                Guard.ArgumentNotNull(modelBinders, "modelBinders");
                Guard.ArgumentNotNullOrEmpty(actionName, "actionName");

                controllerContext.Request.ReportProgress(new ResolvingActionNavigationEvent());
                var actionDescriptor = FindAction(controllerContext, actionName);
                if (actionDescriptor == null)
                {
                    throw new ActionNotFoundException(string.Format("An action by the name '{0}' could not be found on the controller '{1}'. In general, actions should be public instance methods, and should return ActionResult or a derived type. Please ensure the action exists, has a valid signature and the name is spelled correctly.", actionName, controllerContext.Controller.GetType()));
                }

                var actionResult = ExecuteAction(controllerContext, modelBinders, actionName, actionDescriptor);
                ExecuteResult(actionDescriptor, controllerContext, actionName, actionResult);
            }
            finally
            {
                controllerContext.Dispose();
                TraceSources.MagellanSource.TraceInformation("Request completed: '{0}'.", controllerContext.Request);
            }
        }
        public void ShouldFallBackToDefaultBinder()
        {
            var dictionary = new ModelBinderDictionary(new DefaultModelBinder());
            dictionary.Add(typeof(DataSet), FakeBinder);

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

            Assert.IsInstanceOf<DefaultModelBinder>(binder);
        }
 private void ExecuteBase(ControllerContext controllerContext, string actionName, ModelBinderDictionary modelBinders)
 {
     base.ExecuteAction(controllerContext, actionName, modelBinders);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ControllerRouteHandler"/> class.
 /// </summary>
 /// <param name="controllerFactory">The controller factory.</param>
 /// <param name="viewEngines">The view engines.</param>
 /// <param name="modelBinders">The model binders.</param>
 public ControllerRouteHandler(IControllerFactory controllerFactory, ViewEngineCollection viewEngines, ModelBinderDictionary modelBinders)
 {
     this.controllerFactory = controllerFactory;
     this.modelBinders      = modelBinders;
     this.viewEngines       = viewEngines ?? ViewEngines.CreateDefaults();
 }
Exemplo n.º 32
0
        /// <summary>
        /// Invokes all pre-request filters, before the controller has been invoked.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="modelBinders">The model binders.</param>
        /// <param name="filters">The filters.</param>
        /// <returns></returns>
        protected virtual ActionResult ExecutePreActionFilters(ControllerContext controllerContext, ModelBinderDictionary modelBinders, IEnumerable <IActionFilter> filters)
        {
            controllerContext.Request.ReportProgress(new PreActionFiltersNavigationEvent());

            var context = new ActionExecutingContext(controllerContext, modelBinders);

            foreach (var filter in filters)
            {
                filter.OnActionExecuting(context);
                if (context.OverrideResult != null)
                {
                    return(context.OverrideResult);
                }
            }
            return(null);
        }