void BindField(string key, IModelValueProvider provider, object model, PropertyInfo pi) { // Get converted value var mv = GetModelValue(key, provider, pi); if (mv == null) return; // Add to our collection of model values ModelState.Values.Add(mv.Name, mv); // Copy over errors too foreach (var e in mv.Errors) { ModelState.Errors.Add(e); } // Apply the property value if (mv.IsValid) { // Save the original value mv.OriginalValue = pi.GetValue(model, null); // Apply the new value pi.SetValue(model, mv.ConvertedValue, null); } }
public bool TryUpdateModel(IModelValueProvider provider, object model) { // Build the list of fields to be updated string[] field_list = BuildFieldList(provider, model); if (field_list == null) { throw new InvalidOperationException("Can't bind model as value provider can't provide a valid list of fields and neither an include nor exclude list was provided"); } // Update all property values foreach (var f in field_list) { // Find the target property object target; PropertyInfo pi; if (!ResolveField(model, f, out target, out pi)) { throw new InvalidOperationException(string.Format("Can't bind field `{0}` to model of type `{1}`", f, model.GetType().FullName)); } // Apply it's value BindField(f, provider, target, pi); } return ModelState.IsValid; }
public void UpdateModel(IModelDefinition definition, IModelValueProvider model, IModelValueGetter newState) { CopyModelValueProvider copy = new CopyModelValueProvider(definition, true); copy.Update(model, newState); Has = true; Added?.Invoke(); }
private object GetLocalModelValue(IDictionary<string, object> documentHost, Statement statement, IModelValueProvider modelValueProvider, object model) { object value; if (statement.Parameters.Count > 0) { if (statement.Parameters.Count == 1) { //check here first if (modelValueProvider.GetValue(documentHost, model, statement.Parameters[0].Value, out value)) { return value; } } List<object> parameters = new List<object>(); foreach (var parameter in statement.Parameters) { if (modelValueProvider.GetValue(documentHost, model, parameter.Value, out value)) { parameters.Add(value); } } return parameters; } if (model != null) { //check DocumentHost next //if (modelValueProvider.GetValue(documentHost, model, null, out value)) if (modelValueProvider.GetValue(documentHost, model, Parrot.Infrastructure.ValueType.Property, null, out value)) { return value; } } return model; }
/// <summary> /// Creates a new instance for <paramref name="modelDefinition"/> with custom innner value storage <paramref name="valueProvider"/>. /// </summary> /// <param name="modelDefinition">A definition of model.</param> /// <param name="valueProvider">A custom inner value storage.</param> public ObservableModel(IModelDefinition modelDefinition, IModelValueProvider valueProvider) : base(valueProvider) { Ensure.NotNull(modelDefinition, "modelDefinition"); ModelDefinition = modelDefinition; }
/// <summary> /// Creates new instance that wraps <paramref name="innerSetter"/> and raises <see cref="INotifyPropertyChanged.PropertyChanged"/>. /// </summary> /// <param name="innerSetter">Inner provider.</param> public ObservableModelValueProvider(IModelValueProvider innerProvider) : base(innerProvider) { Ensure.NotNull(innerProvider, "innerProvider"); this.innerProvider = innerProvider; }
public void UpdateModel(IModelDefinition definition, IModelValueProvider model, IModelValueGetter newState) { }
private object GetLocalModelValue(IDictionary <string, object> documentHost, Statement statement, IModelValueProvider modelValueProvider, object model) { object value; if (statement.Parameters.Count > 0) { if (statement.Parameters.Count == 1) { //check here first if (modelValueProvider.GetValue(documentHost, model, statement.Parameters[0].Value, out value)) { return(value); } } List <object> parameters = new List <object>(); foreach (var parameter in statement.Parameters) { if (modelValueProvider.GetValue(documentHost, model, parameter.Value, out value)) { parameters.Add(value); } } return(parameters); } if (model != null) { //check DocumentHost next //if (modelValueProvider.GetValue(documentHost, model, null, out value)) if (modelValueProvider.GetValue(documentHost, model, Parrot.Infrastructure.ValueType.Property, null, out value)) { return(value); } } return(model); }
ModelValue GetModelValue(string key, IModelValueProvider provider, PropertyInfo pi) { // Get the model value var string_value = provider.GetValue(key); if (string_value == null) return null; // Create the model value entry var mv = new ModelValue(); mv.Name = key; mv.InputValue = string_value; // Convert to required type if (pi.PropertyType != typeof(string)) { try { mv.ConvertedValue = Convert.ChangeType(string_value, pi.PropertyType); } catch (Exception x) { mv.Errors.Add(new ModelStateError(pi.Name, string.Format("Can't convert '{0}' to {1} - {2}", string_value, pi.PropertyType.FullName, x.Message))); } } else { mv.ConvertedValue = string_value; } return mv; }
string[] BuildFieldList(IModelValueProvider provider, object model) { string[] fields = null; // If an include list was passed, try to get it from the provider // The HttpModelValueProvider uses the hidden field generated by the form // to build a list of valid fields for binding if (IncludeFields == null) { fields = provider.GetFieldList(model); } // If an exclude list was specified, apply it now if (ExcludeFields != null) { // If we don't have an include list, start with the full list of properties on the model if (fields == null) fields = (from pi in model.GetType().GetProperties() select pi.Name).ToArray(); // Exclude specified fields fields = (from f in fields where !ExcludeFields.Contains(f) select f).ToArray(); } // Return the final field list return fields; }