示例#1
0
        /// <summary>
        /// Adds the context entities from headers.
        /// </summary>
        protected virtual void AddContextEntitiesFromHeaders()
        {
            foreach (var kvp in Headers)
            {
                //
                // Skip any header that isn't an entity context header.
                //
                if (!kvp.Key.StartsWith("X-ENTITYCONTEXT-", StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                //
                // Determine the the entity type in question.
                //
                var entityName = kvp.Key.Substring(16);
                var type       = EntityTypeCache.All()
                                 .Where(a => a.IsEntity)
                                 .FirstOrDefault(a => a.FriendlyName.Equals(entityName, StringComparison.InvariantCultureIgnoreCase))
                                 ?.GetEntityType();
                string entityKey = kvp.Value.First();

                //
                // If we got an unknown type or no Id/Guid then skip.
                //
                if (type == null || entityKey.IsNullOrWhiteSpace())
                {
                    continue;
                }

                //
                // Lazy load the entity so we don't actually load if it is never
                // accessed.
                //
                ContextEntities.AddOrReplace(type, new Lazy <IEntity>(() =>
                {
                    IEntity entity = null;

                    if (int.TryParse(entityKey, out int entityId))
                    {
                        entity = Reflection.GetIEntityForEntityType(type, entityId);
                    }
                    else if (Guid.TryParse(entityKey, out Guid entityGuid))
                    {
                        entity = Reflection.GetIEntityForEntityType(type, entityGuid);
                    }

                    if (entity != null && entity is IHasAttributes attributedEntity)
                    {
                        Helper.LoadAttributes(attributedEntity);
                    }

                    return(entity);
                }));
            }
        }
        /// <summary>
        /// Adds the context entities for page.
        /// </summary>
        /// <param name="pageCache">The page cache.</param>
        internal virtual void AddContextEntitiesForPage(PageCache pageCache)
        {
            foreach (var pageContext in pageCache.PageContexts)
            {
                var entityType = EntityTypeCache.Get(pageContext.Key)?.GetEntityType();
                if (entityType == null)
                {
                    continue;
                }

                int?contextId = GetPageParameter(pageContext.Value).AsIntegerOrNull();
                if (contextId.HasValue)
                {
                    ContextEntities.AddOrReplace(entityType, new Lazy <IEntity>(() =>
                    {
                        var entity = Reflection.GetIEntityForEntityType(entityType, contextId.Value);

                        if (entity != null && entity is IHasAttributes attributedEntity)
                        {
                            Helper.LoadAttributes(attributedEntity);
                        }

                        return(entity);
                    }));
                }

                Guid?contextGuid = GetPageParameter(pageContext.Value).AsGuidOrNull();
                if (contextGuid.HasValue)
                {
                    ContextEntities.AddOrReplace(entityType, new Lazy <IEntity>(() =>
                    {
                        var entity = Reflection.GetIEntityForEntityType(entityType, contextGuid.Value);

                        if (entity != null && entity is IHasAttributes attributedEntity)
                        {
                            Helper.LoadAttributes(attributedEntity);
                        }

                        return(entity);
                    }));
                }
            }
        }