Пример #1
0
        /// <summary>
        /// Commits a single model.
        /// </summary>
        protected override bool Commit(DataModelBase dataModel, ModelMetadata metadata)
        {
            if (metadata.GetKey(dataModel) < 0)
            {
                dataModel.SetValue(metadata.PrimaryKeyProperty, _nextKey++);
            }

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Initializes a new record.
        /// </summary>
        /// <param name="model">The newly created model object.</param>
        /// <param name="metadata">Metadata about the model.</param>
        protected override void InitializeNewRecord(DataModelBase model, ModelMetadata metadata)
        {
            base.InitializeNewRecord(model, metadata);

            var databaseMetadata = metadata as IDatabaseModelMetadata;

            if (databaseMetadata != null)
            {
                databaseMetadata.InitializeNewRecord(model, _database);
            }
        }
Пример #3
0
            public DataModelBase TryCache(int id, DataModelBase model)
            {
                lock (_cache)
                {
                    int           idx;
                    DataModelBase cached = Find(id, out idx);
                    if (cached != null)
                    {
                        return(cached);
                    }

                    _cache.Insert(idx, new KeyValuePair <int, WeakReference>(id, new WeakReference(model)));
                    return(model);
                }
            }
Пример #4
0
        void IDataModelCollection.Add(DataModelBase item)
        {
            if (IsReadOnly)
            {
                throw new ReadOnlyException("Cannot modify read only collection.");
            }

            if (IsModified)
            {
                Add((T)item);
            }
            else
            {
                _collection.Add((T)item);
                SetValueCore(CountProperty, _collection.Count);
            }
        }
Пример #5
0
            public void Cache(int id, DataModelBase model)
            {
                lock (_cache)
                {
                    var entry = new KeyValuePair <int, WeakReference>(id, new WeakReference(model));

                    int           idx;
                    DataModelBase cached = Find(id, out idx);
                    if (cached != null)
                    {
                        _cache[idx] = entry;
                    }
                    else
                    {
                        _cache.Insert(idx, entry);
                    }
                }
            }
Пример #6
0
 /// <summary>
 /// Commits a single model.
 /// </summary>
 protected abstract bool Commit(DataModelBase dataModel, ModelMetadata metadata);
Пример #7
0
        /// <summary>
        /// Commits changes made to a data model. The shared model and any future copies will contain committed changes.
        /// </summary>
        /// <param name="dataModel">Data model to commit.</param>
        /// <returns><c>true</c> if the changes were committed, <c>false</c> if not.</returns>
        public bool Commit(DataModelBase dataModel)
        {
            if (!dataModel.IsModified && !(dataModel is IDataModelCollection))
            {
                return(true);
            }

            var metadata = _metadataRepository.GetModelMetadata(dataModel.GetType());

            if (metadata == null)
            {
                return(false);
            }

            var collection = dataModel as IDataModelCollection;

            if (collection != null)
            {
                var collectionMetadata = (IDataModelCollectionMetadata)metadata;
                if (collectionMetadata.CommitItems)
                {
                    foreach (DataModelBase model in collection)
                    {
                        if (!Commit(model))
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    foreach (DataModelBase model in collection)
                    {
                        model.AcceptChanges();
                    }
                }
            }

            var key = metadata.GetKey(dataModel);

            _logger.Write("Committing {0}({1})", dataModel.GetType().Name, key);

            if (!Commit(dataModel, metadata))
            {
                _logger.WriteWarning("Commit failed {0}({1})", dataModel.GetType().Name, key);
                return(false);
            }

            var newKey = metadata.GetKey(dataModel);

            if (key != newKey)
            {
                _logger.WriteVerbose("New key for {0}:{1}", dataModel.GetType().Name, newKey);

                ExpireCollections(dataModel.GetType());

                var fieldMetadata = metadata.GetFieldMetadata(metadata.PrimaryKeyProperty);
                UpdateKeys(fieldMetadata, key, newKey);
            }

            dataModel.AcceptChanges();
            return(true);
        }
Пример #8
0
 /// <summary>
 /// Initializes a new record.
 /// </summary>
 /// <param name="model">The newly created model object.</param>
 /// <param name="metadata">Metadata about the model.</param>
 protected virtual void InitializeNewRecord(DataModelBase model, ModelMetadata metadata)
 {
     metadata.InitializePrimaryKey(model);
 }
Пример #9
0
 /// <summary>
 /// Gets the unmodified value of a property on a model.
 /// </summary>
 /// <param name="model">The model to examine.</param>
 /// <param name="property">The property to examine.</param>
 /// <returns>The unmodified value of the property (which may be the current value of the property).</returns>
 public static object GetOriginalValue(DataModelBase model, ModelProperty property)
 {
     return(model.GetOriginalValue(property));
 }
Пример #10
0
 /// <summary>
 /// Determines if the specified property is modified on a model.
 /// </summary>
 /// <param name="model">The model to examine.</param>
 /// <param name="property">The property to examine.</param>
 /// <returns><c>true</c> if the property has been modified on the model, <c>false</c> if not.</returns>
 public static bool IsPropertyModified(DataModelBase model, ModelProperty property)
 {
     return(model.IsModified && model.UpdatedPropertyKeys.Contains(property.Key));
 }
Пример #11
0
 bool IDataModelCollection.Contains(DataModelBase item)
 {
     return(_collection.Contains((T)item));
 }
Пример #12
0
        /// <summary>
        /// Commits a single model.
        /// </summary>
        protected override bool Commit(DataModelBase dataModel, ModelMetadata metadata)
        {
            var databaseMetadata = metadata as IDatabaseModelMetadata;

            return(databaseMetadata.Commit(dataModel, _database));
        }