Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
0
        public void ApplyNavigationProperty(
            INavigationProperty navigationProperty,
            IEnumerable <IEntity> entities,
            Action <IODataEntityDtoBuilder, IEntity> childDtoInitializer)
        {
            if (!navigationProperty.IsCollection())
            {
                var singleEntity = entities.SingleOrDefault();
                if (singleEntity != null)
                {
                    var childDtoBuilder = this.dtoBuilderFactory.Create(navigationProperty.TargetType);
                    childDtoInitializer(childDtoBuilder, singleEntity);

                    this.navigationProperties.Add(
                        new ODataNavigationProperty {
                        Name = navigationProperty.Name, Value = childDtoBuilder.DtoUnderConstruction
                    });
                }
            }
            else
            {
                var childDtoBuilders = new List <IODataEntityDtoBuilder>();
                foreach (var entity in entities)
                {
                    var childDtoBuilder = this.dtoBuilderFactory.Create(navigationProperty.TargetType);
                    childDtoInitializer(childDtoBuilder, entity);
                    childDtoBuilders.Add(childDtoBuilder);
                }

                var childDtos = childDtoBuilders.Select(b => b.DtoUnderConstruction).ToArray();
                this.navigationProperties.Add(new ODataNavigationProperty {
                    Name = navigationProperty.Name, Value = childDtos
                });
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        protected override void Prefilter(IOrmAttribute attribute, Dictionary <string, Defaultable> attributeGroupItems)
        {
            switch (propertyOwner.PropertyKind)
            {
            case PropertyKind.Scalar:
            {
                IScalarProperty scalarProperty = (IScalarProperty)propertyOwner;
                InternalPrefilter(scalarProperty, attribute, attributeGroupItems);
                break;
            }

            case PropertyKind.Structure:
            {
                IStructureProperty structureProperty = (IStructureProperty)propertyOwner;
                InternalPrefilter(structureProperty, attribute, attributeGroupItems);
                break;
            }

            case PropertyKind.Navigation:
            {
                INavigationProperty navigationProperty = (INavigationProperty)propertyOwner;
                InternalPrefilter(navigationProperty, attribute, attributeGroupItems);
                break;
            }
            }
        }
Ejemplo n.º 7
0
        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));
            }
        }
Ejemplo n.º 8
0
 private static void ThrowIfContained(INavigationProperty navigationProperty)
 {
     if (navigationProperty.IsContained())
     {
         throw new ArgumentException("Navigation property must be uncontained.", nameof(navigationProperty));
     }
 }
Ejemplo n.º 9
0
        public bool IsImplementedBy(IInterface @interface)
        {
            bool result = false;

            if (@interface != null)
            {
                switch (this.PropertyKind)
                {
                case PropertyKind.Scalar:
                {
                    IScalarProperty thisProperty = (IScalarProperty)this;

                    result = @interface.Properties.Any(delegate(IPropertyBase propertyItem)
                        {
                            IScalarProperty scalarProperty = (IScalarProperty)propertyItem;

                            return(Util.StringEqual(thisProperty.Name, scalarProperty.Name, true) &&
                                   thisProperty.Type.EqualsTo(scalarProperty.Type));
                        });

                    break;
                }

                case PropertyKind.Structure:
                {
                    IStructureProperty thisProperty = (IStructureProperty)this;
                    result = @interface.Properties.Any(delegate(IPropertyBase propertyItem)
                        {
                            IStructureProperty structureProperty = (IStructureProperty)propertyItem;

                            return(Util.StringEqual(thisProperty.Name, structureProperty.Name, true) &&
                                   thisProperty.TypeOf == structureProperty.TypeOf &&
                                   thisProperty.TypeOf != null);
                        });

                    break;
                }

                case PropertyKind.Navigation:
                {
                    INavigationProperty thisProperty = (INavigationProperty)this;
                    result = @interface.NavigationProperties.Any(delegate(INavigationProperty navigationProperty)
                        {
                            var thisAssociations = thisProperty.PersistentTypeHasAssociations;

                            return(Util.StringEqual(thisProperty.Name, navigationProperty.Name, true) &&
                                   thisAssociations.TargetPersistentType == navigationProperty.PersistentTypeHasAssociations.TargetPersistentType &&
                                   thisAssociations.TargetPersistentType != null);
                        });

                    break;
                }
                }
            }

            return(result);
        }
Ejemplo n.º 10
0
        private void InternalPrefilter(INavigationProperty property, IOrmAttribute attribute, Dictionary <string, Defaultable> attributeGroupItems)
        {
            OrmFieldAttribute fieldAttribute = attribute as OrmFieldAttribute;

            if (fieldAttribute != null)
            {
                Prefilter(property, fieldAttribute, attributeGroupItems);
            }
        }
Ejemplo n.º 11
0
        public bool TryGetNavigationProperty(string sourcePropertyName, out INavigationProperty navigationProperty)
        {
            if (this.navigationProperties == null)
            {
                throw new InvalidOperationException("Navigation properties haven't been set when building this entity type.");
            }

            return(this.navigationProperties.TryGetValue(sourcePropertyName, out navigationProperty));
        }
Ejemplo n.º 12
0
        internal static void ValidateNavigationPropertyAssociation(NavigationProperty navigationProperty, ValidationContext context)
        {
            if (navigationProperty.PersistentTypeHasAssociations == null)
            {
                INavigationProperty navProp = navigationProperty;

                context.LogError(
                    string.Format(ERROR_NO_ASSOCIATION_ASSIGNED,
                                  navigationProperty.Name, navProp.Owner.Name),
                    CODE_NO_ASSOCIATION_ASSIGNED, new ModelElement[] { navigationProperty });
            }
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
        public Func <object, object, bool> CreatMatchForeignKeyDelegate(INavigationProperty navigationProperty)
        {
            Expression body = null;

            var parameterExpression      = Expression.Parameter(typeof(object));
            var valueParameterExpression = Expression.Parameter(typeof(object));

            var paramConvert = Expression.Convert(parameterExpression, _entity.ClrType);
            var valueConvert = Expression.Convert(valueParameterExpression, navigationProperty.TargetEntity.ClrType);

            IDictionary <IScalarProperty, IScalarProperty> keyProperties = null;

            if (navigationProperty.Multiplicity == NavigationPropertyMultiplicity.One || navigationProperty.Multiplicity == NavigationPropertyMultiplicity.ZeroOrOne)
            {
                if (navigationProperty.TargetNavigationProperty != null && (navigationProperty.TargetNavigationProperty.Multiplicity == NavigationPropertyMultiplicity.One || navigationProperty.TargetNavigationProperty.Multiplicity == NavigationPropertyMultiplicity.ZeroOrOne))
                {
                    keyProperties = _primaryKeys
                                    .Where(p => p.IsPrimaryKey)
                                    .Select((p, i) => new { Prop = p, Index = i })
                                    .ToDictionary(
                        p => p.Prop,
                        p => navigationProperty.TargetEntity.GetProperties().Where(x => x.IsPrimaryKey).ElementAt(p.Index));
                }
            }

            if (keyProperties == null)
            {
                keyProperties = navigationProperty.ForeignKeyProperties.ToDictionary(p => p.Dependant, p => p.Principal);
            }

            foreach (var item in keyProperties)
            {
                var propLeft  = Expression.Property(paramConvert, item.Key.Name);
                var propRight = Expression.Property(valueConvert, item.Value.Name);

                Expression expression = GetPropertyCompareExpression(propLeft, propRight);

                if (body == null)
                {
                    body = expression;
                }
                else
                {
                    body = Expression.And(body, expression);
                }
            }

            var lambda = Expression.Lambda <Func <object, object, bool> >(body, parameterExpression, valueParameterExpression);

            return(lambda.Compile());
        }
Ejemplo n.º 15
0
    public string ForSetter(IPropertyBase property, IPropertiesBuilder propertiesBuilder)
    {
        string result = string.Empty;

        if (this.ActiveDTOStage)
        {
            return(result);
        }

        switch (property.PropertyKind)
        {
        case PropertyKind.Scalar:
        {
            IOrmAttribute[] typeAttributes = propertiesBuilder.GetPropertyTypeAttributes(property);
            OrmKeyAttribute keyAttribute   = (OrmKeyAttribute)typeAttributes.Single(item => item is OrmKeyAttribute);

            result = keyAttribute.Enabled ? VISIBILITY_PRIVATE : string.Empty;
            break;
        }

        case PropertyKind.Navigation:
        {
            IOrmAttribute[] typeAttributes = propertiesBuilder.GetPropertyTypeAttributes(property);
            OrmKeyAttribute keyAttribute   = (OrmKeyAttribute)typeAttributes.Single(item => item is OrmKeyAttribute);

            INavigationProperty navigationProperty = (INavigationProperty)property;
            if (navigationProperty.Multiplicity == MultiplicityKind.Many || keyAttribute.Enabled)
            {
                result = VISIBILITY_PRIVATE;
            }

            break;
        }
        }

        // only when result is not directly 'private' because of business rules we can adjust it from settings
        if (string.IsNullOrEmpty(result))
        {
            PropertyAccessModifier higherModifier = property.PropertyAccess.GetHigherModifier();

            if (property.PropertyAccess.Setter != PropertyAccessModifier.Public && property.PropertyAccess.Setter != higherModifier)
            {
                result = GetPropertyAccessModifierString(property.PropertyAccess.Setter);
            }
        }

        return(result);
    }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
0
        public static void ReadCollectionNavigationProperty(ReadContext readContext, INavigationProperty navigationProperty)
        {
            ICollectionNavigationPropertyBinding binding;

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

            IEntity parentEntity;

            if (TryGetParentEntity(readContext, out parentEntity))
            {
                readContext.SetResult(binding.GetAll(parentEntity));
            }
        }
Ejemplo n.º 18
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);
        }
Ejemplo n.º 19
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 + ".");
        }
Ejemplo n.º 20
0
    public string EscapeType(IPropertyBase property)
    {
        string result = null;

        if (property is IScalarProperty)
        {
            IScalarProperty scalarProperty = (IScalarProperty)property;
            Type            clrType        = scalarProperty.Type.TryGetClrType(null);
            string          typeName;

            if (clrType != null)
            {
                typeName = Escape(clrType);
                Defaultable <bool> nullable = scalarProperty.FieldAttribute.Nullable;
                bool isNullable             = !nullable.IsDefault() && nullable.Value;
                if (clrType.IsValueType && isNullable)
                {
                    typeName = String.Format(CultureInfo.InvariantCulture, SYSTEM_NULLABLE_FORMAT, typeName);
                }
            }
            else
            {
                typeName = scalarProperty.Type.FullName;
            }

            result = typeName;
        }
        else if (property is INavigationProperty)
        {
            INavigationProperty navigationProperty = (INavigationProperty)property;
            result = navigationProperty.GetPropertyType(_code, EscapeNameWithNamespace,
                                                        delegate(OrmType type, string s)
            {
                return(BuildXtensiveType(type, s));
            });
        }
        else if (property is IStructureProperty)
        {
            IStructureProperty structureProperty = (IStructureProperty)property;
            result = EscapeNameWithNamespace(structureProperty.TypeOf, property.Owner);
        }

        return(result);
    }
Ejemplo n.º 21
0
        public void ProcessNavigationProperty(INavigationProperty property)
        {
            var tabCount = this.GeneratorConfiguration.HierarchyStack.Count;
            var tabText  = this.CurrentTabText;
            HandlerStackItem handlerStackItem;

            if (property.Facets.Length > 0)
            {
                var facetList       = property.Facets.Select(f => string.Format("{0}[{1}]", tabText, f.AttributeCode)).ToMultiLineList();
                var uiHierarchyPath = property.Facets.GetUIHierarchyPathList(generatorOptions.PrintMode);

                this.CurrentUIHierarchyPath = uiHierarchyPath;

                WriteLine(facetList, PrintMode.PrintFacetsOnly);
                WriteLine(uiHierarchyPath, PrintMode.PrintUIHierarchyPathOnly);
            }

            handlerStackItem = this.GeneratorConfiguration.HandleFacets(property);

            WriteLine("{0}{1} ({2})", PrintMode.All, tabText, property.Name, property.DataType.Name);

            if (!handlerStackItem.DoNotFollow)
            {
                this.GeneratorConfiguration.Push(handlerStackItem);

                foreach (var childEntity in property.ChildEntities)
                {
                    recursionEntitiesList.AddOrUpdateDictionary(childEntity.Name, new EntityCount(childEntity), t => t.Count++);

                    if (recursionEntitiesList.Any(e => e.Value.Count > generatorOptions.RecursionStackLimit))
                    {
                        WriteError("Recursion limit of '{0}' hit.  Change this in GeneratorOptions.RecursionStackLimit", generatorOptions.RecursionStackLimit);
                        return;
                    }

                    using (this.GeneratorConfiguration.BeginChild(childEntity))
                    {
                        ProcessEntity(childEntity);
                    }
                }
            }

            this.GeneratorConfiguration.AddNavigationProperty(property);
        }
Ejemplo n.º 22
0
        public static void ReadCollectionNavigationProperty(
            ReadContext readContext,
            INavigationProperty navigationProperty,
            IEnumerable <KeyValuePair <string, object> > keys)
        {
            ICollectionNavigationPropertyBinding binding;

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

            IEntity parentEntity;

            if (TryGetParentEntity(readContext, out parentEntity))
            {
                IEntity entity;
                readContext.SetResult(binding.TryGetByKeys(parentEntity, keys, out entity) ? entity.Enumerate() : ReadContext.EmptyResult);
            }
        }
Ejemplo n.º 23
0
        public static string GetPropertyType(this INavigationProperty @this, CodeDomProvider code,
                                             Func <IPersistentType, IPersistentType, string> escapeNameWithNamespaceFunc,
                                             Func <OrmType, string, string> buildXtensiveTypeFunc)
        {
            string result = string.Empty;

            IPersistentType ownerPersistentType = @this.OwnerPersistentType;
            bool            isSource            = @this.PersistentTypeHasAssociations.SourcePersistentType == ownerPersistentType;

            IPersistentType targetPersistentType = isSource
                                                      ? @this.PersistentTypeHasAssociations.TargetPersistentType
                                                      : @this.PersistentTypeHasAssociations.SourcePersistentType;

            IPersistentType oppositePersistentType = isSource
                                                      ? @this.PersistentTypeHasAssociations.SourcePersistentType
                                                      : @this.PersistentTypeHasAssociations.TargetPersistentType;

            string targetTypeName = escapeNameWithNamespaceFunc(targetPersistentType, oppositePersistentType);

            switch (@this.Multiplicity)
            {
            case MultiplicityKind.Many:
            {
                result = @this.TypedEntitySet == null
                                     ? buildXtensiveTypeFunc(OrmType.EntitySet, targetTypeName) //OrmUtils.BuildXtensiveType(OrmType.EntitySet, targetTypeName)
                                     : escapeNameWithNamespaceFunc(@this.TypedEntitySet, @this.Owner);

                break;
            }

            case MultiplicityKind.ZeroOrOne:
            case MultiplicityKind.One:
            {
                result = targetTypeName;
                break;
            }
            }

            return(result);
        }
Ejemplo n.º 24
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);
        }
Ejemplo n.º 25
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);
        }
Ejemplo n.º 26
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);
        }
Ejemplo n.º 27
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);
        }
Ejemplo n.º 28
0
        public static bool TryGetNavigationPropertyTarget(
            this INavigationProperty navigationProperty,
            IModelContext context,
            IEntitySet navigationRoot,
            out IEntitySet navigationTarget)
        {
            if (navigationProperty.IsContained())
            {
                throw new ArgumentException("Only uncontained navigation properties can have a navigation target.");
            }

            var edmNavigationTarget = navigationRoot.ResultingEdmType.FindNavigationTarget(navigationProperty.ResultingEdmType);

            if (edmNavigationTarget != null)
            {
                if (context.TryGetEntitySet(edmNavigationTarget.Name, out navigationTarget))
                {
                    return(true);
                }
            }

            navigationTarget = null;
            return(false);
        }
Ejemplo n.º 29
0
        public static void ReadSingleNavigationProperty(ReadContext readContext, INavigationProperty navigationProperty)
        {
            var multiplicity = navigationProperty.ResultingEdmType.TargetMultiplicity();

            if (!(multiplicity == EdmMultiplicity.One || multiplicity == EdmMultiplicity.ZeroOrOne))
            {
                throw new ArgumentException("The specified navigation property is not a single-value property.", nameof(navigationProperty));
            }

            ISingleNavigationPropertyBinding binding;

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

            IEntity parentEntity;

            if (TryGetParentEntity(readContext, out parentEntity))
            {
                IEntity entity;
                readContext.SetResult(binding.TryGet(parentEntity, out entity) ? entity.Enumerate() : ReadContext.EmptyResult);
            }
        }
Ejemplo n.º 30
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);
        }