Example #1
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);
        }
Example #2
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);
        }