コード例 #1
0
        public void InvalidCasesTest()
        {
            ResourceProperty id = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int))) { CanReflectOnInstanceTypeProperty = false };
            ResourceType customerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "CustomerType", false);
            customerType.AddProperty(id);
            ResourceType orderType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "OrderType", false);
            orderType.AddProperty(id);

            ResourceProperty customerOrders = new ResourceProperty("Orders", ResourcePropertyKind.ResourceSetReference, orderType);
            ResourceProperty orderCustomer = new ResourceProperty("Customer", ResourcePropertyKind.ResourceReference, customerType);
            customerType.AddProperty(customerOrders);
            orderType.AddProperty(orderCustomer);
            customerOrders.CanReflectOnInstanceTypeProperty = false;
            orderCustomer.CanReflectOnInstanceTypeProperty = false;
            customerType.SetReadOnly();
            orderType.SetReadOnly();

            ResourceSet customerSet = new ResourceSet("Customers", customerType);
            ResourceSet orderSet = new ResourceSet("Orders", orderType);

            ResourceAssociationSetEnd end1 = new ResourceAssociationSetEnd(customerSet, customerType, null);
            ResourceAssociationSetEnd end2 = new ResourceAssociationSetEnd(orderSet, orderType, null);

            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ResourceAssociationSet), 
                "Value cannot be null or empty.\r\nParameter name: name", 
                null, end1, end2);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ResourceAssociationSet), 
                "Value cannot be null or empty.\r\nParameter name: name", 
                string.Empty, end1, end2);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ResourceAssociationSet), 
                "Value cannot be null.\r\nParameter name: end1", 
                "Customer_Order", null, end2);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ResourceAssociationSet), 
                "Value cannot be null.\r\nParameter name: end2", 
                "Customer_Order", end1, null);
            ExceptionUtils.CheckInvalidConstructorParameters(
                typeof(ResourceAssociationSet),
                "The ResourceProperty of the ResourceAssociationEnds cannot both be null.", 
                "Customer_Order", end1, end2);

        }
コード例 #2
0
        /// <summary>Initializes a new instance of the <see cref="T:Microsoft.OData.Service.Providers.ResourceAssociationSet" /> class.</summary>
        /// <param name="name">Name of the association set.</param>
        /// <param name="end1"><see cref="T:Microsoft.OData.Service.Providers.ResourceAssociationSetEnd" /> that is at the source end of the association set.</param>
        /// <param name="end2"><see cref="T:Microsoft.OData.Service.Providers.ResourceAssociationSetEnd" /> that is at the target end of the association set.</param>
        public ResourceAssociationSet(string name, ResourceAssociationSetEnd end1, ResourceAssociationSetEnd end2)
        {
            WebUtil.CheckStringArgumentNullOrEmpty(name, "name");
            WebUtil.CheckArgumentNull(end1, "end1");
            WebUtil.CheckArgumentNull(end2, "end2");

            if (end1.ResourceProperty == null && end2.ResourceProperty == null)
            {
                throw new ArgumentException(Strings.ResourceAssociationSet_ResourcePropertyCannotBeBothNull);
            }

            ////if (end1.ResourceType == end2.ResourceType && end1.ResourceProperty == end2.ResourceProperty)
            ////{
            ////    throw new ArgumentException(Strings.ResourceAssociationSet_SelfReferencingAssociationCannotBeBiDirectional);
            ////}

            this.name = name;
            this.end1 = end1;
            this.end2 = end2;
        }
コード例 #3
0
ファイル: DSPMetadata.cs プロジェクト: larsenjo/odata.net
 /// <summary>
 /// Get the key for the given association end.
 /// </summary>
 /// <param name="end">associated end whose key needs to be reduced.</param>
 /// <returns>key for the given association end.</returns>
 private string GetAssociationKey(ResourceAssociationSetEnd end)
 {
     return
         end.ResourceSet.Name + "_" +
         end.ResourceType.FullName + "_" +
         (end.ResourceProperty == null ? String.Empty : end.ResourceProperty.Name);
 }
コード例 #4
0
        /// <summary>
        /// Populate ResourceAssociationSetEnd from the AssociationSetEnd instance.
        /// </summary>
        /// <param name="setEnd">Instance of AssociationSetEnd.</param>
        /// <param name="resourceSet">ResourceSet to which the type referred by the end belongs to.</param>
        /// <param name="resourceType">ResourceType referred by the end.</param>
        /// <param name="resourceProperty">ResourceProperty that takes part in the association.</param>
        /// <returns>An instance of ResourceAssociationSetEnd.</returns>
        private static ResourceAssociationSetEnd PopulateResourceAssociationSetEnd(AssociationSetEnd setEnd, ResourceSet resourceSet, ResourceType resourceType, ResourceProperty resourceProperty)
        {
            ResourceAssociationSetEnd resourceAssociationSetEnd = new ResourceAssociationSetEnd(resourceSet, resourceType, resourceProperty);
#if !EF6Provider
            resourceAssociationSetEnd.Name = setEnd.Name;

            // Populate Annotations
            ObjectContextServiceProvider.PopulateAnnotations(setEnd.MetadataProperties, resourceAssociationSetEnd.AddCustomAnnotation);
#endif
            return resourceAssociationSetEnd;
        }
コード例 #5
0
        /// <summary>
        /// Gets the ResourceAssociationSet instance when given the source association end.
        /// </summary>
        /// <param name="resourceSet">Resource set of the source association end.</param>
        /// <param name="resourceType">Resource type of the source association end.</param>
        /// <param name="resourceProperty">Resource property of the source association end.</param>
        /// <returns>ResourceAssociationSet instance.</returns>
        public override ResourceAssociationSet GetResourceAssociationSet(ResourceSet resourceSet, ResourceType resourceType, ResourceProperty resourceProperty)
        {
            WebUtil.CheckArgumentNull(resourceSet, "resourceSet");
            WebUtil.CheckArgumentNull(resourceType, "resourceType");
            WebUtil.CheckArgumentNull(resourceProperty, "resourceProperty");

            ResourceSet knownSet;
            if (!this.TryResolveResourceSet(resourceSet.Name, out knownSet) || knownSet != resourceSet)
            {
                throw new InvalidOperationException(Strings.BadProvider_UnknownResourceSet(resourceSet.Name));
            }

            ResourceType knownType;
            if (!this.TryResolveResourceType(resourceType.FullName, out knownType) || knownType != resourceType)
            {
                throw new InvalidOperationException(Strings.BadProvider_UnknownResourceType(resourceType.FullName));
            }

            if (resourceType != resourceType.GetDeclaringTypeForProperty(resourceProperty))
            {
                throw new InvalidOperationException(Strings.BadProvider_ResourceTypeMustBeDeclaringTypeForProperty(resourceType.FullName, resourceProperty.Name));
            }

            ResourceType targetType = resourceProperty.ResourceType;
            Debug.Assert(targetType != null, "targetType != null");
            if (targetType.ResourceTypeKind != ResourceTypeKind.EntityType)
            {
                throw new InvalidOperationException(Strings.BadProvider_PropertyMustBeNavigationPropertyOnType(resourceProperty.Name, resourceType.FullName));
            }

            ResourceSet targetSet = InternalGetContainerForResourceType(targetType.InstanceType, this.ResourceSets);
            Debug.Assert(targetSet != null, "targetSet != null");

            // In order to prevent this from becoming a breaking change, we use the name to build up the association whenever possible.
            // If there exists an entity with the same name, then we try and use the full name.
            string associationSetName;
            if (this.MetadataCacheItem.ResourceTypeCacheItems.Count(rt => rt.ResourceType.Name == resourceType.Name) > 1)
            {
                associationSetName = resourceType.FullName.Replace('.', '_') + '_' + resourceProperty.Name;
            }
            else
            {
                associationSetName = resourceType.Name + '_' + resourceProperty.Name;
            }

            // We changed the association logic on the server to create the correct navigation property for V4 to run the tests.
            // The association logic on server side needs to be removed.
            // Calculating targetResourceProperty value here is for V4 and it is not the correct code for V3.
            ResourceProperty targetResourceProperty = null;

            if (targetType != resourceType)
            {
                targetResourceProperty = GetResourcePropertyByResourcePropertyTypeInResourceSet(resourceType, targetSet);
            }
            else
            {
                targetResourceProperty = resourceProperty;
            }

            if (targetResourceProperty != null && !this.MetadataCacheItem.TargetResourcePropertiesCacheItems.ContainsKey(targetSet.Name + "_" + targetResourceProperty.Name))
            {
                this.MetadataCacheItem.TargetResourcePropertiesCacheItems.Add(targetSet.Name + "_" + targetResourceProperty.Name, resourceProperty);
            }

            ResourceAssociationSetEnd sourceEnd = new ResourceAssociationSetEnd(resourceSet, resourceType, resourceProperty);
            ResourceAssociationSetEnd targetEnd = new ResourceAssociationSetEnd(targetSet, targetType, targetResourceProperty);
            return new ResourceAssociationSet(associationSetName, sourceEnd, targetEnd);
        }