public static IEntity CreateEntityInContainedNavigationProperty(
            this INavigationProperty navigationProperty,
            IBindableModelContext context,
            IEntity parentEntity,
            IDictionary <string, IDependency> dependencies)
        {
            if (!navigationProperty.IsContained())
            {
                throw new ArgumentException("Navigation property must be contained.", nameof(navigationProperty));
            }

            if (navigationProperty.IsCollection())
            {
                IContainedCollectionNavigationPropertyBinding binding;
                if (!context.TryGetBinding(navigationProperty, out binding))
                {
                    throw new ArgumentException("The specified navigation property does not support the expected binding.", nameof(navigationProperty));
                }

                return(binding.CreateAndAdd(parentEntity, dependencies));
            }
            else
            {
                IContainedNavigationPropertyBinding binding;
                if (!context.TryGetBinding(navigationProperty, out binding))
                {
                    throw new ArgumentException("The specified navigation property does not support the expected binding.", nameof(navigationProperty));
                }

                return(binding.CreateAndSet(parentEntity, dependencies));
            }
        }
Exemple #2
0
        public void SetUncontainedNavigationProperty(
            IBindableModelContext context,
            IEntity targetEntity,
            INavigationProperty navigationProperty,
            IEnumerable <IEntity> entities)
        {
            ThrowIfContained(navigationProperty);
            if (navigationProperty.IsCollection())
            {
                IUncontainedCollectionNavigationPropertyBinding binding;
                if (context.TryGetBinding(navigationProperty, out binding))
                {
                    // TODO: think about having an AddRange method for better performance
                    entities.ForEach(entity => binding.Add(targetEntity, entity));
                    return;
                }
            }
            else
            {
                IUncontainedNavigationPropertyBinding binding;
                if (context.TryGetBinding(navigationProperty, out binding))
                {
                    binding.Set(targetEntity, entities.Single());
                    return;
                }
            }

            throw new ArgumentException("The specified navigation property does not support the expected binding.", nameof(navigationProperty));
        }
Exemple #3
0
        public bool TryGetLinkedOrInlineEntities(
            IBindableModelContext context,
            ODataEntityDto oDataEntity,
            INavigationProperty navigationProperty,
            IEntitySet navigationRoot,
            out IEnumerable <IEntity> entities)
        {
            if (navigationProperty.IsContained())
            {
                throw new ArgumentException("Navigation property must be uncontained.", nameof(navigationProperty));
            }

            // parse reference links as well as inline entities (both can be included at the same time)
            var resultingEntities = new List <IEntity>();
            var success           = this.TryParseReferencedEntities(context, oDataEntity, navigationProperty, resultingEntities)
                                    | this.TryParseAndCreateInlineEntities(context, oDataEntity, navigationProperty, navigationRoot, resultingEntities);

            if (success)
            {
                entities = resultingEntities;
                return(true);
            }

            entities = null;
            return(false);
        }
Exemple #4
0
        public void RemoveFromUncontainedNavigationProperty(
            IBindableModelContext context,
            IEntity targetEntity,
            INavigationProperty navigationProperty,
            IEnumerable <IEntity> entitiesToRemove)
        {
            ThrowIfContained(navigationProperty);
            if (navigationProperty.IsCollection())
            {
                IUncontainedCollectionNavigationPropertyBinding binding;
                if (context.TryGetBinding(navigationProperty, out binding))
                {
                    entitiesToRemove.ForEach(entityToRemove => binding.Remove(targetEntity, entityToRemove));
                    return;
                }
            }
            else
            {
                IUncontainedNavigationPropertyBinding binding;
                if (context.TryGetBinding(navigationProperty, out binding))
                {
                    binding.Clear(targetEntity);
                    return;
                }
            }

            throw new ArgumentException("The specified navigation property does not support the expected binding.", nameof(navigationProperty));
        }
Exemple #5
0
        protected override bool TryHandleNavigationProperty(
            IBindableModelContext context,
            IEntity targetEntity,
            ODataEntityDto oDataEntity,
            INavigationProperty navigationProperty,
            IEntitySet navigationRoot)
        {
            if (navigationProperty.IsContained())
            {
                IEnumerable <ODataEntityDto> inlineODataEntities;
                if (!oDataEntity.TryGetInlineODataEntities(navigationProperty.Name, out inlineODataEntities))
                {
                    return(false);
                }

                this.CreateInContainedNavigationProperty(context, navigationProperty, navigationRoot, targetEntity, inlineODataEntities);
                return(true);
            }

            IEnumerable <IEntity> entities;

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

            return(false);
        }
Exemple #6
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);
        }
 protected void SetPropertyValues(
     IBindableModelContext context,
     IEntitySet navigationRoot,
     IEntity targetEntity,
     ODataEntityDto oDataEntity)
 {
     this.SetPropertyValues(context, navigationRoot, targetEntity, oDataEntity, Enumerable.Empty <string>());
 }
Exemple #8
0
        public IEnumerable <IEntity> ReadEntitiesFromPath(IBindableModelContext modelContext, IEnumerable <ODataPathSegment> oDataPath)
        {
            if (!oDataPath.Any())
            {
                throw new ArgumentException("The path must not be empty.", nameof(oDataPath));
            }

            var readingHandler = new EntityReadingPathSegmentHandler(modelContext);

            readingHandler.HandlePath(oDataPath);
            return(readingHandler.GetResultingEntities());
        }
Exemple #9
0
 public DefaultSelectItemHandler(
     IEntityReader entityReader,
     IBindableModelContext modelContext,
     IEntity sourceEntity,
     IODataEntityDtoBuilder resultingDtoBuilder,
     Action <IODataEntityDtoBuilder, IEntity> defaultChildDtoInitializer)
 {
     this.entityReader               = entityReader;
     this.modelContext               = modelContext;
     this.sourceEntity               = sourceEntity;
     this.resultingDtoBuilder        = resultingDtoBuilder;
     this.defaultChildDtoInitializer = defaultChildDtoInitializer;
 }
Exemple #10
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);
        }
Exemple #11
0
        public static IEntity CreateEntityInEntitySet(
            this IEntitySet entitySet,
            IBindableModelContext context,
            IDictionary <string, IDependency> dependencies)
        {
            IEntitySetBinding binding;

            if (!context.TryGetBinding(entitySet, out binding))
            {
                throw new ArgumentException("The specified entity set does not support the expected binding.", nameof(entitySet));
            }

            return(binding.CreateAndAdd(dependencies));
        }
Exemple #12
0
        public IEnumerable <IEntity> CreateInContainedNavigationProperty(
            IBindableModelContext context,
            INavigationProperty navigationProperty,
            IEntitySet navigationRoot,
            IEntity parentEntity,
            IEnumerable <ODataEntityDto> oDataEntities)
        {
            var createdEntities = new List <IEntity>();

            foreach (var oDataEntity in oDataEntities)
            {
                createdEntities.Add(this.CreateInContainedNavigationProperty(context, navigationProperty, navigationRoot, parentEntity, oDataEntity));
            }

            return(createdEntities);
        }
Exemple #13
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);
        }
Exemple #14
0
        public IEnumerable <IEntity> CreateInUncontainedNavigationProperty(
            IBindableModelContext context,
            INavigationProperty navigationProperty,
            IEntitySet navigationRoot,
            IEnumerable <ODataEntityDto> oDataEntities)
        {
            IEntitySet targetEntitySet;

            if (navigationProperty.TryGetNavigationPropertyTarget(context, navigationRoot, out targetEntitySet) &&
                targetEntitySet.SupportedOperations.HasFlag(Operation.Post))
            {
                return(oDataEntities.Select(oDataEntity => this.CreateInEntitySet(context, targetEntitySet, oDataEntity)).ToArray());
            }

            throw new InvalidOperationException(
                      "No valid navigation target found for property " + navigationProperty.Name + " in the context of " + navigationRoot.Name + ".");
        }
Exemple #15
0
        private IEnumerable <ODataEntityDto> SerializeEntities(
            IBindableModelContext context,
            IEnumerable <IEntity> entities,
            IEntityType entityType,
            SelectExpandClause selectExpandClause)
        {
            foreach (var entity in entities)
            {
                var dtoBuilder = this.dtoBuilderFactory.Create(entityType);
                if (selectExpandClause == null || selectExpandClause.AllSelected)
                {
                    SelectAllProperties(dtoBuilder, entity);
                }
                else
                {
                    var selectItemHandler = new DefaultSelectItemHandler(this.entityReader, context, entity, dtoBuilder, SelectAllProperties);
                    selectItemHandler.HandleAll(selectExpandClause.SelectedItems);
                }

                yield return(dtoBuilder.DtoUnderConstruction);
            }
        }
Exemple #16
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);
        }
Exemple #17
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);
        }
Exemple #18
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);
        }
Exemple #19
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);
        }
Exemple #20
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);
        }
        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.");
            }
        }
Exemple #22
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);
        }
Exemple #23
0
 public void UpdateEntity(IBindableModelContext context, IEntitySet navigationRoot, IEntity targetEntity, ODataEntityDto oDataEntity)
 {
     this.SetPropertyValues(context, navigationRoot, targetEntity, oDataEntity);
 }
 public EntityReadingPathSegmentHandler(IBindableModelContext modelContext)
 {
     this.modelContext = modelContext;
     this.readContext  = new ReadContext(modelContext);
 }
Exemple #25
0
 public ReadContext(IBindableModelContext modelContext)
 {
     this.result = EmptyResult;
     this.Model  = modelContext;
 }
Exemple #26
0
 public ReadContext(IEnumerable <IEntity> result, IBindableModelContext modelContext)
 {
     this.Model  = modelContext;
     this.result = result;
 }
 public EntityReadingPathSegmentHandler(IBindableModelContext modelContext, IEntity existingEntity)
 {
     this.modelContext = modelContext;
     this.readContext  = new ReadContext(existingEntity.Enumerate(), modelContext);
 }
 protected abstract bool TryHandleNavigationProperty(
     IBindableModelContext context,
     IEntity targetEntity,
     ODataEntityDto oDataEntity,
     INavigationProperty navigationProperty,
     IEntitySet navigationRoot);