/// <summary> /// Creates and populates a new entity, or returns the existing one from the cache. /// </summary> /// <param name="factory">The entity factory.</param> /// <param name="cacheKey">The key used for caching.</param> /// <param name="init">The initializer for the entity.</param> /// <param name="dictionary">The dictionary to use for caching.</param> /// <returns>The new/existing entity.</returns> private TEntity CreateEntity <TInit, TCacheKey, TEntity>(CachedEntityFactory <TInit, TEntity> factory, TCacheKey cacheKey, TInit init, IDictionary <TCacheKey, CachedEntity> dictionary) where TCacheKey : notnull where TEntity : CachedEntity { if (dictionary.TryGetValue(cacheKey, out var cached)) { return((TEntity)cached); } using (StackGuard) { var label = GetNewLabel(); var entity = factory.Create(this, init); entity.Label = label; dictionary[cacheKey] = entity; DefineLabel(entity); if (entity.NeedsPopulation) { Populate(init as ISymbol, entity); } #if DEBUG_LABELS using var id = new StringWriter(); entity.WriteQuotedId(id); CheckEntityHasUniqueLabel(id.ToString(), entity); #endif return(entity); } }
internal TEntity CreateEntityFromSymbol <TSymbol, TEntity>(CachedEntityFactory <TSymbol, TEntity> factory, TSymbol init) where TSymbol : ISymbol where TEntity : CachedEntity => CreateEntity(factory, init, init, symbolEntityCache);
/// <summary> /// Creates and populates a new entity from an `ISymbol`, or returns the existing one /// from the cache. /// </summary> /// <typeparam name="TSymbol">The type used to construct the entity.</typeparam> /// <typeparam name="TEntity">The type of the entity to create.</typeparam> /// <param name="factory">The factory used to construct the entity.</param> /// <param name="cx">The extractor context.</param> /// <param name="init">The initializer for the entity.</param> /// <returns>The entity.</returns> public static TEntity CreateEntityFromSymbol <TSymbol, TEntity>(this CachedEntityFactory <TSymbol, TEntity> factory, Context cx, TSymbol init) where TSymbol : ISymbol where TEntity : CachedEntity => cx.CreateEntityFromSymbol(factory, init);
internal TEntity CreateEntity <TInit, TEntity>(CachedEntityFactory <TInit, TEntity> factory, object cacheKey, TInit init) where TEntity : CachedEntity => cacheKey is ISymbol s?CreateEntity(factory, s, init, symbolEntityCache) : CreateEntity(factory, cacheKey, init, objectEntityCache);
/// <summary> /// Creates and populates a new entity, or returns the existing one from the cache, /// based on the supplied cache key. /// </summary> /// <typeparam name="TInit">The type used to construct the entity.</typeparam> /// <typeparam name="TEntity">The type of the entity to create.</typeparam> /// <param name="factory">The factory used to construct the entity.</param> /// <param name="cx">The extractor context.</param> /// <param name="cacheKey">The key used for caching.</param> /// <param name="init">The initializer for the entity.</param> /// <returns>The entity.</returns> public static TEntity CreateEntity <TInit, TEntity>(this CachedEntityFactory <TInit, TEntity> factory, Context cx, object cacheKey, TInit init) where TEntity : CachedEntity => cx.CreateEntity(factory, cacheKey, init);