public static Entity LocateOrCreate <Entity>(string key, string typeString, Type entityFactoryType, bool isLocatorEnabled) where Entity : class, IEntity, new() { if (string.IsNullOrEmpty(typeString)) { throw new ArgumentException("typeString"); } if (entityFactoryType == null) { throw new ArgumentException("entityFactoryType"); } Entity entity = default(Entity); Type defaultType = typeof(Entity); if (defaultType.GetInterface("IEntityCacheItem") != null) { entity = EntityCache.GetItem <Entity>(key.ToString()); } if (entity == null) { IEntityFactory factory = null; if (EntityFactories.ContainsKey(entityFactoryType.FullName)) { factory = EntityFactories[entityFactoryType.FullName]; } else { factory = TryAddEntityFactory(entityFactoryType); } if (((key != null) && isLocatorEnabled) && EntityLocator.Contains(key)) { entity = EntityLocator.Get(key) as Entity; } if (entity == null) { entity = factory.CreateEntity(typeString, defaultType) as Entity; } if (!entity.IsEntityTracked) { StartTracking(key, entity, isLocatorEnabled); } if (entity.GetType().GetInterface("IEntityCacheItem") != null) { EntityCache.AddCache(key, entity); } } return(entity); }
public ClientPools(Type commandType, IEnumerable <Type> eventTypes, IEnumerable <KeyValuePair <Type, Type> > entityTypes) : base(commandType, eventTypes) { // Register Entities foreach (var pair in entityTypes) { var entityType = pair.Key; var stateType = pair.Value; var statePool = CreatePool <State>(stateType); var entityPool = CreatePool <ClientEntity>(entityType); var typeKey = StatePools.Count + 1; // 0 is an invalid type StatePools.Add(typeKey, statePool); EntityFactories.Add(typeKey, entityPool); EntityTypeToKey.Add(entityType, typeKey); } EntityTypeCompressor = new Int32Compressor(0, EntityFactories.Count + 1); }
public static IEntityFactory TryAddEntityFactory(Type entityFactoryTypeToCreate) { lock (syncObject) { if (entityFactoryTypeToCreate == null) { throw new ArgumentException("entityFactoryTypeToCreate"); } if (!EntityFactories.ContainsKey(entityFactoryTypeToCreate.FullName)) { IEntityFactory factory = Activator.CreateInstance(entityFactoryTypeToCreate) as IEntityFactory; if (factory == null) { throw new ArgumentException("This factory can not be found. Please ensure that you are using a valid Entity Factory.", "entityFactoryType"); } EntityFactories.Add(entityFactoryTypeToCreate.FullName, factory); } } return(EntityFactories[entityFactoryTypeToCreate.FullName]); }
/// <summary> /// instatiates a new instance of the entity for view entities that don't implement IEntity and can't be tracked /// </summary> /// <typeparam name="Entity">is the default type to create, and will be the return type.</typeparam> /// <param name="typeString">type string to create</param> /// <param name="entityFactoryType">factory used to try to create this entity.</param> /// <returns>Created entity of T</returns> public static Entity CreateViewEntity <Entity>(string typeString, Type entityFactoryType) where Entity : class, new() { #region Validation if (string.IsNullOrEmpty(typeString)) { throw new ArgumentException("typeString"); } if (entityFactoryType == null) { throw new ArgumentException("entityFactoryType"); } #endregion Entity entity = default(Entity); //Generated Table Entities Type Type defaultType = typeof(Entity); IEntityFactory factory = null; lock (syncObject) { if (EntityFactories.ContainsKey(entityFactoryType.FullName)) { factory = EntityFactories[entityFactoryType.FullName]; } else { factory = TryAddEntityFactory(entityFactoryType); } } entity = factory.CreateViewEntity(typeString, defaultType) as Entity; return(entity); }
public static Entity CreateViewEntity <Entity>(string typeString, Type entityFactoryType) where Entity : class, new() { if (string.IsNullOrEmpty(typeString)) { throw new ArgumentException("typeString"); } if (entityFactoryType == null) { throw new ArgumentException("entityFactoryType"); } Entity local = default(Entity); Type defaultType = typeof(Entity); IEntityFactory factory = null; if (EntityFactories.ContainsKey(entityFactoryType.FullName)) { factory = EntityFactories[entityFactoryType.FullName]; } else { factory = TryAddEntityFactory(entityFactoryType); } return(factory.CreateViewEntity(typeString, defaultType) as 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); }