public static T BindFromAll <T>(this NancyModule module)
        {
            if (module.Context.Request.Method == "GET")
            {
                module.Context.Request.Headers.ContentType = null;
            }

            var request = module.Bind <T>();

            var props = typeof(T).GetProperties();

            foreach (var prop in props)
            {
                var fieldName = Char.ToLowerInvariant(prop.Name[0]) + prop.Name.Substring(1);

                if (module.Request.Query[fieldName] != null)
                {
                    try
                    {
                        prop.SetValue(request, Convert.ChangeType(module.Request.Query[fieldName], prop.PropertyType));
                    }
                    catch (Exception) { }
                }
            }

            return(request);
        }
Esempio n. 2
0
        public static TModel CustomBindAndValidate <TModel>(this NancyModule module)
        {
            TModel model = module.Bind <TModel>();

            InternalValidate(module, model);
            return(model);
        }
Esempio n. 3
0
        /// <summary>
        /// Bind the incoming request to a model and validate
        /// </summary>
        /// <typeparam name="TModel">Model type</typeparam>
        /// <param name="module">Current module</param>
        /// <param name="configuration">The <see cref="BindingConfig"/> that should be applied during binding.</param>
        /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
        /// <returns>Bound model instance</returns>
        /// <remarks><see cref="ModelValidationResult"/> is stored in NancyModule.ModelValidationResult and NancyContext.ModelValidationResult.</remarks>
        public static TModel BindAndValidate <TModel>(this NancyModule module, BindingConfig configuration, params string[] blacklistedProperties)
        {
            var model = module.Bind <TModel>(configuration, blacklistedProperties);

            module.Validate(model);
            return(model);
        }
 public static TModel Bind <TModel>(this NancyModule module, object blacklistedProperties)
 {
     if (module == null)
     {
         throw new ArgumentNullException(nameof(module));
     }
     return(module.Bind <TModel>((BindingConfig)null, null));
 }
 public static TModel Bind <TModel>(this NancyModule module, params Expression <Func <TModel, object> >[] blacklistedProperties)
 {
     if (module == null)
     {
         throw new ArgumentNullException(nameof(module));
     }
     return(module.Bind((BindingConfig)null, blacklistedProperties));
 }
 public static TModel Bind <TModel>(this NancyModule module, BindingConfig configuration)
 {
     if (module == null)
     {
         throw new ArgumentNullException(nameof(module));
     }
     return(module.Bind <TModel>(configuration, null));
 }
        public static T BindAndValidate <T>(this NancyModule module) where T : ApiRequest
        {
            var request = module.Bind <T>();

            var requestValidation = module.Validate(request);

            if (requestValidation.IsValid == false)
            {
                throw new HypermediaEngineException(new BadRequestResponse(module.Context, requestValidation.Errors));
            }

            return(request);
        }
Esempio n. 8
0
        /// <summary>
        /// BindTo extension method to bind to same instance. Pull Request sent to NancyFx with this extension so it might make it into Nancy 0.12
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="module"></param>
        /// <param name="instance"></param>
        /// <param name="blacklistedProperties"></param>
        public static void BindTo <TModel>(this NancyModule module, TModel instance, params string[] blacklistedProperties)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("Bind instance is null");
            }

            TModel boundModel = module.Bind(blacklistedProperties);

            foreach (PropertyDescriptor item in TypeDescriptor.GetProperties(boundModel))
            {
                object value = item.GetValue(boundModel);
                if (value != null)
                {
                    item.SetValue(instance, value);
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Bind the incoming request to an existing instance
        /// </summary>
        /// <typeparam name="TModel">Model type</typeparam>
        /// <param name="module">Current module</param>
        /// <param name="instance">The class instance to bind properties to</param>
        /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
        public static void BindTo <TModel>(this NancyModule module, TModel instance, params string[] blacklistedProperties)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance", "The instance parameter cannot be null");
            }

            var boundModel = module.Bind(blacklistedProperties);

            foreach (var item in TypeDescriptor.GetProperties(boundModel))
            {
                var value = item.GetValue(boundModel);
                if (value != null)
                {
                    item.SetValue(instance, value);
                }
            }
        }
Esempio n. 10
0
        public static T BindAndValidateModel <T>(this NancyModule module, Action <T> hookToSetModel) where T : class
        {
            try {
                var input = module.Bind <T>();

                if (hookToSetModel != null)
                {
                    hookToSetModel(input);
                }

                module.ValidateModel(input);

                return(input);
            }
            catch (JsonReaderException ex) {
                var errors = new Dictionary <string, string[]> {
                    { ex.Path, new[] { ex.Message } }
                };
                throw new BadRequestException(errors);
            }
        }
Esempio n. 11
0
 /// <summary>
 /// Bind the incoming request to a model
 /// </summary>
 /// <typeparam name="TModel">Model type</typeparam>
 /// <param name="module">Current module</param>
 /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
 /// <returns>Bound model instance</returns>
 public static TModel Bind <TModel>(this NancyModule module, params string[] blacklistedProperties)
 {
     return(module.Bind(blacklistedProperties));
 }
Esempio n. 12
0
 /// <summary>
 /// Bind the incoming request to a model
 /// </summary>
 /// <typeparam name="TModel">Model type</typeparam>
 /// <param name="module">Current module</param>
 /// <param name="configuration">The <see cref="BindingConfig"/> that should be applied during binding.</param>
 /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
 /// <returns>Bound model instance</returns>
 public static TModel Bind <TModel>(this NancyModule module, BindingConfig configuration, params string[] blacklistedProperties)
 {
     return(module.Bind(configuration, blacklistedProperties));
 }
Esempio n. 13
0
 /// <summary>
 /// Bind the incoming request to a model
 /// </summary>
 /// <param name="module">Current module</param>
 /// <param name="blacklistedProperties">Property names to blacklist from binding</param>
 /// <returns>Model adapter - cast to a model type to bind it</returns>
 public static dynamic Bind(this NancyModule module, params string[] blacklistedProperties)
 {
     return(module.Bind(BindingConfig.Default, blacklistedProperties));
 }
Esempio n. 14
0
 public static Envelope <T> BindCommandEnvelope <T>(this NancyModule module, Guid commandId)
 {
     return(new Envelope <T>(commandId, module.Bind <T>()));
 }