private static IExtensibleEntity CreateObject(Type entityType, DbDataReader record, ExtensibleObjectContextBase context, bool updatableFromCache, bool skipDeletedItems, out IExtensibleEntity deletedEntity) { IExtensibleEntity returnEntity = null; deletedEntity = null; EntityKey key = CreateEntityKey(entityType, record); EntityCacheEntry entry = context.Cache.FindCacheEntry(key); if (entry != null) { returnEntity = entry.Entity; switch (entry.State) { case EntityRowState.Added: throw new InvalidOperationException("Added entity already exists in the cache"); case EntityRowState.Deleted: deletedEntity = entry.Entity; break; } if (deletedEntity != null && skipDeletedItems) { return(null); } else { return(returnEntity); //return the entity from the cache } } returnEntity = CreateEntity(entityType, record, context, true); //create entity from db record context.Cache.AddEntry(returnEntity, false); return(returnEntity); }
private void AddCacheEntryToDictionary(EntityCacheEntry entry, EntityRowState state) { switch (state) { case EntityRowState.Unchanged: _unchangedEntityStore[entry.Entity.Key] = entry; return; case (EntityRowState.Unchanged | EntityRowState.Detached): return; case EntityRowState.Added: _addedEntityStore[entry.Entity.Key] = entry; return; case EntityRowState.Deleted: _deletedEntityStore[entry.Entity.Key] = entry; return; case EntityRowState.Modified: _modifiedEntityStore[entry.Entity.Key] = entry; return; default: return; } }
/// <summary> /// Detach the entity from cache /// </summary> internal void DetachCache() { this._entityChangedHandler = null; this._cacheEntry = null; this._entityKey = null; //TODO:this.Relationships.DetachContext(); }
internal EntityCacheEntry AddEntry(IExtensibleEntity entity, bool newInstance) { //if (entity.Key == null) // throw new ArgumentNullException("entity.Key"); //TODO: If auto generate key is setted, generate the key values first //TODO: create entity key with key values if (entity.Key == null) { //create entity key entity.Key = new EntityKey((entity as ExtensibleEntity).Metadata, (entity as ExtensibleEntity).KeyValues); } //see whether these's a key confliction EntityCacheEntry entry = this.FindCacheEntry(entity.Key); if (entry != null) { if (entry.Entity != entity) //key conflict { throw new ArgumentException("key already exists", "entity"); } if (entry.State == EntityRowState.Deleted) { //entry.RevertDelete(); } return(entry); } entry = new EntityCacheEntry(entity, this, newInstance); entry.AttachCacheToEntity(); this.AddCacheEntryToDictionary(entry, entry.State); return(entry); }
protected IEnumerable <string> GenerateDeleteScripts(EntityCacheEntry cacheEntry) { ExtensibleEntity entity = cacheEntry.Entity as ExtensibleEntity; return(GenerateDeleteScripts(entity, entity.Metadata)); //TODO: delete relationships }
/// <summary> /// Attach the entity to cache /// </summary> /// <param name="cacheEntry"></param> /// <param name="changed"></param> internal void AttachCache(EntityCacheEntry cacheEntry, EntityChangedHandler changed) { if (this._cacheEntry != null) { throw new InvalidOperationException("Entity cannot exist in multiple object caches"); } this._cacheEntry = cacheEntry; this._entityChangedHandler = changed; }
public EntityCacheEntry GetCacheEntry(EntityKey key) { EntityCacheEntry entry = this.FindCacheEntry(key); if (entry == null) { throw new ArgumentException("No entry exists for EntityKey in the object cache"); } return(entry); }
internal void ChangeState(EntityCacheEntry entry, EntityRowState oldState, EntityRowState newState) { if (newState == EntityRowState.Detached) { RemoveCacheEntryFromDictionary(entry, oldState); entry.Reset(); } else { RemoveCacheEntryFromDictionary(entry, oldState); AddCacheEntryToDictionary(entry, newState); } }
private static bool IsCacheDirty(IExtensibleEntityCache entityCache) { bool flag = false; using (IEnumerator <EntityCacheEntry> enumerator = entityCache.GetCacheEntries(EntityRowState.Modified | EntityRowState.Deleted | EntityRowState.Added).GetEnumerator()) { while (enumerator.MoveNext()) { EntityCacheEntry entry = enumerator.Current; return(true); } } return(flag); }
internal EntityCacheEntry FindCacheEntry(EntityKey key) { if (key == null) { throw new ArgumentNullException("key"); } EntityCacheEntry entry = null; if ((!this._addedEntityStore.TryGetValue(key, out entry) && !this._modifiedEntityStore.TryGetValue(key, out entry)) && !this._deletedEntityStore.TryGetValue(key, out entry)) { this._unchangedEntityStore.TryGetValue(key, out entry); } return(entry); }
public void DeleteObject(IExtensibleEntity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } this.AssertNotDisposed(); if (entity.Key == null) { throw new ArgumentNullException("Can not delete entity without key", "entity"); } EntityCacheEntry entry = this.Cache.FindCacheEntry(entity.Key); if ((entry == null) || !object.ReferenceEquals(entry.Entity, entity)) { throw new InvalidOperationException("Can not delete entity not in cache"); } entry.Delete(); }
public IEnumerable <string> GenerateUpdateScripts(EntityCacheEntry cacheEntry) { switch (cacheEntry.State) { case EntityRowState.Added: return(GenerateInsertScripts(cacheEntry)); case EntityRowState.Deleted: return(GenerateDeleteScripts(cacheEntry)); case EntityRowState.Modified: { ExtensibleEntity entity = cacheEntry.Entity as ExtensibleEntity; return(GenerateUpdateScripts(entity, entity.Metadata, entity.GetModifiedFields())); } default: return(null); } }
public bool TryGetObjectByKey <T>(EntityKey key, out T value) { Type baseType = typeof(ExtensibleEntity); if (!baseType.IsAssignableFrom(typeof(T))) { throw new ArgumentException("Must use a type inherited from ExtensibleEntity", "T"); } if (key == null) { throw new ArgumentNullException("key"); } base.AssertNotDisposed(); value = default(T); //find from cache EntityCacheEntry entry = base.Cache.FindCacheEntry(key); if (entry != null) { value = (T)((entry.State != EntityRowState.Deleted) ? entry.Entity : null); return(value != null); } this.CheckConnection(); KeyValuePair <string, object>[] keyValues = new KeyValuePair <string, object> [key.KeyValues.Count]; key.KeyValues.CopyTo(keyValues, 0); EntityQuery <T> query = new EntityQuery <T>(this, keyValues); base.OpenConnection(); using (IEnumerator <T> enumerator = query.GetEnumerator()) { if (enumerator.MoveNext()) { value = enumerator.Current; } } return(value != null); }
private void RemoveCacheEntryFromDictionary(EntityCacheEntry entry, EntityRowState state) { switch (state) { case EntityRowState.Unchanged: _unchangedEntityStore.Remove(entry.Entity.Key); return; case (EntityRowState.Unchanged | EntityRowState.Detached): return; case EntityRowState.Added: _addedEntityStore.Remove(entry.Entity.Key); return; case EntityRowState.Deleted: _deletedEntityStore.Remove(entry.Entity.Key); return; case EntityRowState.Modified: _modifiedEntityStore.Remove(entry.Entity.Key); return; } }
internal static void EntityValueChangedHandler(EntityCacheEntry cacheEntry, string fieldName, object value) { cacheEntry.ChangeEntityValue(fieldName, value); }
public bool TryGetCacheEntry(EntityKey key, out EntityCacheEntry cacheEntry) { cacheEntry = this.FindCacheEntry(key); return(cacheEntry != null); }