void Awake() { _context = GetComponent <DataBindContext>(); OnPropertyChanged(nameof(Name), _state.Name); OnPropertyChanged(nameof(Count), _state.Count); OnPropertyChanged(nameof(Active), _state.Active); }
private bool ShouldIgnoreProperty(PropertyInfo prop, DataBindContext ctx) { return(!prop.CanWrite || (ctx.AllowedProperties != null && Array.IndexOf(ctx.AllowedProperties, prop.Name) == -1) || (ctx.ExcludedProperties != null && Array.IndexOf(ctx.ExcludedProperties, prop.Name) != -1)); }
private void Awake() { m_Context = GetComponent <DataBindContext>(); m_NumberItems = new ObservableList("Items"); m_Context["Items"] = m_NumberItems; m_Context["Number"] = 0; }
public object BindObjectInstance(object instance, String paramPrefix, NameValueCollection paramList, IDictionary files, IList errorList) { paramPrefix = NormalizeParamPrefix(paramPrefix); String root = GetRoot(instance.GetType(), paramPrefix); DataBindContext ctx = new DataBindContext(root, paramList, files, errorList, null, null); return(InternalRecursiveBindObjectInstance(instance, paramPrefix, DefaultNestedLevelsLeft, ctx)); }
public object BindObject(Type instanceType, String paramPrefix, NameValueCollection paramList, IDictionary files, IList errorList, int nestedLevel, String excludedProperties, String allowProperties) { String root = GetRoot(instanceType, paramPrefix); paramPrefix = NormalizeParamPrefix(paramPrefix); String[] excludedPropertyList = CreateNormalizedList(excludedProperties); String[] allowPropertyList = CreateNormalizedList(allowProperties); DataBindContext ctx = new DataBindContext(root, paramList, files, errorList, excludedPropertyList, allowPropertyList); return(InternalBindObject(instanceType, paramPrefix, nestedLevel, ctx)); }
protected void SaveManyMappings(object instance, ActiveRecordModel model, DataBindContext context) { foreach (HasManyModel hasManyModel in model.HasMany) { if (hasManyModel.HasManyAtt.Inverse) { continue; } if (hasManyModel.HasManyAtt.RelationType != RelationType.Bag && hasManyModel.HasManyAtt.RelationType != RelationType.Set) { continue; } ActiveRecordModel otherModel = ActiveRecordModel.GetModel(hasManyModel.HasManyAtt.MapType); PrimaryKeyModel keyModel = ARCommonUtils.ObtainPKProperty(otherModel); if (otherModel == null || keyModel == null) { continue; // Impossible to save } CreateMappedInstances(instance, hasManyModel.Property, keyModel, otherModel, context); } foreach (HasAndBelongsToManyModel hasManyModel in model.HasAndBelongsToMany) { if (hasManyModel.HasManyAtt.Inverse) { continue; } if (hasManyModel.HasManyAtt.RelationType != RelationType.Bag && hasManyModel.HasManyAtt.RelationType != RelationType.Set) { continue; } ActiveRecordModel otherModel = ActiveRecordModel.GetModel(hasManyModel.HasManyAtt.MapType); PrimaryKeyModel keyModel = ARCommonUtils.ObtainPKProperty(otherModel); if (otherModel == null || keyModel == null) { continue; // Impossible to save } CreateMappedInstances(instance, hasManyModel.Property, keyModel, otherModel, context); } }
protected override void AfterBinding(object instance, String paramPrefix, DataBindContext context) { // Defensive programming if (instance == null) { return; } ActiveRecordModel model = ActiveRecordModel.GetModel(instance.GetType()); if (model == null) { return; } SaveManyMappings(instance, model, context); }
protected virtual void AfterBinding(object instance, String paramPrefix, DataBindContext context) { }
protected object InternalRecursiveBindObjectInstance(object instance, String paramPrefix, int nestedLevelsLeft, DataBindContext ctx) { if (--nestedLevelsLeft < 0) { return(instance); } if (ShouldIgnoreElement(ctx.ParamList, paramPrefix)) { return(instance); } BeforeBinding(instance, paramPrefix, ctx); PropertyInfo[] props = instance.GetType().GetProperties(PropertiesBindingFlags); foreach (PropertyInfo prop in props) { if (ShouldIgnoreProperty(prop, ctx)) { continue; } Type propType = prop.PropertyType; String paramName = BuildParamName(paramPrefix, prop.Name); try { if (!IsSimpleProperty(propType)) { if (nestedLevelsLeft > 0) { // if the property is an object, we look if it is already instanciated object value = prop.GetValue(instance, null); // if it's not there, we create it // Or if is an array if (value == null || propType.IsArray) { value = InternalBindObject(propType, paramName, nestedLevelsLeft, ctx); prop.SetValue(instance, value, null); } else // if the object already instanciated, then we use it { InternalRecursiveBindObjectInstance(value, paramName, nestedLevelsLeft, ctx); } } } else { bool conversionSucceeded; String[] values = ctx.ParamList.GetValues(paramName); object value = ConvertUtils.Convert(prop.PropertyType, values, paramName, ctx.Files, ctx.ParamList, out conversionSucceeded); // we don't want to set the value if the form param was missing // to avoid loosing existing values in the object instance if (conversionSucceeded && value != null) { prop.SetValue(instance, value, null); } } } catch (Exception ex) { if (ctx.Errors != null) { ctx.Errors.Add(new DataBindError(ctx.Root, (paramPrefix == "") ? paramName : prop.Name, ex)); } else { throw; } } } AfterBinding(instance, paramPrefix, ctx); return(instance); }
protected object InternalBindObject(Type instanceType, String paramPrefix, int nestedLevelsLeft, DataBindContext ctx) { if (ShouldIgnoreType(instanceType) || ShouldIgnoreElement(ctx.ParamList, paramPrefix)) { return(null); } if (instanceType.IsArray) { return(InternalBindObjectArray(instanceType, paramPrefix, nestedLevelsLeft, ctx)); } else { object instance = CreateInstance(instanceType, paramPrefix, ctx.ParamList); return(InternalRecursiveBindObjectInstance(instance, paramPrefix, nestedLevelsLeft, ctx)); } }
// Only here so we can support 2 types of loop in the BindObjectArrayInstance // without duplicating too much code private void AddArrayElement(ArrayList bindArray, Type instanceType, object arrayPrefix, String paramPrefix, int nestedLevelsLeft, DataBindContext ctx) { String arrayParamPrefix = paramPrefix + "[" + arrayPrefix + "]"; if (!ShouldIgnoreElement(ctx.ParamList, arrayParamPrefix)) { object instance = CreateInstance( instanceType.GetElementType(), arrayParamPrefix, ctx.ParamList); InternalRecursiveBindObjectInstance(instance, arrayParamPrefix, nestedLevelsLeft, ctx); bindArray.Add(instance); } }
private object[] InternalBindObjectArray(Type instanceType, String paramPrefix, int nestedLevelsLeft, DataBindContext ctx) { ArrayList bindArray = new ArrayList(); // check if a count attribute is present, if so we assume the // params are in order i.e. // param[0], param[1], ... param[count-1] // otherwise we have to find all uniques id for that identifier // which is probably slower but is more flexible String countBeforeCast = ctx.ParamList[paramPrefix + CountAttribute]; if (countBeforeCast != null) { Int32 count = System.Convert.ToInt32(countBeforeCast); // if count > paramList.Count that means that there is a problem // in the count variable if (count > ctx.ParamList.Count) { count = ctx.ParamList.Count; } for (int prefix = 0; prefix < count; prefix++) { AddArrayElement(bindArray, instanceType, prefix, paramPrefix, nestedLevelsLeft, ctx); } } else { String[] uniquePrefixes = Grep(ctx.ParamList.AllKeys, "^" + Regex.Escape(paramPrefix) + @"\[(.*?)]", 1); foreach (String prefix in uniquePrefixes) { AddArrayElement(bindArray, instanceType, prefix, paramPrefix, nestedLevelsLeft, ctx); } } return((object[])bindArray.ToArray(instanceType.GetElementType())); }
private void CreateMappedInstances(object instance, PropertyInfo prop, PrimaryKeyModel keyModel, ActiveRecordModel otherModel, DataBindContext context) { object container = InitializeRelationPropertyIfNull(instance, prop); // TODO: Support any kind of key String paramName = String.Format("{0}.{1}", prop.Name, keyModel.Property.Name); String[] values = context.ParamList.GetValues(paramName); int[] ids = (int[])ConvertUtils.Convert(typeof(int[]), values, paramName, null, context.ParamList); if (ids != null) { foreach (int id in ids) { object item = Activator.CreateInstance(otherModel.Type); keyModel.Property.SetValue(item, id, EmptyArg); AddToContainer(container, item); } } }
private void Awake() { _context = GetComponent <DataBindContext>(); _items = new ObservableList("Items"); _context["Items"] = _items; }