Ejemplo n.º 1
0
        /// <summary>
        /// Gets the value of a <see cref="ModelProperty"/> for this instance.
        /// </summary>
        /// <param name="property">The <see cref="ModelProperty"/> to query.</param>
        /// <returns>The current value of the <see cref="ModelProperty"/> for this instance.</returns>
        public virtual object GetValue(ModelProperty property)
        {
            object value;

            if (!_values.TryGetValue(property.Key, out value))
            {
                value = property.DefaultValue;

                var uninitializedValue = value as ModelProperty.UnitializedValue;
                if (uninitializedValue != null)
                {
                    _values = _values.AddOrUpdate(property.Key, null); // set a temporary placeholder to prevent infinite recursion

                    value = uninitializedValue.GetValue(this);
                    SetValueCore(property, value);
                }
            }

            return(value);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Registers a callback to call when a specified property changes.
        /// </summary>
        /// <param name="property">The property to monitor.</param>
        /// <param name="handler">The method to call when the property changes.</param>
        public void AddPropertyChangedHandler(ModelProperty property, EventHandler <ModelPropertyChangedEventArgs> handler)
        {
            lock (_lockObject)
            {
                List <WeakAction <object, ModelPropertyChangedEventArgs> > handlers;
                if (!_propertyChangedHandlers.TryGetValue(property.Key, out handlers))
                {
                    handlers = new List <WeakAction <object, ModelPropertyChangedEventArgs> >();
                    _propertyChangedHandlers = _propertyChangedHandlers.AddOrUpdate(property.Key, handlers);
                }

                handlers.Add(new WeakAction <object, ModelPropertyChangedEventArgs>(handler.Method, handler.Target));
            }

            var uninitializedValue = property.DefaultValue as ModelProperty.UnitializedValue;

            if (uninitializedValue != null && !_values.ContainsKey(property.Key))
            {
                var value = uninitializedValue.GetValue(this);
                SetValueCore(property, value);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sets the value of a <see cref="ModelProperty"/> for this instance.
        /// </summary>
        /// <param name="property">The <see cref="ModelProperty"/> to update.</param>
        /// <param name="value">The new value for the <see cref="ModelProperty"/>.</param>
        public override sealed void SetValue(ModelProperty property, object value)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }
            if (property.DefaultValue is ModelProperty.UnitializedValue)
            {
                throw new ArgumentException("Cannot assign value to derived property: " + property.FullName);
            }

            object currentValue;

            lock (_lockObject)
            {
                object originalValue = GetOriginalValue(property);
                if (!_updatedValues.TryGetValue(property.Key, out currentValue))
                {
                    currentValue = originalValue;
                }

                if (Object.Equals(value, currentValue))
                {
                    return;
                }

                if (Object.Equals(value, originalValue))
                {
                    _updatedValues = _updatedValues.Remove(property.Key);
                }
                else
                {
                    _updatedValues = _updatedValues.AddOrUpdate(property.Key, value);
                }
            }

            OnModelPropertyChanged(new ModelPropertyChangedEventArgs(property, currentValue, value));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Binds a property on a model to the view model.
        /// </summary>
        /// <param name="viewModelProperty">View model property to bind.</param>
        /// <param name="binding">Information about how the view model property is bound.</param>
        public void SetBinding(ModelProperty viewModelProperty, ModelBinding binding)
        {
            ModelBinding oldBinding;

            if (_bindings.TryGetValue(viewModelProperty.Key, out oldBinding))
            {
                _bindings = _bindings.Remove(viewModelProperty.Key);
                if (!IsObserving(oldBinding.Source, oldBinding.SourceProperty))
                {
                    oldBinding.Source.RemovePropertyChangedHandler(oldBinding.SourceProperty, OnSourcePropertyChanged);
                }
            }
            else if (_selfBindings.TryGetValue(viewModelProperty.Key, out oldBinding))
            {
                _selfBindings = _selfBindings.Remove(viewModelProperty.Key);
            }

            if (binding != null)
            {
                if (ReferenceEquals(binding.Source, this))
                {
                    _selfBindings = _selfBindings.Add(viewModelProperty.Key, binding);
                    RefreshBinding(viewModelProperty.Key, binding);
                }
                else
                {
                    if (!IsObserving(binding.Source, binding.SourceProperty))
                    {
                        binding.Source.AddPropertyChangedHandler(binding.SourceProperty, OnSourcePropertyChanged);
                    }

                    _bindings = _bindings.Add(viewModelProperty.Key, binding);
                    RefreshBinding(viewModelProperty.Key, binding);
                }
            }
        }
Ejemplo n.º 5
0
        private bool UpsertRelatedTable(ModelBase model, IDatabase database, string primaryTable, string relatedTable, ITinyDictionary <ForeignKeyFieldMetadata, List <int> > tableForeignKeyMetadatas)
        {
            ModelProperty joinProperty;
            string        joinFieldName = GetJoin(database, primaryTable, relatedTable, out joinProperty);

            if (joinFieldName == null)
            {
                throw new InvalidOperationException("Cannot determine relationship between " + primaryTable + " and " + relatedTable);
            }

            List <int> tablePropertyKeys;

            if (tableForeignKeyMetadatas.TryGetValue(null, out tablePropertyKeys))
            {
                if (!UpdateRow(model, database, relatedTable, tablePropertyKeys, joinProperty, joinFieldName) &&
                    !CreateRow(model, database, relatedTable, tablePropertyKeys, joinProperty, joinFieldName))
                {
                    return(false);
                }
            }

            // TODO: support updating related data on arbitrary foreign key join

            return(true);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Adds a field to the object.
        /// </summary>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="type">The type of the field.</param>
        /// <param name="value">The value of the field.</param>
        public void AddField(string fieldName, JsonFieldType type, object value)
        {
            var field = new JsonField(fieldName, type, value);

            _fields = _fields.AddOrUpdate(fieldName.ToLower(), field);
        }
Ejemplo n.º 7
0
 public TinyDictionaryDebugView(ITinyDictionary <TKey, TValue> dictionary)
 {
     _dictionary = dictionary;
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
 /// </summary>
 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
 public void Clear()
 {
     _data = EmptyTinyDictionary <TKey, TValue> .Instance;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Constructs a new TinyDictionary.
 /// </summary>
 public TinyDictionary()
 {
     _data = EmptyTinyDictionary <TKey, TValue> .Instance;
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Removes all items from the collection.
 /// </summary>
 public void Clear()
 {
     _items = EmptyTinyDictionary <T, int> .Instance;
 }