Example #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));
        }
Example #2
0
        /// <summary>
        /// Locates an entity for retrieval from the <see cref="Locator"/> if tracking is enabled.
        /// </summary>
        /// <typeparam name="Entity">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 Entity LocateEntity <Entity>(string key, bool isLocatorEnabled) where Entity : class, IEntity, new()
        {
            Entity entity = null;

            //attempt to locate
            if (key != null && isLocatorEnabled)
            {
                lock (syncObject)
                {
                    if (EntityLocator.Contains(key))
                    {
                        entity = EntityLocator.Get(key) as Entity;
                    }
                }
            }
            return(entity);
        }
Example #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;
        }
Example #4
0
        /// <summary>
        /// Locates an entity for retrieval from the <see cref="Locator"/>, or instatiates a new instance
        /// of the entity if not currently being tracked.
        /// </summary>
        /// <typeparam name="Entity">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 Entity LocateOrCreate <Entity>(string key, string typeString, Type entityFactoryType, bool isLocatorEnabled) where Entity : class, IEntity, new()
        {
            #region Validation
            if (string.IsNullOrEmpty(typeString))
            {
                throw new ArgumentException("typeString");
            }

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

            Entity entity = default(Entity);

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

                //see if entity is cachable, if IEntityCacheItem
                //retrieve from cache.
                if (isCacheable)
                {
                    entity = EntityCache.GetItem <Entity>(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 Entity;
                        }
                    }
                }

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

                //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);
        }