Пример #1
0
        public static bool TryGetInlineODataEntities(
            this ODataEntityDto oDataEntity,
            string navigationPropertyName,
            out IEnumerable <ODataEntityDto> includedODataEntities)
        {
            var navigationProperty = oDataEntity.NavigationProperties.FirstOrDefault(p => p.Name == navigationPropertyName);

            if (navigationProperty != null)
            {
                var collectionValue = navigationProperty.Value as IEnumerable <ODataEntityDto>;
                if (collectionValue != null)
                {
                    includedODataEntities = collectionValue;
                    return(true);
                }

                var value = navigationProperty.Value as ODataEntityDto;
                if (value != null)
                {
                    includedODataEntities = value.Enumerate();
                    return(true);
                }
            }

            includedODataEntities = null;
            return(false);
        }
Пример #2
0
        private static bool TryGetEntityReferenceLinks(
            ODataEntityDto oDataEntity,
            string navigationPropertyName,
            out IEnumerable <ODataPath> referenceLinks)
        {
            var navigationProperty = oDataEntity.NavigationProperties.FirstOrDefault(p => p.Name == navigationPropertyName);

            if (navigationProperty != null)
            {
                var collectionValue = navigationProperty.Value as IEnumerable <ODataPath>;
                if (collectionValue != null)
                {
                    referenceLinks = collectionValue;
                    return(true);
                }

                var value = navigationProperty.Value as ODataPath;
                if (value != null)
                {
                    referenceLinks = value.Enumerate();
                    return(true);
                }
            }

            referenceLinks = null;
            return(false);
        }
Пример #3
0
        private bool TryHandlePostInContainedNavigationProperty(
            IOperationContext context,
            ODataEntityDto oDataEntity,
            out IEntity createdEntity)
        {
            var navigationProperty = context.NavigationTarget as INavigationProperty;

            if (navigationProperty == null || !navigationProperty.IsContained())
            {
                createdEntity = null;
                return(false);
            }

            var bindableModelContext = context.ModelContext.As <IBindableModelContext>();
            var parentPath           = context.Path.Take(context.Path.Count - 1);
            var parentEntity         = this.entityReader.ReadEntitiesFromPath(bindableModelContext, parentPath).SingleOrDefault();

            if (parentEntity == null)
            {
                throw new KeyNotFoundException("The preceeding entity could not be found.");
            }

            createdEntity = this.entityCreator.CreateInContainedNavigationProperty(
                bindableModelContext,
                navigationProperty,
                context.NavigationRoot,
                parentEntity,
                oDataEntity);

            return(true);
        }
Пример #4
0
 protected void SetPropertyValues(
     IBindableModelContext context,
     IEntitySet navigationRoot,
     IEntity targetEntity,
     ODataEntityDto oDataEntity)
 {
     this.SetPropertyValues(context, navigationRoot, targetEntity, oDataEntity, Enumerable.Empty <string>());
 }
Пример #5
0
        public IEnumerable <ODataEntityDto> Handle(IOperationContext context, ODataEntityDto incomingODataEntity)
        {
            var entities = this.entityReader.ReadEntitiesFromPath(context.ModelContext.As <IBindableModelContext>(), context.Path);

            return(this.SerializeEntities(
                       context.ModelContext.As <IBindableModelContext>(),
                       entities,
                       context.GetTargetEntityType(),
                       context.QueryOptions.SelectExpandClause));
        }
Пример #6
0
        private static bool TryGetPropertyDependency(IStructuralProperty property, ODataEntityDto oDataEntity, out IDependency dependency)
        {
            object propertyValue;

            if (oDataEntity.TryGetPropertyValue(property, out propertyValue))
            {
                dependency = new PropertyDependency(property, propertyValue);
                return(true);
            }

            dependency = null;
            return(false);
        }
Пример #7
0
        public IEntity CreateInContainedNavigationProperty(
            IBindableModelContext context,
            INavigationProperty navigationProperty,
            IEntitySet navigationRoot,
            IEntity parentEntity,
            ODataEntityDto oDataEntity)
        {
            var dependencies  = this.dependencyResolver.ResolveDependencies(context, navigationProperty, navigationRoot, oDataEntity);
            var createdEntity = navigationProperty.CreateEntityInContainedNavigationProperty(context, parentEntity, dependencies);

            this.SetPropertyValues(context, navigationRoot, createdEntity, oDataEntity, dependencies.Keys);
            return(createdEntity);
        }
Пример #8
0
        public static bool TryGetPropertyValue(this ODataEntityDto oDataEntity, IStructuralProperty property, out object value)
        {
            var targetProperty = oDataEntity.Entry.Properties.FirstOrDefault(p => p.Name == property.EdmPropertyName);

            if (targetProperty != null)
            {
                var odataPropertyValue = targetProperty.Value;
                value = property.Deserialize(odataPropertyValue);
                return(true);
            }

            value = null;
            return(false);
        }
Пример #9
0
        public IEnumerable <ODataEntityDto> Handle(IOperationContext context, ODataEntityDto incomingODataEntity)
        {
            var modelContext = context.ModelContext.As <IBindableModelContext>();

            var entityToPatch = this.entityReader.ReadEntitiesFromPath(modelContext, context.Path).SingleOrDefault();

            if (entityToPatch == null)
            {
                throw new KeyNotFoundException("The specified entity could not be found.");
            }

            this.entityUpdater.UpdateEntity(modelContext, context.NavigationRoot, entityToPatch, incomingODataEntity);
            return(context.CreatePatchOrPostResponse(entityToPatch, this.dtoBuilderFactory).Enumerate());
        }
Пример #10
0
        private bool TryHandlePostInEntitySet(IOperationContext context, ODataEntityDto oDataEntity, out IEntity createdEntity)
        {
            var entitySet = context.NavigationTarget as IEntitySet;

            if (entitySet == null)
            {
                createdEntity = null;
                return(false);
            }

            var bindableModelContext = context.ModelContext.As <IBindableModelContext>();

            createdEntity = this.entityCreator.CreateInEntitySet(bindableModelContext, context.NavigationTarget.As <IEntitySet>(), oDataEntity);
            return(true);
        }
Пример #11
0
        public static IEntityType GetEntityType(this ODataEntityDto oDataEntity, IModelContext context)
        {
            var edmEntityType = context.EdmModel.FindDeclaredType(oDataEntity.Entry.TypeName);

            if (edmEntityType != null)
            {
                IEntityType entityType;
                if (context.TryGetEntityType(edmEntityType.Name, out entityType))
                {
                    return(entityType);
                }
            }

            throw new KeyNotFoundException("The entity type " + oDataEntity.Entry.TypeName + " could not be found.");
        }
Пример #12
0
        public IEnumerable <ODataEntityDto> Handle(IOperationContext context, ODataEntityDto incomingODataEntity)
        {
            IEntity createdEntity;

            if (this.TryHandlePostInContainedNavigationProperty(context, incomingODataEntity, out createdEntity))
            {
                return(context.CreatePatchOrPostResponse(createdEntity, this.dtoBuilderFactory).Enumerate());
            }

            if (this.TryHandlePostInEntitySet(context, incomingODataEntity, out createdEntity))
            {
                return(context.CreatePatchOrPostResponse(createdEntity, this.dtoBuilderFactory).Enumerate());
            }

            throw new InvalidOperationException("The POST operation cannot be performed against the specified URL.");
        }
Пример #13
0
        protected override bool TryHandleStructuralProperty(
            IEntity targetEntity,
            ODataEntityDto oDataEntity,
            IStructuralProperty structuralProperty)
        {
            var structuralEntityProperty = structuralProperty as IStructuralEntityProperty;

            if (structuralEntityProperty != null && structuralEntityProperty.IsReadOnly(targetEntity))
            {
                // According to OData v4 Part 1 (Protocol), section 11.4.3 and 11.4.4, properties which are non-updatable should be ignored
                // during Update and Upsert. Hence, we return true here, claiming the property was handled correctly.
                return(true);
            }

            return(base.TryHandleStructuralProperty(targetEntity, oDataEntity, structuralProperty));
        }
Пример #14
0
        private bool TryParseReferencedEntities(
            IBindableModelContext context,
            ODataEntityDto oDataEntity,
            INavigationProperty navigationProperty,
            List <IEntity> resultingEntities)
        {
            IEnumerable <IEntity> referencedEntities;

            if (this.referenceParser.TryParseReferencedEntities(context, navigationProperty, oDataEntity, out referencedEntities))
            {
                resultingEntities.AddRange(referencedEntities);
                return(true);
            }

            return(false);
        }
Пример #15
0
        public bool TrySetOnEntity(IEntity targetEntity, ODataEntityDto sourceODataEntity, IStructuralEntityProperty property)
        {
            object value;

            if (!sourceODataEntity.TryGetPropertyValue(property, out value))
            {
                return(false);
            }

            if (property.IsReadOnly(targetEntity))
            {
                return(false);
            }

            return(property.TrySetValueOnEntity(targetEntity, value));
        }
Пример #16
0
        private IEnumerable <ODataEntityDto> Execute(
            Operation operation, ODataPath path, QueryOptions queryOptions, ODataEntityDto incomingObject)
        {
            var operationContext = this.operationContextFactory.Create(this.modelContext, operation, path, queryOptions);
            IOperationHandler relevantHandler;

            if (!this.modelContext.TryGetHandler(operationContext, out relevantHandler))
            {
                throw new InvalidOperationException($"No handler found that could handle the {operation} operation on {path.Last()}.");
            }

            // We need to make this call synchronous in the current thread, because otherwise we might not have a unit of work
            var operationResult = relevantHandler.Handle(operationContext, incomingObject).ToArray();

            return(operationResult);
        }
Пример #17
0
        protected virtual bool TryHandleStructuralProperty(
            IEntity targetEntity,
            ODataEntityDto oDataEntity,
            IStructuralProperty structuralProperty)
        {
            var structuralEntityProperty = structuralProperty as IStructuralEntityProperty;

            if (structuralEntityProperty == null)
            {
                return(false);
            }

            if (!this.structuralPropertyBinder.TrySetOnEntity(targetEntity, oDataEntity, structuralEntityProperty))
            {
                return(false);
            }

            return(true);
        }
Пример #18
0
        public bool TryGetLinkedEntities(
            IBindableModelContext context,
            ODataEntityDto oDataEntity,
            INavigationProperty navigationProperty,
            out IEnumerable <IEntity> entities)
        {
            if (navigationProperty.IsContained())
            {
                throw new ArgumentException("Navigation property must be uncontained.", nameof(navigationProperty));
            }

            var resultingEntities = new List <IEntity>();

            if (this.TryParseReferencedEntities(context, oDataEntity, navigationProperty, resultingEntities))
            {
                entities = resultingEntities;
                return(true);
            }

            entities = null;
            return(false);
        }
Пример #19
0
        protected override bool TryHandleNavigationProperty(
            IBindableModelContext context,
            IEntity targetEntity,
            ODataEntityDto oDataEntity,
            INavigationProperty navigationProperty,
            IEntitySet navigationRoot)
        {
            if (navigationProperty.IsContained())
            {
                throw new InvalidOperationException("Only uncontained navigation properties can be processed while updating the parent.");
            }

            IEnumerable <IEntity> entities;

            if (this.navigationPropertyParser.TryGetLinkedEntities(context, oDataEntity, navigationProperty, out entities))
            {
                this.navigationPropertyBinder.SetUncontainedNavigationProperty(context, targetEntity, navigationProperty, entities);
                return(true);
            }

            return(false);
        }
Пример #20
0
        public IDictionary <string, IDependency> ResolveDependencies(
            IBindableModelContext context,
            INavigatable navigationTarget,
            IEntitySet navigationRoot,
            ODataEntityDto oDataEntity)
        {
            //var changedPropertyNames = oDataEntity.GetChangedPropertyNames();
            var changedPropertyNames = oDataEntity.Entry.Properties.Select(p => p.Name).Concat(oDataEntity.NavigationProperties.Select(np => np.Name)).ToArray();             // TODO
            var dependencies         = new Dictionary <string, IDependency>();

            foreach (var declaration in context.GetDependencies(navigationTarget))
            {
                IDependency dependency;

                var property = declaration.DependableElement as IStructuralProperty;
                if (property != null && changedPropertyNames.Contains(property.EdmPropertyName) &&
                    TryGetPropertyDependency(property, oDataEntity, out dependency))
                {
                    dependencies[property.EdmPropertyName] = dependency;
                    continue;
                }

                var navigationProperty = declaration.DependableElement as INavigationProperty;
                if (navigationProperty != null && changedPropertyNames.Contains(navigationProperty.Name) &&
                    this.TryGetUncontainedNavigationPropertyDependency(context, navigationProperty, navigationRoot, oDataEntity, out dependency))
                {
                    dependencies[navigationProperty.Name] = dependency;
                    continue;
                }

                if (!declaration.IsOptional)
                {
                    throw new NotSupportedException("The dependency is unknown and could not be resolved.");
                }
            }

            return(dependencies);
        }
Пример #21
0
        private bool TryParseAndCreateInlineEntities(
            IBindableModelContext context,
            ODataEntityDto oDataEntity,
            INavigationProperty navigationProperty,
            IEntitySet navigationRoot,
            List <IEntity> resultingEntities)
        {
            IEnumerable <ODataEntityDto> inlineODataEntities;

            if (oDataEntity.TryGetInlineODataEntities(navigationProperty.Name, out inlineODataEntities))
            {
                if (this.uncontainedEntitiesFactory == null)
                {
                    throw new InvalidOperationException("Cannot create uncontained entities. Factory hasn't been set.");
                }

                var createdEntities = this.uncontainedEntitiesFactory(context, navigationProperty, navigationRoot, inlineODataEntities);
                resultingEntities.AddRange(createdEntities);
                return(true);
            }

            return(false);
        }
Пример #22
0
        private bool TryGetUncontainedNavigationPropertyDependency(
            IBindableModelContext context,
            INavigationProperty navigationProperty,
            IEntitySet navigationRoot,
            ODataEntityDto oDataEntity,
            out IDependency dependency)
        {
            if (navigationProperty.IsContained())
            {
                throw new InvalidOperationException("A contained navigation property cannot be included as a dependency.");
            }

            IEnumerable <IEntity> entities;

            if (this.navigationPropertyParser.TryGetLinkedOrInlineEntities(context, oDataEntity, navigationProperty, navigationRoot, out entities))
            {
                dependency = new NavigationPropertyDependency(navigationProperty, entities);
                return(true);
            }

            dependency = null;
            return(false);
        }
Пример #23
0
        protected void SetPropertyValues(
            IBindableModelContext context,
            IEntitySet navigationRoot,
            IEntity targetEntity,
            ODataEntityDto oDataEntity,
            IEnumerable <string> propertiesToExclude)
        {
            var entityType         = oDataEntity.GetEntityType(context);
            var relevantProperties = oDataEntity.Entry.Properties.Where(p => !propertiesToExclude.Contains(p.Name));

            foreach (var property in relevantProperties)
            {
                IStructuralProperty structuralProperty;
                if (entityType.TryGetStructuralProperty(property.Name, out structuralProperty))
                {
                    if (!this.TryHandleStructuralProperty(targetEntity, oDataEntity, structuralProperty))
                    {
                        throw new InvalidOperationException("The included structural property \"" + property.Name + "\" could not be processed.");
                    }

                    continue;
                }

                INavigationProperty navigationProperty;
                if (entityType.TryGetNavigationProperty(property.Name, out navigationProperty))
                {
                    if (!this.TryHandleNavigationProperty(context, targetEntity, oDataEntity, navigationProperty, navigationRoot))
                    {
                        throw new InvalidOperationException("The included navigation property \"" + property.Name + "\" could not be processed.");
                    }

                    continue;
                }

                throw new NotSupportedException("The included property \"" + property.Name + "\" is unsupported.");
            }
        }
Пример #24
0
        public bool TryParseReferencedEntities(
            IBindableModelContext context,
            INavigationProperty navigationProperty,
            ODataEntityDto oDataEntity,
            out IEnumerable <IEntity> entities)
        {
            IEnumerable <ODataPath> referenceLinks;

            if (!TryGetEntityReferenceLinks(oDataEntity, navigationProperty.Name, out referenceLinks))
            {
                entities = null;
                return(false);
            }

            var readEntities = new List <IEntity>();

            foreach (var referenceLink in referenceLinks)
            {
                readEntities.AddRange(this.entityReader.ReadEntitiesFromPath(context, referenceLink));
            }

            entities = readEntities;
            return(true);
        }
Пример #25
0
 public void UpdateEntity(IBindableModelContext context, IEntitySet navigationRoot, IEntity targetEntity, ODataEntityDto oDataEntity)
 {
     this.SetPropertyValues(context, navigationRoot, targetEntity, oDataEntity);
 }
Пример #26
0
 protected abstract bool TryHandleNavigationProperty(
     IBindableModelContext context,
     IEntity targetEntity,
     ODataEntityDto oDataEntity,
     INavigationProperty navigationProperty,
     IEntitySet navigationRoot);
Пример #27
0
        public Task <ODataEntityDto> Patch(ODataPath path, QueryOptions queryOptions, ODataEntityDto incomingObject)
        {
            var result = this.Execute(Operation.Patch, path, queryOptions, incomingObject);

            return(Task.FromResult(result.FirstOrDefault()));
        }
Пример #28
0
        public IEntity CreateInEntitySet(IBindableModelContext context, IEntitySet entitySet, ODataEntityDto oDataEntity)
        {
            var dependencies  = this.dependencyResolver.ResolveDependencies(context, entitySet, entitySet, oDataEntity);
            var createdEntity = entitySet.CreateEntityInEntitySet(context, dependencies);

            this.SetPropertyValues(context, entitySet, createdEntity, oDataEntity, dependencies.Keys);
            return(createdEntity);
        }