Binding a Nancy Model to a Child Class

We have a problem with binding the default model to Nancy. Given below ...

public class Foo { public Foo() { } public string Name { get; set; } public Bar Bar { get; set; } } public class Bar { public string Name { get; set; } } 

with elements like ...

 <input type="text" value="Name" /> <input type="text" value="Bar.Name" /> 

When using a binder, the default is used as follows.

var foo = this.Bind<Foo>();

This binds Foo.Name correctly, but does not bind Foo.Bar.Name

Is there a way to enable this type of binding with a default binder or do we need a roll? If so, are there good examples?

+7
source share
1 answer

Why not try it. I am sure that recursion can be optimized, and something will appear where it does not work, and that it could be removed somewhere than IModelBinder, but it basically does what you want.

 /// <summary> /// Sample model binder that manually binds customer models /// </summary> public class CustomModelBinder : IModelBinder { /// <summary> /// Bind to the given model type /// </summary> /// <param name="context">Current context</param> /// <param name="modelType">Model type to bind to</param> /// <param name="blackList">Blacklisted property names</param> /// <returns>Bound model</returns> public object Bind(NancyContext context, Type modelType, params string[] blackList) { var parentObject = Activator.CreateInstance(modelType); foreach (var key in context.Request.Form) { var value = context.Request.Form[key]; this.SetObjectValue(parentObject, modelType, key, value); } return parentObject; } /// <summary> /// Sets the object value. /// </summary> /// <param name="instance">The instance.</param> /// <param name="type">The type.</param> /// <param name="key">Name of the property.</param> /// <param name="propertyValue">The property value.</param> private void SetObjectValue(object instance, Type type, string key, string propertyValue) { if (key.Contains(".")) { this.SetObjectValueDeep(instance, type, key, propertyValue); } PropertyInfo propertyInfo = type.GetProperty(key); if (propertyInfo == null) { return; } propertyInfo.SetValue(instance, Convert.ChangeType(propertyValue, propertyInfo.PropertyType), null); } /// <summary> /// Sets the object value derp. /// </summary> /// <param name="instance">The instance.</param> /// <param name="type">The type.</param> /// <param name="key">The key.</param> /// <param name="propertyValue">The property value.</param> private void SetObjectValueDeep(object instance, Type type, string key, string propertyValue) { var propList = key.Split('.').ToList(); PropertyInfo propertyInfo = type.GetProperty(propList.First()); if (propertyInfo == null) { return; } var childObject = propertyInfo.GetValue(instance, null); if (childObject == null) { childObject = Activator.CreateInstance(propertyInfo.PropertyType); propertyInfo.SetValue(instance, childObject, null); } propList.RemoveAt(0); var newKey = propList.Aggregate(string.Empty, (current, prop) => current + (prop + ".")).TrimEnd('.'); if (newKey.Contains(".")) { this.SetObjectValueDeep(childObject, childObject.GetType(), newKey, propertyValue); } else { this.SetObjectValue(childObject, childObject.GetType(), newKey, propertyValue); } } /// <summary> /// Determines whether this instance can bind the specified model type. /// </summary> /// <param name="modelType">Type of the model.</param> /// <returns> /// <c>true</c> if this instance can bind the specified model type; otherwise, <c>false</c>. /// </returns> public bool CanBind(Type modelType) { return true; } } 
+11
source

All Articles