Exemplo n.º 1
0
        /// <summary>
        /// Get the unvalidated string of a model property posted in Request.
        /// </summary>
        /// <typeparam name="M">The type of the model</typeparam>
        /// <param name="propertySelector">An expression to select the property from the model.</param>
        /// <returns>Returns the string in the request for the property.</returns>
        protected string GetUnvalidatedPropertyFromRequest <M>(Expression <Func <M, object> > propertySelector)
        {
            if (propertySelector == null)
            {
                throw new ArgumentNullException(nameof(propertySelector));
            }

            string requestKey = GenericExpressionHelper.GetExpressionText(propertySelector);

            return(this.Request.Unvalidated[requestKey]);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempt to update the specified model instance using values from the controller's current value provider.
        /// </summary>
        /// <typeparam name="M">The type of the model.</typeparam>
        /// <typeparam name="VM">The type of object which contains the model.</typeparam>
        /// <param name="modelSelector">The expression which extracts the model from which container.</param>
        /// <param name="model">The model to update.</param>
        /// <returns>Returns true when the included model properties were valid.</returns>
        protected bool TryUpdateModel <M, VM>(M model, Expression <Func <VM, M> > modelSelector)
            where M : class
        {
            if (modelSelector == null)
            {
                throw new ArgumentNullException(nameof(modelSelector));
            }

            string modelPrefix = GenericExpressionHelper.GetExpressionText(modelSelector);

            return(TryUpdateModel(model, modelPrefix));
        }
Exemplo n.º 3
0
        private static string[] GetPropertyNames <M>(Expression <Func <M, object> >[] propertySelectors)
            where M : class
        {
            string[] propertyNames = new string[propertySelectors.Length];

            for (int i = 0; i < propertySelectors.Length; i++)
            {
                propertyNames[i] = GenericExpressionHelper.GetExpressionText(propertySelectors[i]);
            }

            return(propertyNames);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Updates the specified model instance using values from the controller's current value provider
        /// and included properties.
        /// </summary>
        /// <typeparam name="M">The type of the model.</typeparam>
        /// <typeparam name="VM">The type of object which contains the model.</typeparam>
        /// <param name="modelSelector">The expression to extract the model the view model of type <typeparamref name="VM"/>.</param>
        /// <param name="model">The model to update.</param>
        /// <param name="excludedPropertiesSelectors">Array of expressions of excluded properties.</param>
        /// <exception cref="InvalidOperationException">Thrown when the included properties are not valid.</exception>
        protected void UpdateModelExcluding <M, VM>(
            Expression <Func <VM, M> > modelSelector,
            M model,
            params Expression <Func <M, object> >[] excludedPropertiesSelectors)
            where M : class
        {
            if (modelSelector == null)
            {
                throw new ArgumentNullException(nameof(modelSelector));
            }

            string prefix = GenericExpressionHelper.GetExpressionText(modelSelector);

            UpdateModelExcluding(prefix, model, excludedPropertiesSelectors);
        }
        /// <summary>
        /// Using the controller's value provider, Bind a <see cref="IStatefulObjectExecutionModel"/> for executing a path on a stateful object.
        /// Any validation errors are capured in ModelState.
        /// </summary>
        /// <typeparam name="ST">The type of state transitions of the stateful object.</typeparam>
        /// <typeparam name="SO">The type of the stateful object.</typeparam>
        /// <typeparam name="WM">The type of the workflow manager for the stateful object.</typeparam>
        /// <typeparam name="VM">The type of the view model containing the <see cref="IStatefulObjectExecutionModel"/>.</typeparam>
        /// <param name="workflowManager">he workflow manager for the stateful object.</param>
        /// <param name="modelSelector">The expression to extract <see cref="IStatefulObjectExecutionModel"/> from the view model of type <typeparamref name="VM"/>.</param>
        /// <returns>Returns the model requested. Check ModelState for any validation errors.</returns>
        protected async Task <StatefulObjectExecutionModel <U, ST, SO> > BindStatefulObjectExecutionModelAsync <ST, SO, WM, VM>(
            WM workflowManager,
            Expression <Func <VM, IStatefulObjectExecutionModel> > modelSelector)
            where ST : StateTransition <U>
            where SO : IStateful <U, ST>
            where WM : IWorkflowManager <U, ST, SO>
        {
            if (modelSelector == null)
            {
                throw new ArgumentNullException(nameof(modelSelector));
            }

            string prefix = GenericExpressionHelper.GetExpressionText(modelSelector);

            return(await BindStatefulObjectExecutionModelAsync <ST, SO, WM>(workflowManager, prefix));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Attempt to get the value of a model property from a value provider,
        /// else return null if the value was not specified.
        /// </summary>
        /// <typeparam name="M">The type of the model being bound to the value provider.</typeparam>
        /// <param name="valueProvider">The Value provoder.</param>
        /// <param name="modelPropertyExpression">Expression for selecting the property from the bound model.</param>
        /// <returns>Returns the result found if the value exists, else null.</returns>
        public static ValueProviderResult GetValue <M>(
            this IValueProvider valueProvider,
            Expression <Func <M, object> > modelPropertyExpression)
        {
            if (valueProvider == null)
            {
                throw new ArgumentNullException(nameof(valueProvider));
            }
            if (modelPropertyExpression == null)
            {
                throw new ArgumentNullException(nameof(modelPropertyExpression));
            }

            string propertyKey = GenericExpressionHelper.GetExpressionText(modelPropertyExpression);

            return(valueProvider.GetValue(propertyKey));
        }
        /// <summary>
        /// Adds the specified exception to the errors collection for the model-state
        /// dictionary that is associated with the specified property.
        /// </summary>
        /// <typeparam name="M">The type of the model being bound.</typeparam>
        /// <param name="modelState">The model state.</param>
        /// <param name="propertySelector">Expression specifying the property having the error.</param>
        /// <param name="exception">The exception.</param>
        public static void AddModelError <M>(
            this ModelStateDictionary modelState,
            Expression <Func <M, object> > propertySelector,
            Exception exception)
        {
            if (modelState == null)
            {
                throw new ArgumentNullException(nameof(modelState));
            }
            if (propertySelector == null)
            {
                throw new ArgumentNullException(nameof(propertySelector));
            }
            if (exception == null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            string propertyKey = GenericExpressionHelper.GetExpressionText(propertySelector);

            modelState.AddModelError(propertyKey, exception);
        }