Exemple #1
0
        private ResourceObject GetIncludedEntity(IIdentifiable entity, RelationshipAttribute relationship)
        {
            if (entity == null)
            {
                return(null);
            }

            var contextEntity      = _jsonApiContext.ResourceGraph.GetContextEntity(entity.GetType());
            var resourceDefinition = _scopedServiceProvider.GetService(contextEntity.ResourceType) as IResourceDefinition;

            var data = GetData(contextEntity, entity, resourceDefinition);

            data.Attributes = new Dictionary <string, object>();

            contextEntity.Attributes.ForEach(attr =>
            {
                var attributeValue = attr.GetValue(entity);
                if (ShouldIncludeAttribute(attr, attributeValue, relationship))
                {
                    data.Attributes.Add(attr.PublicAttributeName, attributeValue);
                }
            });

            return(data);
        }
Exemple #2
0
        private ResourceObject GetIncludedEntity(IIdentifiable entity)
        {
            if (entity == null)
            {
                return(null);
            }

            var contextEntity      = _jsonApiContext.ResourceGraph.GetContextEntity(entity.GetType());
            var resourceDefinition = _scopedServiceProvider.GetService(contextEntity.ResourceType) as IResourceDefinition;

            var data = GetData(contextEntity, entity, resourceDefinition);

            data.Attributes = new Dictionary <string, object>();

            foreach (var attr in contextEntity.Attributes)
            {
                if (attr.InternalAttributeName == nameof(Identifiable.Id))
                {
                    continue;
                }

                data.Attributes.Add(attr.PublicAttributeName, attr.GetValue(entity));
            }

            return(data);
        }
        public Document Build(IIdentifiable entity)
        {
            var contextEntity = _resourceGraph.GetContextEntity(entity.GetType());

            var resourceDefinition = _scopedServiceProvider?.GetService(contextEntity.ResourceType) as IResourceDefinition;
            var document           = new Document
            {
                Data = GetData(contextEntity, entity, resourceDefinition),
                Meta = GetMeta(entity)
            };

            if (ShouldIncludePageLinks(contextEntity))
            {
                document.Links = _jsonApiContext.PageManager.GetPageLinks(new LinkBuilder(_jsonApiContext));
            }

            document.Included = AppendIncludedObject(document.Included, contextEntity, entity);

            return(document);
        }
        public object Compose(IValueContainer container, Type valueType)
        {
            var entityKeyContainer = (EntityKeyContainer)container;
            var entityClrType      = entityKeyContainer.GetObjectType();
            var dbContextType      = _entityToContextTypeMap[entityClrType];
            var dbContext          = (DbContext)_scopedServiceProvider.GetService(dbContextType);
            var entity             = dbContext.Find(entityClrType, entityKeyContainer.GetValues());

            // What if an entity does not exist anymore? Is returning NULL ok?
            return(entity);
        }
        public object Compose(IValueContainer container, Type valueType)
        {
            var dbContextContainer  = (DbContextContainer)container;
            var targetDbContextType = GetDbContextType(valueType);

            if (targetDbContextType.FullName != dbContextContainer.Type)
            {
                targetDbContextType = _knownDbContextTypes.Types.SingleOrDefault(t => t.FullName == dbContextContainer.Type);
            }
            if (targetDbContextType == null)
            {
                throw new InvalidOperationException($"Cannot find a known DbContext of type '{dbContextContainer.Type}'.");
            }
            return(_scopedServiceProvider.GetService(targetDbContextType));
        }
        private DocumentData GetIncludedEntity(IIdentifiable entity)
        {
            if (entity == null)
            {
                return(null);
            }

            var contextEntity      = _jsonApiContext.ContextGraph.GetContextEntity(entity.GetType());
            var resourceDefinition = _scopedServiceProvider.GetService(contextEntity.ResourceType) as IResourceDefinition;

            var data = GetData(contextEntity, entity, resourceDefinition);

            data.Attributes = new Dictionary <string, object>();

            contextEntity.Attributes.ForEach(attr =>
            {
                data.Attributes.Add(attr.PublicAttributeName, attr.GetValue(entity));
            });

            return(data);
        }
 /// <inheritdoc/>
 public IResourceDefinition Get(Type resourceType)
 {
     return((IResourceDefinition)_serviceProvider.GetService(_resourceContextProvider.GetResourceContext(resourceType).ResourceDefinitionType));
 }