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