Exemplo n.º 1
0
        internal static void AddResourcePropertyFromComplexType(this ResourceType resourceType, ResourceType resourcePropertyComplexType)
        {
            var resourceTypeProperty = new ResourceProperty(
                resourcePropertyComplexType.Name,
                ResourcePropertyKind.ComplexType,
                resourcePropertyComplexType);

            resourceTypeProperty.CanReflectOnInstanceTypeProperty = false;
            resourceType.AddProperty(resourceTypeProperty);
        }
Exemplo n.º 2
0
        /// <summary>Adds a collection of complex or primitive items property.</summary>
        /// <param name="resourceType">The resource type to add the property to.</param>
        /// <param name="name">The name of the property to add.</param>
        /// <param name="itemType">The resource type of the item in the collection.</param>
        public void AddCollectionProperty(ResourceType resourceType, string name, ResourceType itemType)
        {
            var property = new ResourceProperty(
                name,
                ResourcePropertyKind.Collection,
                ResourceType.GetCollectionResourceType(itemType));

            property.CanReflectOnInstanceTypeProperty = false;
            resourceType.AddProperty(property);
        }
Exemplo n.º 3
0
        internal void AddResourceReferenceProperty(ResourceType sourceType, string name, ResourceType targetType, Schema.AssociationType assocType, object defaultValue)
        {
            ResourceProperty resourcePropertyWithDescription = new ResourcePropertyWithDescription(name, ResourcePropertyKind.ResourceReference, targetType);

            resourcePropertyWithDescription.CanReflectOnInstanceTypeProperty = false;
            resourcePropertyWithDescription.CustomState = new ReferenceCustomState(false);
            resourcePropertyWithDescription.GetReferenceCustomState().AssociationType = assocType;
            resourcePropertyWithDescription.GetCustomState().DefaultValue             = defaultValue;
            sourceType.AddProperty(resourcePropertyWithDescription);
        }
Exemplo n.º 4
0
        public void VerifyVersionOfEntityWithGeographyPropertyIsV3()
        {
            ResourceType rt          = new ResourceType(typeof(IDictionary <string, string>), ResourceTypeKind.EntityType, null, "TestNamespace", "TestEntity", false);
            var          keyProperty = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            };
            var geographyProperty = new ResourceProperty("GeographyProperty", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(Geography)))
            {
                CanReflectOnInstanceTypeProperty = false
            };

            rt.AddProperty(keyProperty);
            rt.AddProperty(geographyProperty);
            rt.SetReadOnly();

            Assert.AreEqual(v4, rt.MetadataVersion, "MetadataVersion must be 4.0");
            Assert.AreEqual(MetadataEdmSchemaVersion.Version4Dot0, rt.SchemaVersion, "Schema version must be 4.0");
        }
Exemplo n.º 5
0
        private ResourceType GetCreateComplexResourceType(
            Type clrType)
        {
            if (ReflectionHelper.HasAttribute <DataContractAttribute>(clrType) == false)
            {
                throw new InvalidOperationException(
                          string.Format(
                              "Class must be marked with <DataContractAttribute>: '{0}'.",
                              clrType.FullName));
            }

            ResourceType resourceType;

            if (resourceTypes.TryGetValue(clrType.FullName, out resourceType) == false)
            {
                ResourceType baseType = null;

                if (clrType.BaseType != null &&
                    ReflectionHelper.HasAttribute <DataContractAttribute>(clrType.BaseType))
                {
                    baseType = GetCreateComplexResourceType(clrType.BaseType);
                }

                resourceType = new ResourceType(
                    clrType,
                    ResourceTypeKind.ComplexType,
                    baseType,
                    clrType.Namespace,
                    clrType.Name,
                    clrType.IsAbstract);

                resourceTypes.Add(clrType.FullName, resourceType);

                var props = ReflectionHelper.GetDeclaredProperties <DataMemberAttribute>(clrType);

                foreach (var p in props)
                {
                    if (IsSupportedPrimitiveResourceType(p.PropertyType))
                    {
                        ResourceProperty rp = new ResourceProperty(
                            p.Name,
                            ResourcePropertyKind.Primitive,
                            GetPrimitiveResourceType(p));

                        resourceType.AddProperty(rp);
                    }
                    else if (ReflectionHelper.IsGenericEnumerableType(p.PropertyType))
                    {
                        throw new NotSupportedException("Collection properties on complex types not supported ((c) by Microsoft Odata lib).");
                    }
                }
            }

            return(resourceType);
        }
Exemplo n.º 6
0
        internal void AddComplexCollectionProperty(ResourceType resourceType, string name, ResourceType complexType)
        {
            CollectionResourceType collectionResourceType          = ResourceType.GetCollectionResourceType(complexType);
            ResourceProperty       resourcePropertyWithDescription = new ResourcePropertyWithDescription(name, ResourcePropertyKind.Collection, collectionResourceType);

            resourcePropertyWithDescription.CanReflectOnInstanceTypeProperty = false;
            PropertyCustomState propertyCustomState = new PropertyCustomState();

            resourcePropertyWithDescription.CustomState = propertyCustomState;
            resourceType.AddProperty(resourcePropertyWithDescription);
        }
Exemplo n.º 7
0
        internal static void AddResourcePropertyFromInstanceCollectionResourceType(this ResourceType resourceType, ResourceType propertyCollection)
        {
            var resourceProperty = new ResourceProperty(
                propertyCollection.Name,
                ResourcePropertyKind.Collection,
                ResourceType.GetCollectionResourceType(propertyCollection));

            resourceProperty.CanReflectOnInstanceTypeProperty = false;
            resourceProperty.GetAnnotation().InstanceProperty = (PropertyInfo)propertyCollection.CustomState;
            resourceType.AddProperty(resourceProperty);
        }
Exemplo n.º 8
0
        internal void AddPrimitiveCollectionProperty(ResourceType resourceType, string name, Type propertyType, object defaultValue)
        {
            CollectionResourceType collectionResourceType          = ResourceType.GetCollectionResourceType(ResourceType.GetPrimitiveResourceType(propertyType));
            ResourceProperty       resourcePropertyWithDescription = new ResourcePropertyWithDescription(name, ResourcePropertyKind.Collection, collectionResourceType);

            resourcePropertyWithDescription.CanReflectOnInstanceTypeProperty = false;
            PropertyCustomState propertyCustomState = new PropertyCustomState();

            propertyCustomState.DefaultValue            = defaultValue;
            resourcePropertyWithDescription.CustomState = propertyCustomState;
            resourceType.AddProperty(resourcePropertyWithDescription);
        }
Exemplo n.º 9
0
        /// <summary>Adds a complex property to the specified <paramref name="resourceType"/>.</summary>
        /// <param name="resourceType">The resource type to add the property to.</param>
        /// <param name="name">The name of the property to add.</param>
        /// <param name="complexType">Complex type to use for the property.</param>
        public void AddComplexProperty(ResourceType resourceType, string name, ResourceType complexType)
        {
            if (complexType.ResourceTypeKind != ResourceTypeKind.ComplexType)
            {
                throw new ArgumentException("The specified type for the complex property is not a complex type.");
            }

            ResourceProperty property = new ResourceProperty(name, ResourcePropertyKind.ComplexType, complexType);

            property.CanReflectOnInstanceTypeProperty = false;
            resourceType.AddProperty(property);
        }
Exemplo n.º 10
0
        public void Initialize()
        {
            this.host = new DataServiceHost2Simulator();

            var context = new DataServiceOperationContext(this.host);

            this.dataServiceSimulator = new DataServiceSimulator {
                OperationContext = context
            };

            var providerSimulator = new DataServiceProviderSimulator();

            DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(this.dataServiceSimulator.Instance.GetType(), providerSimulator);
            IDataServiceProviderBehavior   providerBehavior    = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;

            var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "SelectTestNamespace", "Fake", false)
            {
                CanReflectOnInstanceType = false, IsOpenType = true
            };

            resourceType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            var resourceSet = new ResourceSet("FakeSet", resourceType);

            resourceSet.SetReadOnly();

            providerSimulator.AddResourceSet(resourceSet);

            var configuration = new DataServiceConfiguration(providerSimulator);

            configuration.SetEntitySetAccessRule("*", EntitySetRights.All);

            var provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(
                    configuration,
                    staticConfiguration),
                providerSimulator,
                providerSimulator,
                this.dataServiceSimulator,
                false);

            this.dataServiceSimulator.ProcessingPipeline = new DataServiceProcessingPipeline();
            this.dataServiceSimulator.Provider           = provider;
            provider.ProviderBehavior = providerBehavior;
            this.dataServiceSimulator.Configuration = new DataServiceConfiguration(providerSimulator);
            this.dataServiceSimulator.Configuration.DataServiceBehavior.MaxProtocolVersion = ODataProtocolVersion.V4;

            this.responseMessageSimulator = new ODataResponseMessageSimulator();
        }
Exemplo n.º 11
0
        private void CreateResourceType(ContentTypeDefinition contentTypeDefinition)
        {
            // Try to get the resource type from the dictionary
            if (this._resourceTypes.ContainsKey(contentTypeDefinition.Name))
            {
                return;
            }

            // Create resource type
            var resourceTypeContentType = new ResourceType(
                typeof(ContentItem),
                ResourceTypeKind.EntityType,
                null,
                (this as IDataServiceMetadataProvider).ContainerNamespace,
                contentTypeDefinition.Name,
                false);

            resourceTypeContentType.CanReflectOnInstanceType = true;

            var resourceProperty = new ResourceProperty(
                "Id",
                ResourcePropertyKind.Primitive | ResourcePropertyKind.Key,
                ResourceType.GetPrimitiveResourceType(typeof(int)));

            resourceProperty.CanReflectOnInstanceTypeProperty = true;
            resourceTypeContentType.AddProperty(resourceProperty);

            // Add the resource type to the dictionary
            this._resourceTypes[contentTypeDefinition.Name] = resourceTypeContentType;

            var contentItem = this._queryEntityService.New(contentTypeDefinition.Name);

            contentItem.Parts
            .Join(
                inner: contentItem.TypeDefinition.Parts,
                outerKeySelector: contentPart => contentPart.PartDefinition.Name,
                innerKeySelector: contentypePartDefinition => contentypePartDefinition.PartDefinition.Name,
                resultSelector: (contentPart, partDefinition) => contentPart)
            .ToList()
            .ForEach(contentPart => this.CreateResourceProperty(resourceTypeContentType, contentPart));

            // Provide ResourceSet
            ResourceSet resourceSet = new ResourceSet(resourceTypeContentType.Name + "s", resourceTypeContentType);

            resourceTypeContentType.GetAnnotation().ResourceSet = resourceSet;
            // Add annotation to resource set so we can request on this ResourceType
            resourceSet.CustomState = resourceTypeContentType;

            this._resourceSets[resourceTypeContentType] = resourceSet;
        }
Exemplo n.º 12
0
        /// <summary>Adds a key property to the specified <paramref name="resourceType"/>.</summary>
        /// <param name="resourceType">The resource type to add the property to.</param>
        /// <param name="name">The name of the property to add.</param>
        /// <param name="propertyType">The CLR type of the property to add. This can be only a primitive type.</param>
        /// <param name="isKey">true if the property should be a key property.</param>
        private void AddPrimitiveProperty(ResourceType resourceType, string name, Type propertyType, bool isKey)
        {
            ResourceType         type = ResourceType.GetPrimitiveResourceType(propertyType);
            ResourcePropertyKind kind = ResourcePropertyKind.Primitive;

            if (isKey)
            {
                kind |= ResourcePropertyKind.Key;
            }

            ResourceProperty property = new ResourceProperty(name, kind, type);

            property.CanReflectOnInstanceTypeProperty = false;
            resourceType.AddProperty(property);
        }
Exemplo n.º 13
0
 internal void AddComplexProperty(ResourceType resourceType, string name, ResourceType complexType)
 {
     if (complexType.ResourceTypeKind == ResourceTypeKind.ComplexType)
     {
         ResourceProperty resourcePropertyWithDescription = new ResourcePropertyWithDescription(name, ResourcePropertyKind.ComplexType, complexType);
         resourcePropertyWithDescription.CanReflectOnInstanceTypeProperty = false;
         PropertyCustomState propertyCustomState = new PropertyCustomState();
         resourcePropertyWithDescription.CustomState = propertyCustomState;
         resourceType.AddProperty(resourcePropertyWithDescription);
         return;
     }
     else
     {
         throw new InvalidResourceTypeException(complexType.Name, complexType.ResourceTypeKind.ToString(), ResourceTypeKind.ComplexType.ToString());
     }
 }
Exemplo n.º 14
0
        /// <summary>
        /// Add stream properties that are marked NamedStream in clrType to resourceType.
        /// </summary>
        /// <param name="resourceType">the given resource type.</param>
        /// <param name="clrType">backing clr type for the resource.</param>
        /// <param name="inherit">indicates if the resource type has a base type.</param>
        protected static void AddStreamProperties(ResourceType resourceType, Type clrType, bool inherit)
        {
            Debug.Assert(resourceType != null, "resourceType != null");
            Debug.Assert(clrType != null, "clrType != null");

            // Add named streams if there is any.
            // Note Named streams are like virtual properties and each ResourceType will inherit named streams from its parent type.
            // That's why we set inherit to 'false' when the base entity type is not null or else we get name collisions.
            // However if the NamedStreamAttribute is on a base type that is not an entity type, we want to inherit it by setting
            // inherit to 'true'.
            var namedStreamAttributes = clrType.GetCustomAttributes(typeof(NamedStreamAttribute), inherit).Cast <NamedStreamAttribute>().OrderBy(a => a.Name);

            foreach (var namedStream in namedStreamAttributes)
            {
                resourceType.AddProperty(new ResourceProperty((namedStream).Name, ResourcePropertyKind.Stream, PrimitiveResourceTypeMap.TypeMap.GetPrimitive(typeof(Stream))));
            }
        }
Exemplo n.º 15
0
        private void AddProperties(
            MetadataWorkspace workspace,
            ComplexType sourceType,
            ResourceType targetType)
        {
            foreach (var edmp in sourceType.Properties.Where(x => x.IsPrimitiveType))
            {
                ResourceProperty resourceProperty = new ResourceProperty(
                    edmp.Name,
                    ResourcePropertyKind.Primitive,
                    GetPrimitiveResourceType(edmp.PrimitiveType));

                SetNullable(edmp, resourceProperty);

                targetType.AddProperty(resourceProperty);
            }
        }
Exemplo n.º 16
0
        public OperationLinkBuilderTests()
        {
            ResourceType intType = ResourceType.GetPrimitiveResourceType(typeof(int));

            var customerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "FQ.NS", "Customer", false);

            customerType.CanReflectOnInstanceType = false;
            customerType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, intType)
            {
                CanReflectOnInstanceTypeProperty = false
            });
            customerType.SetReadOnly();

            var operation = new ServiceAction("Action", intType, OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("P1", customerType), new ServiceActionParameter("P2", intType) }, null);

            operation.SetReadOnly();
            this.operationWithParameters = new OperationWrapper(operation);

            var typeWithEscapedName = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "FQ NS", "+ /", false);

            typeWithEscapedName.CanReflectOnInstanceType = false;
            typeWithEscapedName.AddProperty(new ResourceProperty("Number", ResourcePropertyKind.Primitive, intType)
            {
                CanReflectOnInstanceTypeProperty = false
            });
            typeWithEscapedName.SetReadOnly();

            operation = new ServiceAction("Action", intType, OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("P1", customerType), new ServiceActionParameter("P2", typeWithEscapedName) }, null);
            operation.SetReadOnly();
            this.operationWithEscapedParameter = new OperationWrapper(operation);

            var bestCustomerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, customerType, "FQ.NS", "BestCustomer", false);

            bestCustomerType.SetReadOnly();

            operation = new ServiceAction("Action", intType, OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("P1", customerType) }, null);
            operation.SetReadOnly();
            this.operationBoundToBaseType = new OperationWrapper(operation);

            this.entityToSerialize = EntityToSerialize.CreateFromExplicitValues(new object(), bestCustomerType, new TestSerializedEntityKey("http://odata.org/Service.svc/Customers/", bestCustomerType.FullName));

            var metadataUri = new Uri("http://odata.org/Service.svc/$metadata");

            this.testSubject = new OperationLinkBuilder("MyContainer", metadataUri);
        }
        private static ResourceType CreateResourceTypeWithKeyProperties(params string[] keyPropertyNames)
        {
            var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "FQ.NS", "EntityType", false)
            {
                CanReflectOnInstanceType = false
            };

            foreach (var keyPropertyName in keyPropertyNames)
            {
                resourceType.AddProperty(new ResourceProperty(keyPropertyName, ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
                {
                    CanReflectOnInstanceTypeProperty = false
                });
            }

            resourceType.SetReadOnly();
            return(resourceType);
        }
Exemplo n.º 18
0
        private static EntityToSerialize CreateEntityToSerialize(bool shouldIncludeTypeSegment)
        {
            ResourceType baseType = new ResourceType(typeof(MyType), ResourceTypeKind.EntityType, null, "TestNamespace", "BaseType", /*isAbstract*/ false);

            baseType.AddProperty(new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, new ResourceType(typeof(int), ResourceTypeKind.Primitive, null, "int")));

            baseType.SetReadOnly();

            Uri serviceUri = new Uri("http://dummy");

            KeySerializer keySerializer = KeySerializer.Create(false);

            Func <ResourceProperty, object> getPropertyValue = p => "fakePropertyValue";

            return(EntityToSerialize.Create(new MyType {
                ID = 42
            }, baseType, "MySet", shouldIncludeTypeSegment, getPropertyValue, keySerializer, serviceUri));
        }
Exemplo n.º 19
0
        public ExpandAndSelectParsingTests()
        {
            this.requestDescription = new RequestDescription(Microsoft.OData.Service.RequestTargetKind.Resource, RequestTargetSource.EntitySet, new Uri("http://fake.org/"));
            var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake", "Type", false)
            {
                CanReflectOnInstanceType = false, IsOpenType = true
            };

            resourceType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            var resourceSet = new ResourceSet("FakeSet", resourceType);

            resourceSet.SetReadOnly();

            this.requestDescription.LastSegmentInfo.TargetResourceType = resourceType;
            this.requestDescription.LastSegmentInfo.TargetResourceSet  = ResourceSetWrapper.CreateForTests(resourceSet);
        }
Exemplo n.º 20
0
        private ResourceType GenerateComplexTypeSchema(Type clrType, ResourceTypeKind resourceTypeKind)
        {
            if (this.complexTypeResourceTypes.ContainsKey(clrType.FullName))
            {
                return(this.complexTypeResourceTypes[clrType.FullName]);
            }
            ResourceType resourceType = new ResourceType(clrType, resourceTypeKind, null, "TenantReporting", clrType.Name, false);

            foreach (PropertyInfo propertyInfo in clrType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy))
            {
                Type type = propertyInfo.PropertyType;
                Type type2;
                if (ReportingSchema.IsNullableType(type, out type2))
                {
                    type = type2;
                }
                ResourcePropertyKind resourcePropertyKind = 1;
                ResourceType         resourceType2        = ResourceType.GetPrimitiveResourceType(type);
                if (resourceType2 == null)
                {
                    if (type.IsEnum || type.IsValueType)
                    {
                        throw new NotSupportedException("struct and enum are not supported. For struct, try to change it to class. For enum, try to change it to integer or string.");
                    }
                    if (type.Equals(clrType))
                    {
                        resourceType2 = resourceType;
                    }
                    else
                    {
                        resourceType2 = this.GenerateComplexTypeSchema(type, 1);
                    }
                    resourcePropertyKind = 4;
                }
                resourceType.AddProperty(new ResourceProperty(propertyInfo.Name, resourcePropertyKind, resourceType2));
            }
            if (resourceTypeKind == 1)
            {
                this.complexTypeResourceTypes.Add(clrType.FullName, resourceType);
            }
            return(resourceType);
        }
        private ResourceType GetComplexResourceType(ContentTypePartDefinition part)
        {
            ResourceType t;
            var          name = part.PartDefinition.Name;

            if (resourceTypes.TryGetValue(name, out t))
            {
                return(t);
            }
            t = new ResourceType(typeof(ContentPart), ResourceTypeKind.ComplexType, null, null, name, false);
            t.CanReflectOnInstanceType = false;
            t.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int))));
            foreach (var field in part.PartDefinition.Fields)
            {
            }
            t.SetReadOnly();
            resourceTypes.Add(name, t);

            return(t);
        }
Exemplo n.º 22
0
 internal void AddPrimitiveProperty(ResourceType resourceType, string name, Type propertyType, ResourcePropertyKind flags, object defaultValue)
 {
     if (flags == ResourcePropertyKind.Primitive || flags == (ResourcePropertyKind.Primitive | ResourcePropertyKind.ETag) || flags == (ResourcePropertyKind.Primitive | ResourcePropertyKind.Key))
     {
         ResourceType         primitiveResourceType = ResourceType.GetPrimitiveResourceType(propertyType);
         ResourcePropertyKind resourcePropertyKind  = ResourcePropertyKind.Primitive;
         resourcePropertyKind = resourcePropertyKind | flags;
         ResourceProperty resourcePropertyWithDescription = new ResourcePropertyWithDescription(name, resourcePropertyKind, primitiveResourceType);
         resourcePropertyWithDescription.CanReflectOnInstanceTypeProperty = false;
         PropertyCustomState propertyCustomState = new PropertyCustomState();
         propertyCustomState.DefaultValue            = defaultValue;
         resourcePropertyWithDescription.CustomState = propertyCustomState;
         resourceType.AddProperty(resourcePropertyWithDescription);
         return;
     }
     else
     {
         throw new ArgumentException(ExceptionHelpers.GetExceptionMessage(Resources.SchemaInvalidKeyOrEtagDiscrepancy, new object[0]), "flags");
     }
 }
Exemplo n.º 23
0
        /// <summary>Adds a key property to the specified <paramref name="resourceType"/>.</summary>
        /// <param name="resourceType">The resource type to add the property to.</param>
        /// <param name="name">The name of the property to add.</param>
        /// <param name="propertyType">The CLR type of the property to add. This can be only a primitive type.</param>
        /// <param name="isKey">true if the property should be a key property.</param>
        private void AddPrimitiveProperty(ResourceType resourceType, string name, Type propertyType, bool isKey)
        {
            var type = ResourceType.GetPrimitiveResourceType(propertyType);

            if (type == null)
            {
                throw new ArgumentException(string.Format("Unable to resolve primitive type {0}", propertyType), "propertyType");
            }

            var kind = ResourcePropertyKind.Primitive;

            if (isKey)
            {
                kind |= ResourcePropertyKind.Key;
            }

            var property = new ResourceProperty(name, kind, type);

            property.CanReflectOnInstanceTypeProperty = false;
            resourceType.AddProperty(property);
        }
Exemplo n.º 24
0
        private MessageWriterBuilder ForNormalRequest()
        {
            var requestDescription = new RequestDescription(RequestTargetKind.Resource, RequestTargetSource.EntitySet, new Uri("http://temp.org/"));

            var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake", "Type", false)
            {
                CanReflectOnInstanceType = false, IsOpenType = true
            };

            resourceType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            var resourceSet = new ResourceSet("FakeSet", resourceType);

            resourceSet.SetReadOnly();

            requestDescription.LastSegmentInfo.TargetResourceType = resourceType;
            requestDescription.LastSegmentInfo.TargetResourceSet  = ResourceSetWrapper.CreateForTests(resourceSet);

            return(this.ForNormalRequest(requestDescription));
        }
Exemplo n.º 25
0
        /// <summary>Helper method to add a reference property.</summary>
        /// <param name="resourceType">The resource type to add the property to.</param>
        /// <param name="name">The name of the property to add.</param>
        /// <param name="targetResourceSet">The resource set the resource reference property points to.</param>
        /// <param name="resourceSetReference">true if the property should be a resource set reference, false if it should be resource reference.</param>
        private void AddReferenceProperty(ResourceType resourceType, string name, ResourceSet targetResourceSet, bool resourceSetReference)
        {
            ResourceProperty property = new ResourceProperty(
                name,
                resourceSetReference ? ResourcePropertyKind.ResourceSetReference : ResourcePropertyKind.ResourceReference,
                targetResourceSet.ResourceType);

            property.CanReflectOnInstanceTypeProperty = false;
            resourceType.AddProperty(property);

            // We don't support type inheritance so the property can only point to the base resource type of the target resource set
            // We also don't support MEST, that is having two resource sets with the same resource type, so we can determine
            //   the resource set from the resource type. That also means that the property can never point to different resource sets
            //   so we can precreate the ResourceAssociationSet for this property right here as we have all the information.
            property.CustomState = new ResourcePropertyAnnotation()
            {
                ResourceAssociationSet = new ResourceAssociationSet(
                    resourceType.Name + "_" + name + "_" + targetResourceSet.Name,
                    new ResourceAssociationSetEnd(resourceType.GetAnnotation().ResourceSet, resourceType, property),
                    new ResourceAssociationSetEnd(targetResourceSet, targetResourceSet.ResourceType, null))
            };
        }
Exemplo n.º 26
0
        /// <summary>
        /// Returns a test instance of one of the provider OM types.
        /// </summary>
        /// <param name="type">The type to get instance of.</param>
        /// <returns>The </returns>
        public static object GetTestInstance(Type type)
        {
            if (type == typeof(ResourceType))
            {
                ResourceProperty p = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int)))
                {
                    CanReflectOnInstanceTypeProperty = false
                };
                ResourceType resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "NoBaseType", false);
                Assert.IsTrue(resourceType.KeyProperties.Count == 0, "It's okay not to have key properties, since we haven't set this type to readonly yet");
                resourceType.AddProperty(p);
                return(resourceType);
            }
            else if (type == typeof(ResourceProperty))
            {
                return(new ResourceProperty("NonKeyProperty", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int))));
            }
            else if (type == typeof(ServiceOperation))
            {
                ServiceOperationParameter parameter = new ServiceOperationParameter("o", ResourceType.GetPrimitiveResourceType(typeof(string)));
                ServiceOperation          operation = new ServiceOperation("Dummy", ServiceOperationResultKind.Void, null, null, "POST", new ServiceOperationParameter[] { parameter });
                return(operation);
            }
            else if (type == typeof(ServiceOperationParameter))
            {
                return(new ServiceOperationParameter("o", ResourceType.GetPrimitiveResourceType(typeof(string))));
            }
            else if (type == typeof(ResourceSet))
            {
                ResourceProperty p = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int)));

                ResourceType resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "NoBaseType", false);
                resourceType.AddProperty(p);
                return(new ResourceSet("Customers", resourceType));
            }

            throw new Exception(String.Format("Unexpected type encountered: '{0}'", type.FullName));
        }
Exemplo n.º 27
0
 private void BuildResourceType(IEntity entity, ResourceType resourceType, Dictionary <string, ResourceType> complexTypeResourceTypes)
 {
     if (ResourceType.GetPrimitiveResourceType(entity.ClrType) == null)
     {
         foreach (PropertyInfo propertyInfo in entity.ClrType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy))
         {
             if (entity.ReportPropertyCmdletParamsMap == null || !entity.ReportPropertyCmdletParamsMap.ContainsKey(propertyInfo.Name) || this.IsEntityPropertyVisibleForCurrentUser(entity, propertyInfo.Name))
             {
                 Type type = propertyInfo.PropertyType;
                 Type type2;
                 if (ReportingSchema.IsNullableType(type, out type2))
                 {
                     type = type2;
                 }
                 ResourcePropertyKind resourcePropertyKind = 1;
                 ResourceType         resourceType2        = ResourceType.GetPrimitiveResourceType(type);
                 if (resourceType2 == null)
                 {
                     if (type.IsEnum || type.IsValueType)
                     {
                         throw new NotSupportedException("struct and enum are not supported. For struct, try to change it to class. For enum, try to change it to integer or string.");
                     }
                     if (type.Equals(entity.ClrType))
                     {
                         resourceType2 = resourceType;
                     }
                     else
                     {
                         resourceType2 = complexTypeResourceTypes[type.FullName];
                     }
                     resourcePropertyKind = 4;
                 }
                 resourceType.AddProperty(new ResourceProperty(propertyInfo.Name, resourcePropertyKind | (entity.KeyMembers.Contains(propertyInfo.Name) ? 2 : 0), resourceType2));
             }
         }
     }
     resourceType.SetReadOnly();
 }
Exemplo n.º 28
0
        public void DataServiceProviderWrapperShouldFailOnMultipleActionsWithSameNameAndBindingType()
        {
            var entityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake.NS", "Type", false)
            {
                CanReflectOnInstanceType = false
            };

            entityType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            entityType.SetReadOnly();

            var resourceSet = new ResourceSet("MyEntitySet", entityType);

            resourceSet.SetReadOnly();

            ResourceType stringType       = ResourceType.GetPrimitiveResourceType(typeof(string));
            var          duplicateAction1 = new ServiceAction("Duplicate", stringType, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", entityType), new ServiceActionParameter("p2", stringType) }, null);

            duplicateAction1.SetReadOnly();
            var duplicateAction2 = new ServiceAction("Duplicate", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("p1", entityType) }, null);

            duplicateAction2.SetReadOnly();

            var actionProvider = new TestActionProvider
            {
                GetServiceActionsCallback = ctx => new[] { duplicateAction1, duplicateAction2 }
            };

            var providerWrapper = CreateProviderWrapper(actionProvider, p => p.AddResourceSet(resourceSet));

            Action getVisibleOperations = () => providerWrapper.GetVisibleOperations().ToList();

            getVisibleOperations.ShouldThrow <DataServiceException>()
            .WithMessage(ErrorStrings.DataServiceActionProviderWrapper_DuplicateAction("Duplicate"))
            .And.StatusCode.Should().Be(500);
        }
Exemplo n.º 29
0
        internal static void AddResourcePropertyFromInstancePropertyInfo(this ResourceType resourceType, PropertyInfo property)
        {
            var resourcePropertyType      = ResourceType.GetPrimitiveResourceType(property.PropertyType);
            var innerEnumerableType       = TypeSystem.GetIEnumerableElementType(property.PropertyType);
            var innerResourcePropertyType = null != innerEnumerableType?ResourceType.GetPrimitiveResourceType(innerEnumerableType) : null;

            resourcePropertyType = null == resourcePropertyType && null != innerResourcePropertyType?ResourceType.GetCollectionResourceType(innerResourcePropertyType) : resourcePropertyType;

            if (null == resourcePropertyType)
            {
                return;
            }

            var resourcePropertyKind = null == innerResourcePropertyType ? ResourcePropertyKind.Primitive : ResourcePropertyKind.Collection;
            var resourceProperty     = new ResourceProperty(
                property.Name,
                resourcePropertyKind,
                resourcePropertyType);

            resourceProperty.CanReflectOnInstanceTypeProperty = false;
            resourceProperty.GetAnnotation().InstanceProperty = property;
            resourceType.AddProperty(resourceProperty);
        }
Exemplo n.º 30
0
        public void ResourceTypeMustBeDeclaringType_2()
        {
            ResourceType baseCustomerType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Test", "Employee", true /*isAbstract*/);
            ResourceType customerType     = new ResourceType(typeof(object), ResourceTypeKind.EntityType, baseCustomerType, "Test", "Customer", false /*isAbstract*/);
            ResourceSet  customerSet      = new ResourceSet("CustomerSet", customerType);

            ResourceType     orderType     = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Test", "Employee", false /*isAbstract*/);
            ResourceProperty orderProperty = new ResourceProperty("Orders", ResourcePropertyKind.ResourceSetReference, orderType);

            baseCustomerType.AddProperty(orderProperty);

            try
            {
                new ResourceAssociationSetEnd(customerSet, customerType, orderProperty);
                Assert.Fail("Creating resource association set end should never have succeeded");
            }
            catch (ArgumentException e)
            {
                Assert.AreEqual(
                    DataServicesResourceUtil.GetString("ResourceAssociationSetEnd_ResourceTypeMustBeTheDeclaringType", customerType.FullName, orderProperty.Name),
                    e.Message);
            }
        }