Esempio n. 1
0
        /// <summary>
        /// Stops Tracking an Entity, it will be re-added in the next round.
        /// </summary>
        /// <param name="key">Entity Key used in the Locator's Bucket</param>
        /// <returns>true if found, false if not found</returns>
        public static bool StopTracking(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            return(EntityLocator.Remove(key));
        }
Esempio n. 2
0
        /// <summary>
        /// Locates an entity for retrieval from the <see cref="EntityLocator"/> if tracking is enabled.
        /// </summary>
        /// <typeparam name="TEntity">Must implement <see cref="IEntity"/> and is the default type to create, and will be the return type.</typeparam>
        /// <param name="key">primary key representation</param>
        /// <param name="isLocatorEnabled">bool determining whether to use Entity Locating.</param>
        /// <returns>found entity of T, or null</returns>
        public static TEntity LocateEntity <TEntity>(string key, bool isLocatorEnabled) where TEntity : class, IEntity, new()
        {
            TEntity entity = null;

            //attempt to locate
            if (key != null && isLocatorEnabled)
            {
                lock (syncObject)
                {
                    if (EntityLocator.Contains(key))
                    {
                        entity = EntityLocator.Get(key) as TEntity;
                    }
                }
            }
            return(entity);
        }
Esempio n. 3
0
        /// <summary>
        /// Starts Tracking an Entity, it will be tracked until modified or persisted.
        /// </summary>
        /// <param name="key">Entity Key used in the Locator's Bucket</param>
        /// <param name="entity">entity to be tracked</param>
        /// <param name="isTrackingEnabled">Determines whether tracking is enabled</param>
        public static void StartTracking(string key, IEntity entity, bool isTrackingEnabled)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            if (!entity.IsEntityTracked && isTrackingEnabled)
            {
                EntityLocator.Add(key, entity);
                entity.IsEntityTracked   = true;
                entity.EntityTrackingKey = key;
            }

            return;
        }
Esempio n. 4
0
        /// <summary>
        /// Locates an entity for retrieval from the <see cref="EntityLocator"/>, or instatiates a new instance
        /// of the entity if not currently being tracked.
        /// </summary>
        /// <typeparam name="TEntity">Must implement <see cref="IEntity"/> and is the default type to create, and will be the return type.</typeparam>
        /// <param name="key">primary key representation</param>
        /// <param name="typeString">type string to create</param>
        /// <param name="entityFactoryType">factory used to try to create this entity.</param>
        /// <param name="isLocatorEnabled">bool determining whether to use Entity Locating.</param>
        /// <returns>Created entity of T</returns>
        public static TEntity LocateOrCreate <TEntity>(string key, string typeString, Type entityFactoryType, bool isLocatorEnabled) where TEntity : class, IEntity, new()
        {
            #region Validation
            if (string.IsNullOrEmpty(typeString))
            {
                throw new ArgumentException("typeString");
            }

            if (entityFactoryType == null)
            {
                throw new ArgumentException("entityFactoryType");
            }
            #endregion

            TEntity entity = default(TEntity);

            lock (syncObject)             //This is here because most of the classes in ObjectBuilder are NOT thread-safe
            {
                //Generated Table Entities Type
                Type defaultType = typeof(TEntity);
                bool isCacheable = defaultType.GetInterface("IEntityCacheItem") != null;

                //see if entity is cachable, if IEntityCacheItem
                //retrieve from cache.
                if (isCacheable)
                {
                    entity = EntityCache.GetItem <TEntity>(key.ToString());
                }

                if (entity != null)
                {
                    return(entity);
                }

                IEntityFactory factory = null;
                lock (syncObject)
                {
                    if (EntityFactories.ContainsKey(entityFactoryType.FullName))
                    {
                        factory = EntityFactories[entityFactoryType.FullName];
                    }
                    else
                    {
                        factory = TryAddEntityFactory(entityFactoryType);
                    }
                }


                //attempt to locate
                if (key != null && isLocatorEnabled)
                {
                    lock (syncObject)
                    {
                        if (EntityLocator.Contains(key))
                        {
                            entity = EntityLocator.Get(key) as TEntity;
                        }
                    }
                }

                //if not found try create from factory
                if (entity == null)
                {
                    entity = factory.CreateEntity(typeString, defaultType) as TEntity;
                }

                //add to locator and start tracking.
                if (!entity.IsEntityTracked)
                {
                    StartTracking(key, entity, isLocatorEnabled);
                }

                //add entity to Cache if IEntityCacheItem
                if (entity.GetType().GetInterface("IEntityCacheItem") != null)
                {
                    EntityCache.AddCache(key, entity);
                }
            }

            return(entity);
        }