Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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;
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Detach the entity from cache
 /// </summary>
 internal void DetachCache()
 {
     this._entityChangedHandler = null;
     this._cacheEntry           = null;
     this._entityKey            = null;
     //TODO:this.Relationships.DetachContext();
 }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        protected IEnumerable <string> GenerateDeleteScripts(EntityCacheEntry cacheEntry)
        {
            ExtensibleEntity entity = cacheEntry.Entity as ExtensibleEntity;

            return(GenerateDeleteScripts(entity, entity.Metadata));
            //TODO: delete relationships
        }
Exemplo n.º 6
0
 /// <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;
 }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
 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);
     }
 }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        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();
        }
Exemplo n.º 12
0
        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);
            }
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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;
            }
        }
Exemplo n.º 15
0
 internal static void EntityValueChangedHandler(EntityCacheEntry cacheEntry, string fieldName, object value)
 {
     cacheEntry.ChangeEntityValue(fieldName, value);
 }
Exemplo n.º 16
0
 public bool TryGetCacheEntry(EntityKey key, out EntityCacheEntry cacheEntry)
 {
     cacheEntry = this.FindCacheEntry(key);
     return(cacheEntry != null);
 }