Esempio n. 1
0
        /// <summary>
        /// Parses model and validates it asynchronously
        /// </summary>
        /// <typeparam name="T">Model type</typeparam>
        /// <param name="resolver">The resolver.</param>
        /// <returns></returns>
        public async Task <T> ProcessAsync <T>(IDIResolver resolver)
        {
            var args = new ModelBinderEventArgs <T>(_context);

            foreach (var binder in ModelBindersTypes.Select(binderType => (IModelBinder)resolver.Resolve(binderType)))
            {
                await binder.BindAsync(args);

                if (!args.IsBound)
                {
                    continue;
                }

                Validate(args, resolver);

                return(args.Model);
            }

            throw new ModelBindingException($"Unrecognized request content type for binding: {_context.Request.ContentType}");
        }
Esempio n. 2
0
        /// <summary>
        /// Parses object from data and validates it
        /// </summary>
        /// <typeparam name="T">Model type</typeparam>
        /// <returns></returns>
        /// <exception cref="ModelBindingException"></exception>
        public T Process <T>()
        {
            var args = new ModelBinderEventArgs <T>(_context);

            foreach (var binder in ModelBindersTypes.Select(binderType => (IModelBinder)Activator.CreateInstance(binderType)))
            {
                binder.Bind(args);

                if (!args.IsBinded)
                {
                    continue;
                }

                foreach (var validator in ModelValidatorsTypes.Select(x => (IModelValidator)Activator.CreateInstance(x)))
                {
                    validator.Validate(args.Model);
                }

                return(args.Model);
            }

            throw new ModelBindingException(string.Format("Unrecognized request content type for binding: {0}", _context.Request.ContentType));
        }