private void RegisterFieldMetadata(ModelProperty property, FieldMetadata metadata, ForeignKeyFieldMetadata viaForeignKey, IConverter converter) { base.RegisterFieldMetadata(property, metadata, converter); var tableName = GetTableName(metadata.FieldName); ITinyDictionary <ForeignKeyFieldMetadata, List <int> > tableMetadata; if (!_tableMetadata.TryGetValue(tableName, out tableMetadata)) { tableMetadata = EmptyTinyDictionary <ForeignKeyFieldMetadata, List <int> > .Instance; _tableMetadata = _tableMetadata.AddOrUpdate(tableName, tableMetadata); } List <int> tableProperties; if (!tableMetadata.TryGetValue(viaForeignKey, out tableProperties)) { tableProperties = new List <int>(); tableMetadata = tableMetadata.AddOrUpdate(viaForeignKey, tableProperties); _tableMetadata.AddOrUpdate(tableName, tableMetadata); } tableProperties.Add(property.Key); _queryString = null; }
/// <summary> /// Gets or sets the element with the specified key. /// </summary> /// <returns> /// The element with the specified key. /// </returns> /// <param name="key">The key of the element to get or set.</param> /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception> /// <exception cref="T:System.Collections.Generic.KeyNotFoundException">The property is retrieved and <paramref name="key"/> is not found.</exception> public TValue this[TKey key] { get { if (key == null) { throw new ArgumentNullException("key"); } TValue value; if (!_data.TryGetValue(key, out value)) { throw new KeyNotFoundException(); } return(value); } set { if (key == null) { throw new ArgumentNullException("key"); } _data = _data.AddOrUpdate(key, value); } }
/// <summary> /// Registers metadata for a model type. /// </summary> /// <param name="type">Type of model to register metadata for.</param> /// <param name="metadataType">Type of metadata to register.</param> public void RegisterModelMetadata(Type type, Type metadataType) { if (!typeof(ModelMetadata).IsAssignableFrom(metadataType)) { throw new InvalidOperationException(metadataType.Name + " does not inherit from ModelMetadata"); } _mapping = _mapping.AddOrUpdate(type, metadataType); _modelNames = _modelNames.AddOrUpdate(type.Name.ToLower(), type); }
/// <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); }
public void Subscribe <T>(Action <T> handler) { List <WeakReference> delegates; if (!_events.TryGetValue(typeof(T), out delegates)) { delegates = new List <WeakReference>(); _events = _events.AddOrUpdate(typeof(T), delegates); } delegates.Add(new WeakAction <T>(handler)); }
/// <summary> /// Registers metadata for a <see cref="ModelProperty"/>. /// </summary> /// <param name="property">Property to register metadata for.</param> /// <param name="metadata">Metadata for the field.</param> /// <param name="converter">Converter to use when transfering data from the source field to the model property.</param> protected virtual void RegisterFieldMetadata(ModelProperty property, FieldMetadata metadata, IConverter converter) { _fieldMetadata = _fieldMetadata.AddOrUpdate(property.Key, metadata); if (converter != null) { _fieldConverters = _fieldConverters.AddOrUpdate(property.Key, converter); } if ((metadata.Attributes & InternalFieldAttributes.PrimaryKey) != 0 && PrimaryKeyProperty == null) { PrimaryKeyProperty = property; } }
/// <summary> /// Gets the metadata for the provided model type. /// </summary> /// <param name="type">Type of model to get metadata for.</param> /// <returns>Requested metadata, <c>null</c> if not found.</returns> public ModelMetadata GetModelMetadata(Type type) { ModelMetadata metadata; if (!_repository.TryGetValue(type, out metadata)) { Type metadataType; if (_mapping.TryGetValue(type, out metadataType)) { metadata = (ModelMetadata)Activator.CreateInstance(metadataType); _mapping = _mapping.Remove(type); _repository = _repository.AddOrUpdate(type, metadata); } } return(metadata); }
/// <summary> /// Adds the specified item to the collection. /// </summary> /// <param name="item">The item to add.</param> /// <param name="weight">The weight of the item. Compared to the weights of other items in the collection, determines the liklyhood of this item being selected.</param> public void Add(T item, int weight) { int oldWeight; if (_items.TryGetValue(item, out oldWeight)) { _totalWeight -= oldWeight; _items = _items.AddOrUpdate(item, weight); } else { _items = _items.Add(item, weight); } _totalWeight += weight; }
protected void SetError(string propertyName, string errorMessage) { if (!String.IsNullOrEmpty(errorMessage)) { _errors = _errors.AddOrUpdate(propertyName, errorMessage); IsValid = false; } else if (_errors.Count > 0) { _errors = _errors.Remove(propertyName); if (_errors.Count == 0) { IsValid = true; } } }
/// <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); } }
/// <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)); }
/// <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); }